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

iterable java

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

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

  1. With the help of creating a utility function

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

    1. By using for loop

      通过使用for循环

    2. By using forEach() method of Iterable

      通过使用Iterable的forEach()方法

    3. By using Iterator

      通过使用迭代器

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

    借助Java 8中collect()方法的流

  • 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).

    在这种方法中,我们将明确地将Iterable更改或转换为Collection(即,我们将手动获取对象中的每个元素)。

i)通过使用循环 (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)使用Iterable的forEach() (ii) By using forEach() of Iterable)

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

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

// 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)使用迭代器 (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)借助Java 8中的collect()方法的流 (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.

在此方法中,Iterable首先转换为拆分器,然后在StreamSupport.stream()的帮助下遍历拆分器,然后借助collect()将其收集到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] 


翻译自: https://www.includehelp.com/java/convert-iterable-to-collection-in-java.aspx

iterable java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值