Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaUsing Guava’s Collectors for Collecting Streams to Immutable Collections in Java

Using Guava’s Collectors for Collecting Streams to Immutable Collections in Java

Guava is a set of core libraries from Google that includes new collection types. For example, we multimap and multisets, immutable collections, a graph library, and utilities for concurrency. It is used by many companies. In this article, we are going to see how to use Guava’s Collectors for Collecting Streams to Immutable Collections in Java. To use guava we need to add Maven dependency. Otherwise, we have to add this library externally to our project.

Illustration:

<dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>22.0</version>
</dependency>

Let us go through the same important concepts involving methods been involved prior before jumping to implementation for clarity’s sake of understanding. The concept of streams plays a major role here that is been introduced in Java 8. A stream represents a sequence of elements and supports different kinds of operations to perform computations or bulk operations on those elements.

  • A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  • Streams don’t change the original data structure, they only provide the result as per the pipelined methods.
  • Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.

Syntax:

static IntStream range (int start, int end)

Return type: An integer stream

Description: This method is used to return a sequential order of Integer Stream

Stream intStream boxed()

Return type: Stream

Description: This method returns Stream consisting of the elements of this stream

Example 1:

Java




// Java Program to illustrate Use Guava's Collectors
// for Collecting Streams to Immutable Collections
 
// Importing utility classes from java.util package
// Importing guava Library
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
 
// Main class
public class App {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a stream of Integers from 0 to 9
        // by using boxed method converting that stream
        // into stream of elements
        List<Integer> list
            = IntStream.range(0, 9).boxed().collect(
                ImmutableList.toImmutableList());
        // printing the list
        System.out.println(list);
    }
}


 

 

Output:

 

[0, 1, 2, 3, 4, 5, 6, 7, 8]

Note: In the above output we have printed an immutable list of Integers. If we try to modify the list, it will throw an UnsupportedOperationException.

 

Example 2:

 

Java




// Java Program to illustrate Use Guava's Collectors
// for Collecting Streams to Immutable Collections
// Where UnsupportedOperationException is thrown
 
// Importing utility classes from java.util package
// Importing Guava library
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
 
// Main class
public class App {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a stream of Integers from 0 to 9
        // by using boxed method converting that stream
        // into stream of elements
        List<Integer> list
            = IntStream.range(0, 9).boxed().collect(
                ImmutableList.toImmutableList());
 
        // Adding more elements to immutatble list
        // Note: Trying to add
        list.add(12);
        list.add(13);
        list.add(14);
 
        // Print and display the List
        System.out.println(list);
    }
}


 

 

Output:

 

Exception in thread "main" java.lang.UnsupportedOperationException
at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:220)
at james.App.main(App.java:15)

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments