Java 8 中的 Collectors toSet() 方法
Collections reverse order 类是一个反向排序方法,编码在 Collections 类中。它位于 java.util 包中。它返回一个比较器,本质上是一个预定义的比较器。通过使用这个比较器包,我们可以以反向方式重新排列特定数据集的集合。Collectors toSet() 是一个收集器类,它本身返回一个收集器,并将这些特定元素作为输入累积到一个新的集合中。toSet() 是一个无序的收集器类,它不负责保留某些输入元素的遇到顺序。要使用此函数,我们需要记住,对这些数据的类型、可变性和可序列化性没有任何保证。并且 Set 返回值没有线程安全性。这是一个例子:
int[] array = { 1, 2, 3, 4, 5 };
Set<Integer> set = Stream.of(array)
.flatMapToInt(Arrays::stream).boxed().collect(Collectors.toSet());
在 Java 中执行 Collections toSet 函数的算法
在这个可能的算法中,我们将解释如何声明和构造一个 toSet 函数,通过该函数我们可以将所有数据绑定到特定数据集中。
步骤 1 - 开始流程
步骤 2 - 导入和声明可用的包以运行流程。
步骤 3 - 声明一个公共类。
步骤 4 - 提及参数字符串。
步骤 5 - 创建一个流值节点。
步骤 6 - 使用一些数据填充此流。
步骤 7 - 使用 toSet() 声明一个集合函数。
步骤 8 - 覆盖数据集。
步骤 9 - 选择流 1 和流 2。
步骤 10 - 对数据集进行扁平化操作。
步骤 11 - 返回值。
步骤 12 - 终止流程。
在 Java 中执行 Collections toSet 函数的语法
Set<Integer> set = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toSet());{
Person p1 = new Person("P1");
p1.add(1);
p1.add(5);
p1.add(10);
Person p2 = new Person("P2");
Collectors toSet() in Java with Examples
p2.add(10);
p2.add(20);
p2.add(50);
p2.add(5);
List team = new ArrayList<>();
team.add(p1);
team.add(p2);
Set setOfDistinctNote = team.stream()
.map( p -> p.getNotes())
.flatMap(l -> l.stream())
.collect(Collectors.toSet());
System.out.println("Number of distinct notes in team : " +
setOfDistinctNote);
}
class Person{
private String name;
private List notes = new ArrayList<>();
public Person(String name){
this.name = name;
}
public void add(Integer note){ notes.add(note);
}
public List getNotes(){
return notes;
}
@Override public String toString(){
return String.format(name + notes);
}
}
在这个可能的语法中,我们尝试使用一些 Java 逻辑来构建一个通用的逻辑代码。使用此语法,您可以体验 collectors toSet() 函数的工作流程。现在,我们将转向一些可能的方法,通过这些方法,我们将学习如何有效地解决问题陈述。
遵循的方法
方法 1 - 使用流类方法展示 Collectors toSet() 函数实现的 Java 代码
方法 2 - Java 程序演示如何使用 Collectors 类将集合或值流转换为 Set
方法 1:演示 Collections.sort() 的工作原理
在这些可能的方法中,我们将应用收集器类中存在的 T、A、R 理论。这里:
T - 是我们在此过程中将使用的输入元素类型。
接口收集器类 - 这是一个可变操作类,有助于将元素放入容器类中。
T - 带有归约的输入元素类型。
A - 带有归约的可变累积。
R - 带有归约的结果类型。
Set - 一些数据元素的集合,没有重复。它也不包含任何对以及空元素。
示例 1
//Java code to show the implementation of Collectors toSet() function by using the stream class method
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ARBRDD {
public static void main(String[] args){
Stream<String> s = Stream.of("BANGLADESH",
"INDIA",
"TRAIN",
"MAITREE EXPRESS");
Set<String> mySet = s.collect(Collectors.toSet());
System.out.println(mySet);
}
}
输出
[MAITREE EXPRESS, BANGLADESH, TRAIN, INDIA]
在下面提到的可能方法中,我们将实现 mySet 方法以演示流的 collectors toSet() 函数。
示例 2
//Java code to show the implementation of Collectors toSet() function by using mySet method
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ARBRDD {
public static void main(String[] args){
Stream<String> s = Stream.of("10", "16", "7", "2001", "1997");
Set<String> mySet = s.collect(Collectors.toSet());
System.out.println(mySet);
}
}
输出
[1997, 16, 7, 2001, 10]
方法 2:使用 Collector 类将一个集合转换为另一个集合
在下面提到的这些可能的方法中,我们将尝试使用 Collector.toSet()。在此过程中,我们不能放入任何元素的重复副本,也没有顺序保证。
示例 1
//Java program to demonstrate how we can use the Collectors class to convert a set or stream of values into a Set.
package tool;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ARBRDD {
public static void main(String[] args) {
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6, 78, 9, 10, 3, 2, 34, 5,
3);
List<Integer> listOfIntegers = input.stream() .collect(Collectors.toList());
System.out.println("Stream to List: " + listOfIntegers);
ArrayList<Integer> aList = input.stream()
.collect(Collectors.toCollection(ArrayList::new));
System.out.println("Stream to ArrayList: " + aList);
LinkedList<Integer> linkedList = input.stream()
.collect(Collectors.toCollection(LinkedList::new));
System.out.println("Stream to LinkedList: " + linkedList);
Set<Integer> aSet = input.stream().collect(Collectors.toSet());
System.out.println("Stream to Set: " + aSet);
HashSet<Integer> anHashSet = input.stream()
.collect(Collectors.toCollection(HashSet::new));
System.out.println("Stream to HashSet: " + anHashSet);
LinkedHashSet<Integer> aLinkedHashSet = input.stream()
.collect(Collectors.toCollection(LinkedHashSet::new));
System.out.println("Stream to LinkedHashSet: " + aLinkedHashSet);
Map<Integer, String> aMap = input.stream() .collect(Collectors.toMap(
Function.identity(), String::valueOf, (k1, k2) -> k1));
System.out.println("Stream to Map: " + aMap);
HashMap<Integer, String> anHashMap = input.stream() .collect(Collectors.toMap(
Function.identity(), String::valueOf, (k1, k2) -> k1, HashMap::new));
System.out.println("Stream to HashMap: " + anHashMap);
LinkedHashMap<Integer, String> aLinkedHashMap = input.stream()
.collect(Collectors.toMap( Function.identity(), String::valueOf, (k1, k2) ->
k1, LinkedHashMap::new));
System.out.println("Stream to LinkedHashMap: " + aLinkedHashMap);
ConcurrentMap<Integer, String> aConcurrentMap = input.parallelStream()
.collect(Collectors.toConcurrentMap( Function.identity(), String::valueOf, (k1,
k2) -> k1));
System.out.println("Stream to ConcurrentMap: " + aConcurrentMap);
ConcurrentHashMap<Integer, String> aConcurrentHashMap = input.parallelStream()
.collect(Collectors.toConcurrentMap( Function.identity(), String::valueOf, (k1,
k2) -> k1, ConcurrentHashMap::new));
System.out.println("Stream to ConcurrentHashMap: " + aConcurrentHashMap);
}
}
输出
Stream to List: [1, 2, 3, 4, 5, 6, 78, 9, 10, 3, 2, 34, 5, 3]
Stream to ArrayList: [1, 2, 3, 4, 5, 6, 78, 9, 10, 3, 2, 34, 5, 3]
Stream to LinkedList: [1, 2, 3, 4, 5, 6, 78, 9, 10, 3, 2, 34, 5, 3]
Stream to Set: [1, 2, 34, 3, 4, 5, 6, 9, 10, 78]
Stream to HashSet: [1, 2, 34, 3, 4, 5, 6, 9, 10, 78]
Stream to LinkedHashSet: [1, 2, 3, 4, 5, 6, 78, 9, 10, 34]
Stream to Map: {1=1, 34=34, 2=2, 3=3, 4=4, 5=5, 6=6, 9=9, 10=10, 78=78}
Stream to HashMap: {1=1, 34=34, 2=2, 3=3, 4=4, 5=5, 6=6, 9=9, 10=10, 78=78}
Stream to LinkedHashMap: {1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 78=78, 9=9, 10=10,
34=34}
Stream to ConcurrentMap: {1=1, 34=34, 2=2, 3=3, 4=4, 5=5, 6=6, 9=9, 10=10,
78=78}
Stream to ConcurrentHashMap: {1=1, 2=2, 34=34, 3=3, 4=4, 5=5, 6=6, 9=9, 10=10,
78=78}
在这个 Java 代码中,我们将对数据集使用抽象映射类。在此过程中,我们将取一个数组,并在填充此数组集后,我们将使用一些键值对数据进行细化以执行 toSet() 操作。
示例 2
import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ARBRDD{
public static void main(String[] args){
Map<String, String> person =
Stream.of(
new AbstractMap.SimpleEntry<>("Name", "[ABONI]"),
new AbstractMap.SimpleEntry<>("Age", "[22]"),
new AbstractMap.SimpleEntry<>("Sex", "[FEMALE]")
).collect(Collectors.toMap(Map.Entry::getKey,
Map.Entry::getValue));
for (String key: person.keySet()) {
System.out.println(key + " OF THE PERSON IS " + person.get(key));
}
}
}
输出
Sex OF THE PERSON IS [FEMALE] Age OF THE PERSON IS [22] Name OF THE PERSON IS [ABONI]
结论
toSet() 收集器类将输入数据元素累积到一个全新的集合中。在今天的文章中,我们学习了 Java 收集器包中存在的 toSet 类。通过上述算法、语法;我们创建了一些 Java 代码来有效地解决问题陈述。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP