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