App下載
首頁javastream_convertJava Stream - 如何將Stream轉(zhuǎn)換為Set

Java Stream - 如何將Stream轉(zhuǎn)換為Set

我們想知道如何將Stream轉(zhuǎn)換為Set。
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
  public static void main(String[] args) {
    // collect as typed array
    Stream<String> words = Stream.of("All", "men", "are", "created", "equal");
    Set<String> wordsSet = words.collect(Collectors.toSet());
    wordsSet.forEach(n -> System.out.println(n));
  }
}