Home » Java programming language

How to convert Iterable to Collection in Java?

Converting Iterable to Collection: Here, we are going to learn how to convert Iterable to Collection in Java programming language?
Submitted by Preeti Jain, on September 04, 2019

There are various ways to convert Iterable to Collection in Java programming language.

  1. With the help of creating a utility function
    1. By using for loop
    2. By using forEach() method of Iterable
    3. By using Iterator
  2. With the help of stream with collect() method in Java 8
    • With the help of utility function
      In this method, we will change or convert Iterable to Collection explicitly (i.e. we will take each element in an object manually).

i) By using for Loop

// Java program to demonstrate the example of 
// converting an Iterable into a Collection by using for loop 

import java.util.*;
import java.io.*;

public class ConvertIterableToCollection {
    // This is a user defined method which is used to 
    // convert Iterable to Collection
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            // Create a blank Collection to hold the result 
            Collection < T > collect = new LinkedList < T > ();

            // By using for loop to iterate through the 
            // iterable to add each element
            for (T type: iterable)
                collect.add(type);

            return collect;
        }

    public static void main(String[] args) {
        Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
        System.out.println("The values of Iterable list are : " + itr);

        Collection < Double > coll = convertCollectionFromIterable(itr);
        System.out.println("The values of Collection list are : " + coll);
    }
}

Output

E:\Programs>javac ConvertIterableToCollection.java

E:\Programs>java ConvertIterableToCollection
 The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
 The values of Collection list are : [10.0, 20.0, 30.0, 40.0] 

ii) By using forEach() of Iterable

This method is available in Java 8 or higher versions so it supports java8 or higher versions.

// Java program to demonstrate the example of converting 
// an Iterable into a Collection by using forEach() of Iterable.

import java.util.*;
import java.io.*;

public class ConvertIterableToCollection {
    // This is a user defined method which is used to 
    // convert Iterable to Collection
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            // Create a blank Collection to hold the result 
            Collection < T > collect = new LinkedList < T > ();

            // By using forEach() to iterate through 
            // the iterable to add each element
            iterable.forEach(collect::add);

            return collect;
        }

    public static void main(String[] args) {
        Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
        System.out.println("The values of Iterable list are : " + itr);

        Collection < Double > coll = convertCollectionFromIterable(itr);
        System.out.println("The values of Collection list are : " + coll);
    }
}

Output

E:\Programs>javac ConvertIterableToCollection.java

E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0] 

iii) By using Iterator

// Java program to demonstrate the example of 
// converting an Iterable into a Collection by using Iterator.

import java.util.*;
import java.io.*;

public class ConvertIterableToCollection {
    // This is a user defined method which is used to 
    // convert Iterable to Collection
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            // Create a blank Collection to hold the result 
            Collection < T > collect = new LinkedList < T > ();

            // By using Iterator to get the Iterator 
            Iterator < T > iterate = iterable.iterator();

            // By using Iterator to iterate through the iterable 
            // to add each element into the Collection
            while (iterate.hasNext())
                collect.add(iterate.next());

            return collect;
        }

    public static void main(String[] args) {
        Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
        System.out.println("The values of Iterable list are : " + itr);

        Collection < Double > coll = convertCollectionFromIterable(itr);
        System.out.println("The values of Collection list are : " + coll);
    }
}

Output

E:\Programs>javac ConvertIterableToCollection.java

E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0] 

2) With the help of stream with collect() method in Java 8

In this method Iterable first convert into spliterator then after with the help of StreamSupport.stream() the spliterator can be traversed and then collected with the help of collect() into Collection.

// Java program to demonstrate the example of stream() 
// with collect() to convert an Iterable into Collection

import java.util.*;
import java.io.*;
import java.util.stream.*;

public class ConvertIterableToCollection {
    // This is a user defined method which is used 
    // to convert Iterable to Collection
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            // Create a blank Collection to hold the result 
            Collection < T > collect = new LinkedList < T > ();
            return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
        }

    public static void main(String[] args) {
        Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
        System.out.println("The values of Iterable list are : " + itr);

        Collection < Double > coll = convertCollectionFromIterable(itr);
        System.out.println("The values of Collection list are : " + coll);
    }
}

Output

E:\Programs>javac ConvertIterableToCollection.java

E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0] 


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.