如何在Java中将Iterable转换为Collection?

多种方法可以用Java编程语言将Iterable转换为Collection

  1. 在创建实用程序功能的帮助下

    1. 通过使用for循环

    2. 通过使用forEach()可迭代的方法

    3. 通过使用迭代器

  2. 借助Java 8中的stream 的collect()方法

    • 借助实用程序功能
      在此方法中,我们将显式更改或将Iterable转换为Collection(即,我们将手动获取对象中的每个元素)。

i)通过使用for循环

//Java程序演示的例子 
//将Iterable转换为Collection 

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

public class ConvertIterableToCollection {
    //这是用户定义的方法,用于 
    //将Iterable转换为Collection-
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            //创建一个空白集合以保存结果 
            Collection < T > collect = new LinkedList < T > ();

            //通过使用for循环遍历 
            //重复添加每个元素
            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);
    }
}

输出结果

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)使用

Iterable的forEach()

此方法在Java 8或更高版本中可用,因此它支持java8或更高版本。

//Java程序演示的例子 converting 
//通过使用forEach()Iterable将Iterable转换为Collection 。

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

public class ConvertIterableToCollection {
    //这是用户定义的方法,用于 
    //将Iterable转换为Collection-
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            //创建一个空白集合以保存结果 
            Collection < T > collect = new LinkedList < T > ();

            //通过使用forEach()通过迭代 
            //可迭代地添加每个元素
            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);
    }
}

输出结果

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)使用迭代器

//Java程序演示的例子 
//使用Iterator将Iterable转换为Collection。

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

public class ConvertIterableToCollection {
    //这是用户定义的方法,用于 
    //将Iterable转换为Collection-
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            //创建一个空白集合以保存结果 
            Collection < T > collect = new LinkedList < T > ();

            //通过使用Iterator获取Iterator- 
            Iterator < T > iterate = iterable.iterator();

            //通过使用Iterator遍历可迭代 
            //将每个元素添加到集合中
            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);
    }
}

输出结果

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)在Java 8中借助带有collect()方法的流

在此方法中,首先将Iterable转换为拆分器,然后在StreamSupport.stream()的帮助下遍历拆分器,然后借助collect()将其收集到Collection中。

//Java程序演示的例子 stream() //与collect()将Iterable转换为Collection-

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

public class ConvertIterableToCollection {
    //这是用户定义的方法,用于 
    //将Iterable转换为Collection-
    public static < T > Collection < T >
        convertCollectionFromIterable(Iterable < T > iterable) {
            //创建一个空白集合以保存结果 
            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);
    }
}

输出结果

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]