简体   繁体   中英

Merge two lists of objects without duplicates

I have three object classes:

public class Section{
    private Integer id;
    private List<Program> programs;
}

public class Program{
    private String title;
    private Integer id;
    private List<Broadcast> broadcasts = null;
}

public class Broadcast {
    private Integer id;
    private String title;
}

And I have two lists of Section object: List<Section> oldSections and List<Section> newSections . I need to check if oldSections contains all Sections from newSections list, to determine this I have to compare id values. if it doesn't then I need to add this Section to oldSections and if it does then I have to do the same check for Programs and Broadcasts.

I tried to iterate through all Sections, Programs and Broadcasts but it doesn't seem to be a good solution. What would the best approach?

    private void updateSections(List<Section> oldSections, List<Section> newSections){
        for(Section newSection: newSections){
            boolean alreadyAdded = false;
            for(Section oldSection: oldSections){
                if(newSection.getId() == oldSection.getId()){
                    alreadyAdded = true;
                }
            }
            if(!alreadyAdded){
                oldSections.add(newSection);
            } else {
                //HERE I HAVE TO COMPARE PROGRAMS AND THEN BROADCASTS
            }
        }
    }

A Set is a list that it can help you to avoid duplicate.

You can simply override the equals and hashCode methods of your Java Bean/POJO object and use set. This way a duplicate entry will be rejected and you don't need to merge your data anymore. Your list elements are always unique.

Take a look at this thread to see how to implement Set in java -> Verify there is a combination of unique string

You can also use java 8 to merge list without duplicate.

public class Test {
    public static void main(String[] args) {
        List<Student> list1 = Arrays.asList(new Student("a", 1), new Student("b", 2), new Student("f", 3));
        List<Student> list2 = Arrays.asList(new Student("b", 4), new Student("c", 5), new Student("f", 6));
        List<Student> list3 = Arrays.asList(new Student("b", 7), new Student("d", 8), new Student("e", 9));
        List<Student> dogs = new ArrayList<>(Stream.of(list1, list2, list3).flatMap(List::stream)
                .collect(Collectors.toMap(Student::getName, d -> d, (Student x, Student y) -> x == null ? y : x)).values());
        dogs.forEach(System.out::println);
    }
}

class Student {
    String name;
    int id;

    public Student(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", id=" + id + '}';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM