在数据库中某些数据是重复的,通过mybatis映射成不同的对象。导致不同的对象包含相同的属性。这也是数据重复的一种。下面是我在处理一个属性或者多个属性相同时采用的方法。当然你可以采用其他的方法。比较蠢的方法是两个for循环。但是对于数据的删除是很容易出现异常的。因此我采用的是map的键值对的方法。不多说,直接上代码,代码不做过多解释,相信大家可以看懂,主要是这个思想,我感觉不错!
在这之前你需要模仿一个数据。建一People类。
public class People {
private String name;
private String phone;
private String user;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
其次新建多个对象,对象的属性是重复的。如下我建立的一样,因人而异。
People p1 = new People();
p1.setName("张三");
p1.setPhone("1102");
p1.setUser("法人代表");
People p2 = new People();
p2.setName("张三");
p2.setPhone("110");
p2.setUser("法人代表");
People p3 = new People();
p3.setName("张四");
p3.setPhone("110");
p3.setUser("法人代表");
People p4 = new People();
p4.setName("张三");
p4.setPhone("1120");
p4.setUser("法人代表");
People p5 = new People();
p5.setName("张一");
p5.setPhone("110");
p5.setUser("法人代表");
List<People> peopleList = new ArrayList<>();
peopleList.add(p1);
peopleList.add(p2);
peopleList.add(p3);
peopleList.add(p4);
peopleList.add(p5);
这样的话就已经仿造了一包含重复数据不同对象的List集合了。
一。过滤单个属性重复的对象
List<People> returnList = new ArrayList<>();
Map<String, People> map = new HashMap<>();
for (People people : peopleList) {
System.out.println("people.getName() = " + people.getName() );
String key = people.getName() ;
if (map.containsKey(key)) {
continue;
}
map.put(key, people);
}
Iterator<Map.Entry<String, People>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, People> entry = it.next();
returnList.add(entry.getValue());
}
System.out.println("返回的不重复集合大小为" + returnList.size());
for (People E : returnList
) {
System.out.println("E.getName() = " + E.getName());
}
}
对应的key值为用户名,利用map中键的唯一性特点。
二。过滤多个属性重复的对象。
List<People> returnList = new ArrayList<>();
Map<String, People> map = new HashMap<>();
for (People people : peopleList) {
System.out.println("people.getName() = " + people.getName() + people.getPhone());
String key = people.getName() + people.getPhone();
if (map.containsKey(key)) {
continue;
}
map.put(key, people);
}
Iterator<Map.Entry<String, People>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, People> entry = it.next();
returnList.add(entry.getValue());
}
System.out.println("返回的不重复集合大小为" + returnList.size());
for (People E : returnList
) {
System.out.println("E.getName() = " + E.getName());
}
}
差别就在于key值不同。
个人感觉这中方法去重的话,避免了删除的步骤,更多的是采用put。当然这只是针对单一线程的。
三。List集合根据对象的属性进行排序
public void testR() {
List<Student> list = new ArrayList<>();
//创建3个学生对象,年龄分别是20、19、21,并将他们依次放入List中
Student s1 = new Student();
s1.setAge(20);
Student s2 = new Student();
s2.setAge(19);
Student s3 = new Student();
s3.setAge(21);
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("排序前:" + list);
Collections.sort(list, new Comparator<Student>() {
/*
* int compare(Student o1, Student o2) 返回一个基本类型的整型,
* 返回负数表示:o1 小于o2,
* 返回0 表示:o1和o2相等,
* 返回正数表示:o1大于o2。
*/
public int compare(Student o1, Student o2) {
//按照学生的年龄进行升序排列
if (o1.getAge() > o2.getAge()) {
return 1;
}
if (o1.getAge() == o2.getAge()) {
return 0;
}
return -1;
}
});
System.out.println("排序后:" + list);
}