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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import java.util.*; import java.util.stream.Collectors;
public class StreamDemo { public static void main(String[] args) { StringList list = new StringList(); list .put("") .put("张三") .put("李四") .put("王五") .put("赵六") .put("陈七") .put("秦八") .put("ruben") .put("ruben") .put("007") .put(""); for (String item : list) { System.out.println(item); } list.forEach(System.out::println);
List<User> userList = new ArrayList<>(); User user1 = new User(); user1.setName("狂神"); user1.setAge(23); userList.add(user1); List<Map<String, Object>> userMapList = userList.stream().map( user -> { Map<String, Object> tempMap = new HashMap<>(16); tempMap.put("name", user.getName()); tempMap.put("age", user.getAge()); tempMap.put("isFocus", focusStatus(user)); return tempMap; }).collect(Collectors.toList()); userMapList.forEach(System.out::println);
System.out.println(list.parallelStream().filter(String::isEmpty).count());
list.stream().limit(2).filter(string -> !string.isEmpty()).forEach(System.out::println);
System.out.println(list.stream().distinct().collect(Collectors.joining(" 和 ")));
List<Integer> integerList = new ArrayList<>(); integerList.add(2); integerList.add(1); integerList.stream().sorted().forEach(System.out::println);
Random random = new Random(); IntSummaryStatistics intSummaryStatistics = random.ints().limit(10).summaryStatistics(); System.out.println("平均数" + intSummaryStatistics.getAverage()); System.out.println("最大数" + intSummaryStatistics.getMax()); System.out.println("最小数" + intSummaryStatistics.getMin()); System.out.println("求和" + intSummaryStatistics.getSum()); System.out.println("数量" + intSummaryStatistics.getCount());
}
static Boolean focusStatus(User user) { user = Optional.ofNullable(user).orElse(new User("普通用户", 18)); System.out.println("我关注了" + user.getName()); return true; } }
|