实体类
package com.wgjn.modules.cms.pojo;
import java.util.Date;
public class Person {
private String userName;
private Integer age;
/* private Date createTime;*/
public Person() {
}
public Person(String name, int age) {
this.userName = name;
this.age = age;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person(String userName, Integer age, Date createTime) {
this.userName = userName;
this.age = age;
}
}
根据单个字段去重hashset方法
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三",12));
list.add(new Person("张三",13));
list.add(new Person("李四", 14));
List<Person> list1= new ArrayList<Person>();
Set<String> set=new HashSet<String>();
for (Person person : list) {
if (person == null) {
continue;
}
String userName = person.getUserName();
if (userName != null) {
if (!set.contains(userName)) { //set中不包含重复的
set.add(userName);
list1.add(person);
System.out.println(list1.size());
} else {
continue;
}
}
}
set.clear();
for (int i = 0; i <list1.size() ; i++) {
System.out.println(list1.get(i).getUserName());
}
}
单个字段去重hashmap方法
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三",12));
list.add(new Person("张三",13));
list.add(new Person("李四", 14));
List<Person> list1= new ArrayList<Person>();
HashMap<String, String> hashMap = new HashMap<String, String>();
for (Person person : list) {
if (person == null) {
continue;
}
String userName = person.getUserName();
if (userName != null) {
// String listIdStr = listId.longValue() + "";
String value = hashMap.get(userName);
if (StringUtils.isBlank(value)) { //如果value是空的 说明取到的这个userName是第一次取到
hashMap.put(userName, userName);
list1.add(person); //list1就是我们想要的去重之后的结果
} else {
continue;
}
}
}
hashMap.clear();//结束了记得将hashMap请一个,内存能够得到及时的释放
for (int i = 0; i <list1.size() ; i++) {
System.out.println(list1.get(i).getUserName());
}
}
多个字段去重hashmap方法
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三", 12));
list.add(new Person("张三", 13));
list.add(new Person("李四", 14));
List<Person> list1 = new ArrayList<Person>();
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
for (Person person : list) {
if (person == null) {
continue;
}
String userName = person.getUserName();
int userAge = person.getAge();
if (userName != null) {
if (hashMap.get(userName) == null) {
hashMap.put(userName, userAge);
list1.add(person); //list1就是我们想要的去重之后的结果
} else if (userAge == hashMap.get(userName)) {
continue;
} else {
hashMap.put(userName, userAge);
list1.add(person); //list1就是我们想要的去重之后的结果
}
}
}
hashMap.clear();//结束了记得将hashMap请一个,内存能够得到及时的释放
for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i).getUserName());
}
}
单个字段去重TreeSet方法
package com.wgjn.modules.cms.controller;
import com.wgjn.modules.cms.pojo.Person;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
public class ceshi {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三", 12));
list.add(new Person("张三", 13));
list.add(new Person("李四", 14));
List<Person> list1=removeDuplicate(list);
for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i).getUserName());
}
}
public static List removeDuplicate(List list) {
// 按照订单编号去重
list = (List) list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(Person::getUserName))), ArrayList::new));
return list;
}
}
多个属性去重Treeset方法
package com.wgjn.modules.cms.controller;
import com.wgjn.modules.cms.pojo.Person;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
public class ceshi {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
list.add(new Person("张三", 12));
list.add(new Person("张三", 13));
list.add(new Person("李四", 14));
/* ArrayList<Person> list1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(o -> o.getAge() + ";" + o.getUserName()))), ArrayList::new));
*/
List<Person> list1=removeDuplicate(list);
for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i).getUserName());
}
}
public static List<Person> removeDuplicate(List<Person> list) {
// 按照订单编号去重
ArrayList<Person> list1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
-> new TreeSet<>(Comparator.comparing(o -> o.getAge() + ";" + o.getUserName()))), ArrayList::new));
return list1;
}
}