Basically I would like to know how I can check two lists to see if the position of the elements are the same in both lists after sorting one with Collections package Collections.sort, e.g:
List<String> first = new ArrayList<String>();
a.add("one");
a.add("two");
List<String> second = new ArrayList<String>();
b.add("two");
b.add("one");
Collections.sort(second);
then check if one lists has the elements in the same order, returning a boolean false if the elements position is the same and true otherwise.
PS: I though of just checking the position of the last element but im not sure if thats a good way of doing this.
答案 0 :(得分:4)
Just use first.equals(second)
after sorting one of the lists. If both have the same number of elements and the elements are pair-wise equal, you'll get true.
This relies, of course, on the element type of the Lists to override equals
properly.