近期对java8新特性操作list和map集合总结如下,废话不多说,直接上代码
public class Main {
public static void main(String[] args) {
System.out.println(“List集合===========欢迎来到list家族!”);
List list = new ArrayList<>();
list.add("c");
list.add("a");
list.add("b");
list.add("d");
List<User> userList = new ArrayList<>();
userList.add(new User(new Long(1),"李世民","123lsm@163.com","男","110"));
userList.add(new User(new Long(2),"宇文成都","123ywcd@163.com","男","111"));
userList.add(new User(new Long(3),"裴元庆","123pyq@163.com","男","112"));
userList.add(new User(new Long(4),"程咬金","123cyj@163.com","男","113"));
userList.add(new User(new Long(5),"李元霸","123lyb@163.com","男","114"));
userList.add(new User(new Long(6),"潇美娘","123xmn@163.com","女","11q"));
userList.add(new User(new Long(6),"潇美娘","123xmn@163.com","女","11q"));
for (int i = 0; i < list.size(); i++) {
System.out.println("第一种遍历list方法" + list.get(i));
}
for (String s : list) {
System.out.println("第二种遍历list方法:" + s);
}
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println("第三种遍历list方法:" + iterator.next());
}
list.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.printf("第四种遍历list方法:" + s + "\n");
}
});
List<User> userList1 = new ArrayList<>();
User user1 = new User();
userList.forEach(user -> {
user1.setId(user.getId());
user1.setUserName(user.getUserName());
user1.setSex(user.getSex());
user1.setPhone(user.getPhone());
user1.seteMail(user.geteMail());
userList1.add(user1);
System.out.println("第五种遍历方法########:"+userList1);
});
List<User> mm = userList.stream().filter(user -> user.getSex().equals("男")).collect(Collectors.toList());
System.out.println("第六种:过滤掉不想要的列表,留下想要的列表########:");
mm.forEach(System.out::println);
List<String> gg = userList.stream().map(User::getSex).distinct().collect(Collectors.toList());
System.out.println("=========gg对某个字段去重!");
gg.forEach(System.out::println);
List<String> g = userList.stream().map(user -> user.getUserName()).distinct().collect(Collectors.toList());
System.out.println("=========g对某个字段去重!");
g.forEach(System.out::println);
List<User> users = userList.stream().distinct().collect(Collectors.toList());
System.out.println("去除完全重复的数据!");
users.forEach(System.out::println);
ArrayList<User> arrayList = userList.stream().collect(Collectors.collectingAndThen(Collectors
.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getSex()))), ArrayList::new));
System.out.println("arrayList根据指定的字段去除重复的数据");
arrayList.forEach(System.out::println);
List<Map<String,String>> mapList = new ArrayList<>();
Map<String,String> mapL = new HashMap<>();
Map<String,String> mapL2 = new HashMap<>();
mapL.put("a","李世民");
mapL.put("b","李元霸");
mapL.put("c","秦叔宝");
mapL2.put("a","李世民");
mapList.add(mapL);
mapList.add(mapL2);
ArrayList<Map<String, String>> mapArrayList = mapList.stream().collect(Collectors.collectingAndThen(Collectors
.toCollection(() -> new TreeSet<>(Comparator.comparing(map -> map.get("a")))), ArrayList::new));
System.out.println("根据list集合中某个对象里的字段值去重:"+mapArrayList);
/*
* findAny和findFirst都是获取第一条信息,如果未找到则返回null
* */
User orElse = userList.stream().filter(user -> user.getUserName().equals("潇美娘")).findAny().orElse(null);
System.out.println("获取存在潇美娘的第一条用户信息orElse:"+orElse);
User anElse = userList.stream().filter(user -> user.getUserName().equals("罗成")).findFirst().orElse(null);
System.out.println("获取不存在罗成的第一条用户信息anElse:"+anElse);
System.out.println("下面介绍下map=========欢迎来到map家族!");
Map<String, Integer> map = new HashMap<>();
map.put("e", 2);
map.put("h", 1);
map.put("g", 9);
map.put("f", 6);
for (Map.Entry<String, Integer> m : map.entrySet()) {
System.out.println("第一种map集合遍历:" + "KEY:" + m.getKey() + "\t" + "VALUE:" + m.getValue());
}
for (String key : map.keySet()) {
System.out.println("第二种map集合遍历:" + "KEY:" + key + "\t" + "VALUE:" + map.get(key));
}
Iterator<Map.Entry<String, Integer>> iterator1 = map.entrySet().iterator();
while (iterator1.hasNext()) {
Map.Entry<String, Integer> entry = iterator1.next();
System.out.println("第三种map集合遍历:" + "KEY:" + entry.getKey() + "\t" + "VALUE:" + entry.getValue());
}
map.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String s, Integer s2) {
System.out.println("第四种map集合遍历:" + "KEY:" + s + "\t" + "VALUE:" + s2);
}
});
map.entrySet().forEach(entry -> System.out.println("第五种map集合遍历:" + "KEY:" + entry.getKey() + "\t" + "VALUE:" + entry.getValue()));
map.values().forEach(s -> {
System.out.println("直接获取map中的所有VALUE" + s);
});
map.forEach((k, v) -> {
System.out.println("第六种最好的方法map集合遍历:" + "KEY:" + k + "\t" + "VALUE:" + v);
});
HashMap<String,Integer> maps = map.entrySet().stream().sorted((o1, o2) -> o1.getValue().compareTo(o2.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o1, LinkedHashMap::new));
maps.forEach((k,v)->System.out.println("对maps集合中value数据升序排序:"+"k:"+k+"\t"+"v:"+v));
HashMap<String,Integer> mapsS = map.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o2, LinkedHashMap::new));
mapsS.forEach((k,v)->System.out.println("对mapsS集合中value数据降序排序:"+"k:"+k+"\t"+"v:"+v));
HashMap<String,Integer> map0 = map.entrySet().stream().sorted((Map.Entry.comparingByValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o1, LinkedHashMap::new));
map0.forEach((k,v)->System.out.println("对map0集合中value数据升序排序:" + "k:"+k+"\t"+"v:"+v));
HashMap<String,Integer> map1 = map.entrySet().stream().sorted(Map.Entry.<String,Integer>comparingByKey().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o1, LinkedHashMap::new));
map1.forEach((k,v)-> System.out.println("对map1集合中key降序排序:" + "k:"+k+"\t"+"v:"+v));
HashMap<String,Integer> map2 = map.entrySet().stream().sorted(Map.Entry.<String,Integer>comparingByValue().reversed())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (o1, o2) -> o1, LinkedHashMap::new));
map2.forEach((k,v)-> System.out.println("对map集合中value降序排序:" + "k:"+k+"\t"+"v:"+v));
}
}
以下运行结果:
List集合===========欢迎来到list家族!
第一种遍历list方法c
第一种遍历list方法a
第一种遍历list方法b
第一种遍历list方法d
第二种遍历list方法:c
第二种遍历list方法:a
第二种遍历list方法:b
第二种遍历list方法:d
第三种遍历list方法:c
第三种遍历list方法:a
第三种遍历list方法:b
第三种遍历list方法:d
第四种遍历list方法:c
第四种遍历list方法:a
第四种遍历list方法:b
第四种遍历list方法:d
第五种遍历方法########:[User{id=1, userName=‘李世民’, eMail=‘123lsm@163.com’, sex=‘男’, phone=‘110’}]
第五种遍历方法########:[User{id=2, userName=‘宇文成都’, eMail=‘123ywcd@163.com’, sex=‘男’, phone=‘111’}, User{id=2, userName=‘宇文成都’, eMail=‘123ywcd@163.com’, sex=‘男’, phone=‘111’}]
第五种遍历方法########:[User{id=3, userName=‘裴元庆’, eMail=‘123pyq@163.com’, sex=‘男’, phone=‘112’}, User{id=3, userName=‘裴元庆’, eMail=‘123pyq@163.com’, sex=‘男’, phone=‘112’}, User{id=3, userName=‘裴元庆’, eMail=‘123pyq@163.com’, sex=‘男’, phone=‘112’}]
第五种遍历方法########:[User{id=4, userName=‘程咬金’, eMail=‘123cyj@163.com’, sex=‘男’, phone=‘113’}, User{id=4, userName=‘程咬金’, eMail=‘123cyj@163.com’, sex=‘男’, phone=‘113’}, User{id=4, userName=‘程咬金’, eMail=‘123cyj@163.com’, sex=‘男’, phone=‘113’}, User{id=4, userName=‘程咬金’, eMail=‘123cyj@163.com’, sex=‘男’, phone=‘113’}]
第五种遍历方法########:[User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}, User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}, User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}, User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}, User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}]
第五种遍历方法########:[User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}]
第五种遍历方法########:[User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}, User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}]
第六种:过滤掉不想要的列表,留下想要的列表########:
User{id=1, userName=‘李世民’, eMail=‘123lsm@163.com’, sex=‘男’, phone=‘110’}
User{id=2, userName=‘宇文成都’, eMail=‘123ywcd@163.com’, sex=‘男’, phone=‘111’}
User{id=3, userName=‘裴元庆’, eMail=‘123pyq@163.com’, sex=‘男’, phone=‘112’}
User{id=4, userName=‘程咬金’, eMail=‘123cyj@163.com’, sex=‘男’, phone=‘113’}
User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}
=========gg对某个字段去重!
男
女
=g对某个字段去重!
李世民
宇文成都
裴元庆
程咬金
李元霸
潇美娘
去除完全重复的数据!
User{id=1, userName=‘李世民’, eMail=‘123lsm@163.com’, sex=‘男’, phone=‘110’}
User{id=2, userName=‘宇文成都’, eMail=‘123ywcd@163.com’, sex=‘男’, phone=‘111’}
User{id=3, userName=‘裴元庆’, eMail=‘123pyq@163.com’, sex=‘男’, phone=‘112’}
User{id=4, userName=‘程咬金’, eMail=‘123cyj@163.com’, sex=‘男’, phone=‘113’}
User{id=5, userName=‘李元霸’, eMail=‘123lyb@163.com’, sex=‘男’, phone=‘114’}
User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}
arrayList根据指定的字段去除重复的数据
User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}
User{id=1, userName=‘李世民’, eMail=‘123lsm@163.com’, sex=‘男’, phone=‘110’}
根据list集合中某个对象里的字段值去重:[{a=李世民, b=李元霸, c=秦叔宝}]
获取存在潇美娘的第一条用户信息orElse:User{id=6, userName=‘潇美娘’, eMail=‘123xmn@163.com’, sex=‘女’, phone=‘11q’}
获取不存在罗成的第一条用户信息anElse:null
下面介绍下map=欢迎来到map家族!
第一种map集合遍历:KEY:e VALUE:2
第一种map集合遍历:KEY:f VALUE:6
第一种map集合遍历:KEY:g VALUE:9
第一种map集合遍历:KEY:h VALUE:1
第二种map集合遍历:KEY:e VALUE:2
第二种map集合遍历:KEY:f VALUE:6
第二种map集合遍历:KEY:g VALUE:9
第二种map集合遍历:KEY:h VALUE:1
第三种map集合遍历:KEY:e VALUE:2
第三种map集合遍历:KEY:f VALUE:6
第三种map集合遍历:KEY:g VALUE:9
第三种map集合遍历:KEY:h VALUE:1
第四种map集合遍历:KEY:e VALUE:2
第四种map集合遍历:KEY:f VALUE:6
第四种map集合遍历:KEY:g VALUE:9
第四种map集合遍历:KEY:h VALUE:1
第五种map集合遍历:KEY:e VALUE:2
第五种map集合遍历:KEY:f VALUE:6
第五种map集合遍历:KEY:g VALUE:9
第五种map集合遍历:KEY:h VALUE:1
直接获取map中的所有VALUE2
直接获取map中的所有VALUE6
直接获取map中的所有VALUE9
直接获取map中的所有VALUE1
第六种最好的方法map集合遍历:KEY:e VALUE:2
第六种最好的方法map集合遍历:KEY:f VALUE:6
第六种最好的方法map集合遍历:KEY:g VALUE:9
第六种最好的方法map集合遍历:KEY:h VALUE:1
对maps集合中value数据升序排序:k:h v:1
对maps集合中value数据升序排序:k:e v:2
对maps集合中value数据升序排序:k:f v:6
对maps集合中value数据升序排序:k:g v:9
对mapsS集合中value数据降序排序:k:g v:9
对mapsS集合中value数据降序排序:k:f v:6
对mapsS集合中value数据降序排序:k:e v:2
对mapsS集合中value数据降序排序:k:h v:1
对map0集合中value数据升序排序:k:h v:1
对map0集合中value数据升序排序:k:e v:2
对map0集合中value数据升序排序:k:f v:6
对map0集合中value数据升序排序:k:g v:9
对map1集合中key降序排序:k:h v:1
对map1集合中key降序排序:k:g v:9
对map1集合中key降序排序:k:f v:6
对map1集合中key降序排序:k:e v:2
对map集合中value降序排序:k:g v:9
对map集合中value降序排序:k:f v:6
对map集合中value降序排序:k:e v:2
对map集合中value降序排序:k:h v:1