场景一、
List<String> list = new ArrayList<String>(); Map<String,String> map = new HashMap<>(); map.put("id","11"); map.put("name","zhangsan"); map.put("age","30"); //遍历map集合 map.forEach((k,v) -> list.add(v)); //这几种遍历list集合的方法都可以 list.stream().forEach(System.out::println); list.stream().forEach(e-> System.out.println(e)); list.forEach(e-> System.out.println(e));
场景二、
两种方式将集合对象类型转为集合string类型
List<String> ipList = Lists.newArrayList(); List<OpsResourceTreeSelect> ops = this.parentNodeTree(); ops.forEach(obj -> ipList.add(obj.getIp())); 或者 List<OpsResourceTreeSelect> ops = this.parentNodeTree(); List<String> ips = ops.stream().map(OpsResourceTreeSelect::getIp).collect(Collectors.toList());
场景三、
将集合中的对象,过滤指定字段并去重,最后返回一个集合string类型
List<OpsResourceList> ops = opsResourceListMapper.selectOpsResourceListList(null); List<String> ips = ops.stream() .filter(obj -> StringUtils.isNotBlank(obj.getIp())) .map(OpsResourceList::getIp) .distinct() .collect(Collectors.toList());
返回结果
[66.3.125.44, 66.10.113.8, 66.10.113.9, 66.3.125.45, 66.66.66.66, 32.1.52.6, 66.10.113.10, 66.3.125.31, 66.3.125.35, 66.3.125.36]