将Java中的Iterable转为Collection

将Java中的Iterable转为Collection

IterableCollection 在Java中非常有用。迭代器用于Java的Collection框架中,以逐个检索元素,而Collection则是一组表示为单个单元的单个对象。Java提供了Collection Framework,它定义了几个类和接口来表示一组对象作为单个单元。

但是在某些情况下,需要从可迭代对象转换为集合,反之亦然。有关Iterable和Collection之间差异的更多详细信息,请参阅文章Iterator vs Collection in Java

可以通过以下方式将Iterable转换为Collection:

  • Creating a utility function :创建实用函数意味着创建一个函数,通过明确考虑每个项目将迭代对象转换为集合。这也可以通过以下方式完成,如下所述:
    • Using For loop
//以下是使用for循环将Iterable转换为Collection的程序
import java.io.*;
import java.util.*;

class GFG {
    //将Iterable转换为Collection的函数
    public static <T> Collection<T>
                               getCollectionFromIterable(Iterable<T> itr)
    {
        //创建一个空的Collection来保存结果
        Collection<T> cltn = new ArrayList<T>();

        //遍历可迭代对象
        for (T t : itr)
            cltn.add(t);

        //返回转换后的Collection
        return cltn;
    }

    public static void main(String[] args)
    {
        Iterable<Integer> i = Arrays.asList(1, 2, 3, 4);
        System.out.println("Iterable List : " + i);

        Collection<Integer> cn = getCollectionFromIterable(i);
        System.out.println("Collection List : " + cn);
    }
}

输出结果:

Iterable列表 : [1, 2, 3, 4]
Collection列表 : [1, 2, 3, 4]
  • Using Iterable.forEach() : 可以在Java 8及以上使用。
//以下是使用Iterable.forEach将Iterable转换为Collection的程序
import java.io.*;
import java.util.*;

class GFG {
    //将Iterable转换为Collection的函数
    public static <T> Collection<T>
                                getCollectionFromIterable(Iterable<T> itr)
    {
        //创建一个空的Collection来保存结果
        Collection<T> cltn = new ArrayList<T>();

        //使用Iterable.forEach()遍历Iterable并将每个元素添加到Collection中
        itr.forEach(cltn::add);

        //返回转换后的Collection
        return cltn;
    }

    public static void main(String[] args)
    {
        Iterable<Integer> i = Arrays.asList(1, 2, 3, 4);
        System.out.println("Iterable List : " + i);

        Collection<Integer> cn = getCollectionFromIterable(i);
        System.out.println("Collection List : " + cn);
    }
}

输出结果:

Iterable列表 : [1, 2, 3, 4]
Collection列表 : [1, 2, 3, 4]
  • Using Iterator :forEach循环在后台使用迭代器。因此,可以以下面的方式显式完成。
// 下面是一个将Iterable转换为Collection的程序,
// 其中使用了Iterator
 
import java.io.*;
import java.util.*;
 
class GFG {
    // 将Iterable转换为Collection的函数
    public static <T> Collection<T>
                    getCollectionFromIterable(Iterable<T> itr)
    {
        // 创建一个空的Collection来保存结果
        Collection<T> cltn = new ArrayList<T>();
 
        // 获取iterable的iterator
        Iterator<T> iterator = itr.iterator();
 
        // 使用iterator遍历iterable,将每个元素添加到collection中
        while (iterator.hasNext()) {
            cltn.add(iterator.next());
        }
 
        // 返回转换后的collection
        return cltn;
    }
 
    public static void main(String[] args)
    {
        Iterable<Integer> i = Arrays.asList(1, 2, 3, 4);
        System.out.println("Iterable List : " + i);
 
        Collection<Integer> cn = getCollectionFromIterable(i);
        System.out.println("Collection List : " + cn);
    }
}

输出:

Iterable List : [1, 2, 3, 4]
Collection List : [1, 2, 3, 4]
  • Java 8 Stream :随着Java 8中Stream的引入,这类工作变得非常容易。要将iterable转换为Collection,首先将 iterable 转换为 spliterator 。然后使用 StreamSupport.stream() 遍历spliterator,然后使用 collect() 收集到collection中。
// 将Iterable转换为Collection的程序
 
import java.io.*;
import java.util.*;
import java.util.stream.*;
 
class GFG {
    // 将Iterable转换为Collection的函数
    public static <T> Collection<T>
                    getCollectionFromIterable(Iterable<T> itr)
    {
        // 创建一个空的Collection来保存结果
        Collection<T> cltn = new ArrayList<T>();
 
        return StreamSupport.stream(itr.spliterator(), false)
            .collect(Collectors.toList());
    }
 
    public static void main(String[] args)
    {
        Iterable<Integer> i = Arrays.asList(1, 2, 3, 4);
        System.out.println("Iterable List : " + i);
 
        Collection<Integer> cn = getCollectionFromIterable(i);
        System.out.println("Collection List : " + cn);
    }
}

输出:

Iterable List : [1, 2, 3, 4]
Collection List : [1, 2, 3, 4]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程