集合中数据以名称为依据转换成Map
问题描述:
现在存在一个集合,集合中存在重复数据,需要以名称唯一,其余数据也进行批量保存,但是其余数据保存的时候,不能对重复数据进行保存,也就是说集合中存在名称唯一,但其余数据存在相同或者不相同的,针对这种情况进行保存?
解决思路:
将上述的集合转换成map,通过key value形式进行保存,这样的话名称就保持唯一了,但是此时的value存在重复数据甚至属性为空的情况,再通过遍历将属性为空进行去掉,对于数据中存在重复数据的话,就通过数据层进行判断,如果存在相同的数据就保存,存在不相同的数据就不进行保存。
简单代码演示:
演示jdk8的新特性将集合转换成map
package com.orange.test;
import com.orange.student.Student;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
public class IterateHashMapExample {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s = new Student("李四",18,"2022-02-16 12:00:35","");
Student s1 = new Student("张飞",19,"2022-02-18 12:00:35","2");
Student s2 = new Student("张飞",19,"2022-02-18 12:00:35","3");// --
Student s3 = new Student("张飞",19,"2022-02-18 12:00:35","");// --
Student s4 = new Student("张飞",19,"2022-02-18 12:00:35","");// --
Student s5 = new Student("张飞",19,"2022-02-18 12:00:35","6");// --
Student s6 = new Student("王五",19,"","7");
Student s7 = new Student("王五",19,"2022-02-18 12:00:35","8");
Student s8 = new Student("王五",19,"2022-02-18 12:00:35","");
list.add(s);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s6);
list.add(s4);
list.add(s5);
list.add(s8);
list.add(s7);
Map<String, List<Student>> coursesMap = list.stream().collect(Collectors.groupingBy(Student::getName));
System.out.println(coursesMap.keySet().size());
for (Map.Entry<String, List<Student>> stringListEntry : coursesMap.entrySet()) {
System.out.println(stringListEntry.getKey());
System.out.println(stringListEntry.getValue());
}
}
package com.orange.student;
public class Student {
private String name;
private int age;
private String sendTime;
private String completeTime;
public Student() {
}
public Student(String name, int age, String sendTime, String completeTime) {
this.name = name;
this.age = age;
this.sendTime = sendTime;
this.completeTime = completeTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSendTime() {
return sendTime;
}
public void setSendTime(String sendTime) {
this.sendTime = sendTime;
}
public String getCompleteTime() {
return completeTime;
}
public void setCompleteTime(String completeTime) {
this.completeTime = completeTime;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sendTime='" + sendTime + '\'' +
", completeTime='" + completeTime + '\'' +
'}';
}
}
数据库批量插入时排除属性重复数据
问题描述:
近期在对数据进行批量插入时,发现如果数据中存在相同的属性时,此时不应该插入相关的数据,而是在数据库中对重复的数据不进行批量插入,从而达到数据的唯一性。
解决思路:
在插入数据时进行判断,如果存在相同的数据,则不进行插入,如果不存在相同的数据,则进行插入
slq案例:
方式一:
INSERT INTO personOffice(personID, officeID)
SELECT '2', (SELECT officeID FROM offices WHERE title = 'Berlin') FROM dual
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personId = 2 AND officeID = 1)
方式二:
INSERT INTO personOffice (personID, officeID)
VALUES ('2', (SELECT officeID FROM offices WHERE title = 'Berlin'))
WHERE NOT EXISTS (SELECT * FROM personOffice WHERE personID = 2 AND officeID = 1)