Tag: Collections

How to Shuffle or Randomize a List in Java

In this post, we will discuss how to shuffle or randomize a List in Java using Fisher–Yates shuffle algorithm and shuffle() method provided by the Collections class.

Flatten Stream of Arrays or Lists in Java using Stream.concat() method

Java 8 Streams can holds different types of data. For example, Stream<T>, Stream<T[]>, Stream<List<T>> – all these are valid streams in Java 8. We have seen in the previous post that flatMap() method can be used for flattening Streams in Java. In this post, we will flatten Stream of Arrays or Lists using Stream.concat() method …

Reverse a List in Java (In-place)

In this post, we will see how to reverse a List in Java by modifying the list in-place. That means that the conversion should take place without using any auxiliary list, by overwriting the existing elements of the specified list.   Assume that the specified list is modifiable. For immutable lists, an UnsupportedOperationException should be …

Java 8 Stream.reduce() method code examples

We can perform reduction operation on elements of the stream by using Stream.reduce() method that returns an Optional describing the reduced object or the the reduced value itself. In this post, we will see few simple example of Stream.reduce() method.

Guava’s ImmutableSet Class in Java

In this post, we will talk about Guava’s ImmutableSet class in Java which has many static utility methods for creating Immutable Sets in Java.