java将Map转换为List

  1. 实例
List<BlogComment> blogCommentListResult = new ArrayList<>(blogCommentMap.values());
  1. Map数据转换为自定义对象的List,例如把map的key,value分别对应Person对象两个属性:
List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

List<Person> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
	.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());

以上三种方式不同之处在于排序的处理

猜你喜欢

转载自blog.csdn.net/weixin_43088443/article/details/112971066
今日推荐