组合成新的List"/>
将List中相同属性的元素提取出来,组合成新的List
class Bean{private String name;private String course;private Integer score;//constructor//getter//setter
}public static void main(String[] args) {List<Bean> list = new ArrayList<>();list.add(new Bean("张三","数学",100));list.add(new Bean("张三","语文",100));list.add(new Bean("李四","数学",59));list.add(new Bean("李四","语文",59));Set<String> nameSet= new HashSet<>();for (Bean item : list) {nameSet.add(item.getName());}List<Bean> tempList;for (String name: nameSet) {tempList = new ArrayList<>();for (Bean item : list) {if (name.equals(item.getName())) {tempList.add(item);}}}}
更多推荐
将List中相同属性的元素提取出来,组合成新的List
发布评论