自定义传参
List<PersonMo> list = new ArrayList<>(Arrays.asList(new PersonMo("ttt","man",11,null) ,new PersonMo("AAA","woman",111),new PersonMo("BBB","woman",222)));Map<Integer,PersonMo> map = new HashMap<>(); map.put(1,new PersonMo("ttt","man",11,null)); map.put(2,new PersonMo("AAA","woman",111)); map.put(3,new PersonMo("BBB","woman",222));
EntrySet遍历Map
public Map<Integer, PersonMo> EntrySetGetPerson(Map<Integer, PersonMo> personMoMap) { for (Map.Entry<Integer,PersonMo> person: personMoMap.entrySet()){ if (person.getKey() == 1){ person.setValue(new PersonMo(ReturnMsgConstant.TTT,"superMan",999)); } } return personMoMap; }
Foreach遍历List
public List<PersonMo> ForEachGetPerson(List<PersonMo> person) { person.forEach(e -> { if ("ttt".equals(e.getName())){ e.setName(ReturnMsgConstant.TTT); } }); return person; }
Stream流遍历List
public PersonMo StreamGetPersonList(List<PersonMo> person) { PersonMo personMo = new PersonMo(); person.stream().forEach(e -> { if ("AAssA".equals(e.getName())){ e.setSex("BigFly"); BeanUtils.copyProperties(e,personMo); } }); return personMo.getName() != null && !personMo.getName().trim().isEmpty() ? personMo : new PersonMo(ReturnMsgConstant.AAA,"Man",18); // return personMo.getName() != null ? personMo : new PersonMo(ReturnMsgConstant.AAA,"Man",18); }
Stream流遍历Map,返回值分别用Map和List两种方式接
public PersonMo StreamGetPersonMap(Map<Integer, PersonMo> map) { PersonMo person; // List<PersonMo> personMap = map.entrySet().stream() // .filter( e -> "BBB".equals(e.getValue().getName())) // .map(Map.Entry::getValue) // .collect(Collectors.toList()); // return personMap.isEmpty() ? new PersonMo(ReturnMsgConstant.AAA,"Man",18) : personMap.get(0); Map<Integer,PersonMo> personMap = map.entrySet().stream() .filter( e -> "BBB".equals(e.getValue().getName())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); for (Map.Entry<Integer,PersonMo> personMo : personMap.entrySet()){ return personMo.getValue(); } return new PersonMo(ReturnMsgConstant.AAA,"Man",18); }
Gitee地址 : Thwyl/study (gitee.com)
postMan先执行 http://localhost:8008/study/entrySet 方法,把map添加进来,才可实现之后的方法