All Posts About Java

Streams in Java 8

Streams in Java 8

Ashish Lahoti

One of the major feature of Java 8 is addition of Stream. It also has introduced the functional programming in Java. We will discuss different stream operations available in Collection, Array, IntStream with examples. We will also discuss the difference between Intermediate and Terminal operations.

Immutable Class in Java

Immutable Class in Java

Ashish Lahoti

In this tutorial, we’ll learn about Immutable Class and its benefits in thread-safety, caching and collections. We will also look at rules to create immutable classes and eventually we’ll write an Immutable Class from scratch in Java.

Bubble Sort

Bubble Sort

Ashish Lahoti

Why it is called bubble sort ?

Bubble Sort is nothing but a comparison algorithm where -

  • At the end of first iteration, largest element in the array get placed at last index
  • At the end of second iteration, second largest element in the array get placed at second last index and so on…

This way large elements are moving towards the last indexes and hence small elements are moving towards the starting indexes which is also termed as smaller elements “bubble” to the top of the list that is why it is called bubble sort.

Design Elevator (Lift) in Java

Design Elevator (Lift) in Java

Ashish Lahoti

Implementation of Elevator or Lift has been asked in many interviews. I have tried to implement it using muti-threading and TreeSet. TreeSet is used to store incoming request. It is a good choice here as it removes the duplicate requests and implements NavigableSet which provide you methods such as floor and ceiling.

Elevator in this program implements following features -

  • If elevator is going up or down, it checks for nearest floor request to process first in that direction.
  • If there is no request to process, it waits at last processed floor.
  • If a new request comes while elevator is processing a request. It process the new request first if it is nearest than the processing floor in same direction.