1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| @Test public void test(){ List<Employee> list = new ArrayList<>(); list.add(new Employee(1, "小明", 14, 5000.00)); list.add(new Employee(2, "小红", 12, 4900.00)); list.add(new Employee(3, "小白", 15, 5200.00));
Stream<Employee> stream = list.stream(); stream.filter(e -> e.getSalary() > 7000).forEach(str -> System.out.println(str));
list.stream().limit(3).forEach(System.out::println); System.out.println();
list.stream().skip(3).forEach(System.out::println); System.out.println();
list.add(new Employee(1,"abc",40,8000)); list.add(new Employee(2,"abc",41,7000)); list.add(new Employee(3,"abc",40,9000)); list.stream().distinct().forEach(System.out::println);
List<String> lists = Arrays.asList("aa", "bb", "cc", "dd"); lists.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
Stream<Character> characterStream = lists.stream().flatMap(str -> { ArrayList<Character> list2 = new ArrayList<>(); for(Character c : str.toCharArray()){ list2.add(c); } return list2.stream(); }); characterStream.forEach(System.out::println); System.out.println(); Stream<Stream<Character>> streamStream = lists.stream().map(str -> { ArrayList<Character> list3 = new ArrayList<>(); for(Character c : str.toCharArray()){ list3.add(c); } return list3.stream(); }); streamStream.forEach(s ->{ s.forEach(System.out::println); });
List<Integer> list4 = Arrays.asList(12, 43, 65, 34, 87, 0, -98, 7); list4.stream().sorted().forEach(System.out::println);
List<Employee> employees = new ArrayList<>(); employees.add(new Employee(1, "小明", 14, 5000.00)); employees.add(new Employee(2, "小红", 12, 4900.00)); employees.add(new Employee(3, "小白", 15, 5200.00)); employees.stream().sorted( (e1,e2) -> { int ageValue = Integer.compare(e1.getAge(),e2.getAge()); if(ageValue != 0){ return ageValue; }else{ return -Double.compare(e1.getSalary(),e2.getSalary()); } }).forEach(System.out::println); }
|