List「对象」 转 Set「属性」

    技术2022-07-11  117

    import com.google.common.collect.Lists; public class TestBase { public static void main(String[] args) { List<BABY> list = new TestBase().getList(); if (CommonUtils.isNotEmpty(list)) { // List<Object>转Set<String> Set<String> set = new HashSet(Lists.transform(list, module -> module.getName())); System.out.println("result:" + set); } } public List<BABY> getList(){ List<BABY> list = new ArrayList(); list.add(new BABY("张三",11,new Date())); list.add(new BABY("李四",12,new Date())); list.add(new BABY("张三",15,new Date())); list.add(new BABY("王五",13,new Date())); list.add(new BABY("赵六",14,new Date())); list.add(new BABY("张三",15,new Date())); return list; } class BABY{ private String name; private Integer age; private Date birthday; public BABY(String name,Integer age,Date birthday){ this.name = name; this.age = age; this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } } }

    Processed: 0.014, SQL: 9