import java.util.ArrayList;import java.util.List;import java.util.UUID;import java.util.concurrent.TimeUnit;public class StreamTest9 { public static void main(String[] args) { Listlist = new ArrayList<>(5000000); for(int i = 0; i < 5000000; ++i) { list.add(UUID.randomUUID().toString()); } System.out.println("开始排序"); long startTime = System.nanoTime(); // 为何提示sorted方法是不必要的? list.parallelStream().sorted().count(); long endTime = System.nanoTime(); long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime); System.out.println("排序耗时:" + millis); }}
import java.util.Arrays;import java.util.List;public class StreamTest10 { public static void main(String[] args) { Listlist = Arrays.asList("hello1", "world", "hello world");// list.stream().mapToInt(item -> item.length()).filter(length -> length == 5).// findFirst().ifPresent(System.out::println); list.stream().mapToInt(item -> { int length = item.length(); System.out.println(item); return length; }).filter(length -> length == 5).findFirst().ifPresent(System.out::println); }}
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class StreamTest11 { public static void main(String[] args) { Listlist = Arrays.asList("hello welcome", "world hello", "hello world hello", "hello welcome");// List result = list.stream().map(item -> item.split(" ")).distinct().// collect(Collectors.toList());// result.forEach(item -> Arrays.asList(item).forEach(System.out::println)); List result = list.stream().map(item -> item.split(" ")).flatMap(Arrays::stream).distinct(). collect(Collectors.toList()); result.forEach(System.out::println); }}
import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class StreamTest12 { public static void main(String[] args) { Listlist1 = Arrays.asList("Hi", "Hello", "你好"); List list2 = Arrays.asList("zhangsan", "lisi", "wangwu", "zhaoliu"); List result = list1.stream().flatMap(item -> list2.stream().map(item2 -> item + " " + item2)). collect(Collectors.toList()); result.forEach(System.out::println); }}
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class StreamTest13 { public static void main(String[] args) { Student student1 = new Student("zhangsan", 100, 20); Student student2 = new Student("lisi", 90, 20); Student student3 = new Student("wangwu", 90, 30); Student student4 = new Student("zhangsan", 80, 40); Liststudents = Arrays.asList(student1, student2, student3, student4);// Map > map = students.stream().// collect(Collectors.groupingBy(Student::getName));// Map > map = students.stream().// collect(Collectors.groupingBy(Student::getScore));// Map map = students.stream().// collect(Collectors.groupingBy(Student::getName, Collectors.counting()));// Map map = students.stream().// collect(Collectors.groupingBy(Student::getName, Collectors.averagingDouble(Student::getScore)));// Map > map = students.stream().// collect(Collectors.partitioningBy(student -> student.getScore() >= 90));// System.out.println(map); }}
public class Student { private String name; private int score; private int age; public Student(String name, int score, int age) { this.name = name; this.score = score; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}