The best way to find an additional object among two lists

I have two custom lists that tell CompanyList that

public class CompanyList<E> extends Collection<E> implements List<E> {} 

Here I have a list of CompanyList, so

public class CompanyMakeVO extends BaseVO {

    private static final long serialVersionUID = 1L;

    private String name;

    public CompanyMakeVO() {
        super();
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // overrides equals
    public boolean equals(Object obj) {
        if (obj == null || !(obj.getClass() == this.getClass())) {
            return false;
        }

        CompanyMakeVO make = (CompanyMakeVO) obj;

        // NAME
        String thisName = this.getName();
        String thatName = make.getName();

        if (null == thisName || null == thatName)
            return false;

        return thisName.equals(thatName);
    }

    // hashcode
    public int hashCode() {
        return getName().hashCode();
    }
}

I have two such lists: oldList and newList have some CompanyMakeVO objects, each object represents the company name through the name attribute. Let's say the old list has 3 objects named Audi, BMW and Aston Martin, and the new list consists of 5 objects named Audi, BMW, Aston Martin, Jaquar and Tesla. There will be no duplicate elements in the lists. The dug name will not be repeated. I need to find a unique element present in any list, as well as with the list name and element name. What is the best way to find out?

+4
2

, , List.removeAll().
, , HashMap .
List.removeAll() , O (NM). HashMap O (N + M), .

+2

removeAll() ArrayList, :

List<CompanyMakeVO> companyMakeVOListOld = new ArrayList<>();
//add your items to the old list

List<CompanyMakeVO> companyMakeVOListNew = new ArrayList<>();
//add your items to new list

//now removeAll duplicate items from new list by passing the old list
companyMakeVOListNew.removeAll(companyMakeVOListOld);

ArrayList - API removeAll:

public boolean removeAll ( c)

, .

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#removeAll(java.util.Collection)

0

Source: https://habr.com/ru/post/1659144/


All Articles