List<Player> players = new ArrayList<Player>() {
{
add(new Player("Elsdon", "2017-12-01 10:20:21"));
add(new Player("Tamsen", "2017-12-01 10:20:22"));
add(new Player("Floyd", "2017-12-01 10:20:23"));
add(new Player("Sindy", "2017-12-01 10:20:24"));
add(new Player("Vere", "2017-12-01 10:20:25"));
add(new Player("Maude", "2017-12-02 10:20:26"));
add(new Player("Shawn", "2017-12-01 10:20:27"));
add(new Player("Jayden", "2017-12-01 10:20:28"));
add(new Player("Palmer", "2017-12-01 10:20:29"));
add(new Player("Addison", "2017-12-01 10:20:30"));
}
};
//构建一个List集合
//使用lambda表达式对List进行循环
players.forEach(player -> {
System.out.printf("%s %s; ", player.getName(), player.getAddress());
});
//使用lambda排序
//排序方法 比较player的address字段 如果 a在前面则是升序 b 在前面则//是降序(前后表示红色字母的位置)
Collections.sort(players, (a, b) -> b.getAddress().compareTo(a.getAddress()));
//过滤名字叫Addison的数据,并且按照address的降序排列,并生成一个新的//List集合
List<Player> playerss = players.stream().sorted(((a, b) -> b.getAddress().compareTo(a.getAddress())))
.filter(a-> !"Addison".equals(a.getName())).collect(Collectors.toList());
playerss.forEach(System.out::println);
//对整型的list进行操作
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("List中最大的数字 : " + stats.getMax());
System.out.println("List中最小的数字 : " + stats.getMin());
System.out.println("所有数字的总和 : " + stats.getSum());
System.out.println("所有数字的平均值 : " + stats.getAverage());
//字符串连接收集器
采用函数式编程(使用 Collectors.joining 收集器
)的方式对字符串进行更优雅的连接。Collectors.joining 收集器
支持灵活的参数配置,可以指定字符串连接时的 分隔符,前缀 和 后缀 字符串。
final String[] names = {"1", "2", "3", "4", "5"};
Stream<String> stream1 = Stream.of(names);
Stream<String> stream2 = Stream.of(names);
Stream<String> stream3 = Stream.of(names);
// 拼接成 [x, y, z] 形式
String result1 = stream1.collect(Collectors.joining(", ", "[", "]"));
// 拼接成 x | y | z 形式
String result2 = stream2.collect(Collectors.joining(" | ", "", ""));
// 拼接成 x -> y -> z] 形式
String result3 = stream3.collect(Collectors.joining(" -> ", "", ""));
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
结果如下:
[1, 2, 3, 4, 5]
1| 2| 3| 4| 5
1-> 2-> 3-> 4 -> 5
//List 转map
List<Player> players = new ArrayList<Player>() {
{
add(new Player(1,"Elsdon");
add(new Player(2,"Tamsen");
add(new Player(3,"Floyd");
add(new Player(4,"Sindy");
add(new Player(5,"Vere");
add(new Player(6,"Maude");
add(new Player(7,"Shawn");
add(new Player(8,"Jayden");
add(new Player(9,"Palmer");
add(new Player(10,"Addison");
}
};
Map<Integer, String> result1 = players .stream().collect(Collectors.toMap(Player::getId, Player::getName));
List<Player> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
.map(e -> new Player(e.getKey(), e.getValue())).collect(Collectors.toList());
List<Player> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
.map(e -> new Player(e.getKey(), e.getValue())).collect(Collectors.toList());
//寻找id=1的对象 从List中过滤出一个元素
User match = users.stream().filter((user) -> user.getId() == 1).findAny().get();
List<Employee> employees = persons.stream()
.filter(p -> p.getLastName().equals("l1"))
.map(p -> new Employee(p.getName(), p.getLastName(), 1000))
.collect(Collectors.toList());
//去重
List<LocationDTO> distinctResult = result.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(o -> o.getLocationId()))), ArrayList::new));