在Java中将 Iterable 转换为 Collection
Iterable和Collection在Java中非常有用。 Java中的 Collection 框架中使用迭代器来逐个检索元素,而 Collection 是一组表示为单个单元的单个对象。 Java提供了集合框架,它定义了几个类和接口来将一组对象表示为一个单元。
但在某些时候,需要从可迭代切换到集合,反之亦然。有关 Iterable 和 Collection 之间区别的更多详细信息,请参阅Java中的 Iterator vs Collection帖子。
Iterable到Collection的转换可以通过以下方式进行:
- 创建效用函数:创建效用函数意味着创建一个函数,通过显式考虑每个项目来将可迭代对象转换为集合。这也可以通过多种方式完成,如下所述:
- 使用 For 循环
Java
// Below is the program to convert an Iterable
// into a Collection using for loop
import java.io.*;
import java.util.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
// Iterate through the iterable to
// add each element into the collection
for (T t : itr)
cltn.add(t);
// Return the converted collection
return cltn;
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection cn = getCollectionFromIterable(i);
System.out.println("Collection List : " + cn);
}
}
Java
// Below is the program to convert an Iterable
// into a Collection using iterable.forEach
import java.io.*;
import java.util.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
// Use iterable.forEach() to
// Iterate through the iterable and
// add each element into the collection
itr.forEach(cltn::add);
// Return the converted collection
return cltn;
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection cn = getCollectionFromIterable(i);
System.out.println("Collection List : " + cn);
}
}
Java
// Below is the program to convert an Iterable
// into a Collection using Iterator
import java.io.*;
import java.util.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
// Get the iterator at the iterable
Iterator iterator = itr.iterator();
// Iterate through the iterable using
// iterator to add each element into the collection
while (iterator.hasNext()) {
cltn.add(iterator.next());
}
// Return the converted collection
return cltn;
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection cn = getCollectionFromIterable(i);
System.out.println("Collection List : " + cn);
}
}
Java
// Program to convert an Iterable
// into a Collection
import java.io.*;
import java.util.*;
import java.util.stream.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
return StreamSupport.stream(itr.spliterator(), false)
.collect(Collectors.toList());
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection cn = getCollectionFromIterable(i);
System.out.println("Collection List : " + cn);
}
}
输出:
Iterable List : [1, 2, 3, 4]
Collection List : [1, 2, 3, 4]
- 使用 Iterable.forEach() :
它可以在Java 8 及更高版本中使用。
Java
// Below is the program to convert an Iterable
// into a Collection using iterable.forEach
import java.io.*;
import java.util.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
// Use iterable.forEach() to
// Iterate through the iterable and
// add each element into the collection
itr.forEach(cltn::add);
// Return the converted collection
return cltn;
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection cn = getCollectionFromIterable(i);
System.out.println("Collection List : " + cn);
}
}
输出:
Iterable List : [1, 2, 3, 4]
Collection List : [1, 2, 3, 4]
- 使用迭代器:forEach 循环在后台使用迭代器。因此,它可以通过以下方式明确地完成。
Java
// Below is the program to convert an Iterable
// into a Collection using Iterator
import java.io.*;
import java.util.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
// Get the iterator at the iterable
Iterator iterator = itr.iterator();
// Iterate through the iterable using
// iterator to add each element into the collection
while (iterator.hasNext()) {
cltn.add(iterator.next());
}
// Return the converted collection
return cltn;
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection 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()的帮助下,可以遍历拆分器,然后在collect()的帮助下将其收集到集合中。
Java
// Program to convert an Iterable
// into a Collection
import java.io.*;
import java.util.*;
import java.util.stream.*;
class GFG {
// function to convert Iterable into Collection
public static Collection
getCollectionFromIterable(Iterable itr)
{
// Create an empty Collection to hold the result
Collection cltn = new ArrayList();
return StreamSupport.stream(itr.spliterator(), false)
.collect(Collectors.toList());
}
public static void main(String[] args)
{
Iterable i = Arrays.asList(1, 2, 3, 4);
System.out.println("Iterable List : " + i);
Collection cn = getCollectionFromIterable(i);
System.out.println("Collection List : " + cn);
}
}
输出:
Iterable List : [1, 2, 3, 4]
Collection List : [1, 2, 3, 4]