Rectangle 27 0

java Compare two sets of differrent types?


List<ValueObject> list = new ArrayList<>();
    List<DTO> list2 = new ArrayList<>();

    list.add(ValueObject.of(1));
    list.add(ValueObject.of(2));
    list.add(ValueObject.of(3));

    list2.add(DTO.of(1));
    list2.add(DTO.of(2));
    list2.add(DTO.of(34));
public boolean compareLists(List<ValueObject> list, List<DTO> list2) {
    if(list.size() != list2.size()) {
        return false;
    }
    for(int i = 0; i < list.size(); i++) {
        if(list.get(i).id == list2.get(i).id) {
            continue;
        } else {
            return false;
        }
    }
    return true;
}

Also, avoid using public variables in your classes. A good practice is to make your variables private and use getter and setter methods.

Another solution would be to use List instead of Set (if you are allowed to do so). List has a method called get(int index) that retrieves the element at the specified index and you can compare them one by one when both your lists have the same size. More on lists: http://docs.oracle.com/javase/7/docs/api/java/util/List.html

Lists maintain order of insertion. And from what I gather, you want your result to be true only if each collection has the exact same elements in the exact same positions. If I misinterpreted your question, please let me know.

Oh, ok. Then disregard the solution. If I come up with something for sets, I'll post a comment.

What would be the difference if I iterate through a set elements one by one, what I actually do.

no, same positions is not a requirement here. Actually I use Set interface which does not require preserving positions

Note
Rectangle 27 0

java Compare two sets of differrent types?


List<ValueObject> list = new ArrayList<>();
    List<DTO> list2 = new ArrayList<>();

    list.add(ValueObject.of(1));
    list.add(ValueObject.of(2));
    list.add(ValueObject.of(3));

    list2.add(DTO.of(1));
    list2.add(DTO.of(2));
    list2.add(DTO.of(34));
public boolean compareLists(List<ValueObject> list, List<DTO> list2) {
    if(list.size() != list2.size()) {
        return false;
    }
    for(int i = 0; i < list.size(); i++) {
        if(list.get(i).id == list2.get(i).id) {
            continue;
        } else {
            return false;
        }
    }
    return true;
}

Also, avoid using public variables in your classes. A good practice is to make your variables private and use getter and setter methods.

Another solution would be to use List instead of Set (if you are allowed to do so). List has a method called get(int index) that retrieves the element at the specified index and you can compare them one by one when both your lists have the same size. More on lists: http://docs.oracle.com/javase/7/docs/api/java/util/List.html

Lists maintain order of insertion. And from what I gather, you want your result to be true only if each collection has the exact same elements in the exact same positions. If I misinterpreted your question, please let me know.

Oh, ok. Then disregard the solution. If I come up with something for sets, I'll post a comment.

What would be the difference if I iterate through a set elements one by one, what I actually do.

no, same positions is not a requirement here. Actually I use Set interface which does not require preserving positions

Note