Wednesday, July 6, 2016

How to write custom collectors for Immutable Collection

I have discussed about the Collector in short and wrote a sample code that will collect the data from Stream in ImmutableSet collection. ImmutableSet is a class in Google-Guava library. Here is the link for the Collector.

The article describes a way in which we can create our own Collector and use it as mutable reduction operation.

Below is the example on how we can use our custom collector ImmutableSetCollector to collect elements from stream into Set of type ImmutableSet.

  Set<String> list
                       .collect(new ImmutableSetCollector<>());

Here is Github link for code. In next post we will see custom implementations of Set interface of JDK implementations like HashSet(with initial capacity), LinkedHashSet(with initial capacity), TreeSet(with Comparator).

No comments:

Post a Comment

Ads Inside Post