description: Java-8 语法。
记录 jdk8 的一些新语法,平时参考使用。
Java-8-Tutorials
1. forEach
1.1 map
//1.7
Map<String, Object> map = new HashMap<>();
map.put("A","map-A");
map.put("B","map-B");
map.put("C","map-C");
map.put("D","map-D");
for (Entry<String, Object> entry : map.entrySet()) {
System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
}
//1.8
map.forEach((k,v)->{
System.out.println("key:"+k+" value:"+v);
});
map.forEach((k,v)->{
if(k.equals("A")){
System.out.println(v);
};
});
1.2 list
//1.7
List<Integer> list = Arrays.asList(1,2,3,4);
for (Integer s : list) {
System.out.println(s);
}
//1.8
list.forEach(s->System.out.println(s));
list.forEach(s->{
if(s.equals("A")){
System.out.println(s);
}
});
list.stream().
filter(s->s.compareTo(2)>0).
filter(s->s.compareTo(4)<0).
//forEach(s->System.out.println(s));
forEach(System.out::println);
User user = new User("fw","1@qq.com");
User user2 = new User("fw2","2@qq.com");
List<User> userList = Arrays.asList(user,user2);
List<String> collect = userList.stream()
.map(u->u.getName())
.collect(Collectors.toList());
collect.forEach(n->{System.out.println(n);});
private static class User{
public User(String name, String email) {
super();
this.name = name;
Email = email;
}
String name;
String Email;
public String getName() {
return name;
}
public String getEmail() {
return Email;
}
}
2. Comparator
List<User> userList = Arrays.asList(new User("scott", 18),new User("andy", 20));
//1.7只能使用Collections.sort
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getAge()-o2.getAge();
}
});
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getName().compareTo(o2.getName());
}
});
//1.8可以直接用list的sort方法
Comparator<User> c = (o1,o2)->o2.getAge()-o1.getAge();
userList.sort(c);
userList.forEach(u->System.out.println(u));
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于