-1

I hava to compare two Or more Set like List'<'Integer'>' l1,List l2,List l3 ...,But How to compare them ?

In my view,first Collections.sort(list),then l1.euqals(l2),and so on... But it seems not the effective way,It cost about O(n^2), and that there are not Two List,It may be three or four or more...

And Anthoer Way is useing extra enough max Array A init Zero,first A[l1.get(i)] fill with A number Not-Zero,then A[l2.get(i)] fill with Zero, at last,if the Array A still full with Zero ,the two list are equal. And it cost O(n),but the trouble thing is not only two List...

Is there any fast and efficient way which only cost less on time? I hope I express my mind clear .Hope ans!

xiaocainiao
  • 33
  • 1
  • 5
  • `Set#equals()` in Java doesn't take into account order of elements, so sorting is not necessary. – Alex Salauyou Aug 21 '15 at 08:03
  • @JordiCastilla This question is about n > 2 sets, which is different from the question in the link you've posted (compare **two** sets). – laune Aug 21 '15 at 08:13
  • @JordiCastilla ,@laune yeah, and I need to compare the Collections like List not the Set. – xiaocainiao Aug 21 '15 at 08:21
  • 1
    http://stackoverflow.com/questions/2762093/java-compare-two-lists – Jordi Castilla Aug 21 '15 at 08:30
  • @xiaocainiao please just google it, and, if you know how to compare two, you can apply same mechanism to compare 3 or more.... – Jordi Castilla Aug 21 '15 at 08:31
  • @JordiCastilla The 2nd link you've posted regarding "java-compar-two-lists" is definitely discussing something else, i.e. counting the number of equal elements. You should remove this comment. – laune Aug 21 '15 at 09:31
  • @JordiCastilla This "google it" comment might be applied to - how many percent of SO questions? – laune Aug 21 '15 at 09:41

1 Answers1

0

Let's say you have a

List<Collection<X>> listOfColls

and need to know whether they are all equal. A precondition for list or set equality is that they have equal number of elements. So

public static <X> boolean allequal( List<Collection<X>> listOfColls ){
    if( listOfColls.size() <= 1 ) return true;
    int nel = listOfColls.get(0).size();
    for( int i = 1; i < listOfColls.size(); ++i ){
        if( listOfColls.get(i).size() != nel ) return false;
    }
    for( int i = 1; i < listOfColls.size(); ++i ){
        if( ! listOfColls.get(i).equals( listOfColls.get(0) ) ) return false;
    }
    return true;
}

Chances are high that in a set of lists or sets that aren't all equal you have at least one with different cardinality. That's why it may be, on average, advantageous to do a quick check on this property before calling equals n-1 times.

Note however, that the actual class of the Collection influences the way equals works. For Set, it is set comparison where order does not apply. For List, order is relevant, and you may have duplicate (equal) elements. You may have to preprocess a List by sorting it, or by converting it to a Set...

As for equals itself, rely on the List or Set implementation to do the best thing. One shouldn't rewrite JDK class methods - those programmers are hard to surpass.

laune
  • 31,114
  • 3
  • 29
  • 42
  • I need to compare the Collections like List not the Set. – xiaocainiao Aug 21 '15 at 08:16
  • OK - actually this should work on a List of Collections - same thing for List and Set – laune Aug 21 '15 at 08:18
  • You wrote "compare two or more Set". Make up your mind! - Point is whether you need "set semantics" as opposed to "list semantics". – laune Aug 21 '15 at 08:24
  • your answer seems work, and it cost less compare times. thank you very much! if Set compare does need to sort ,but list compare seems need to sort, – xiaocainiao Aug 21 '15 at 08:38
  • I'll add a hint of different semantics of Set vs. List compare to my answer. – laune Aug 21 '15 at 09:21