[英]Check two lists for order equality after sorting elements of one list
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, eg: 基本上,我想知道如何使用Collections包Collections.sort对一个列表进行排序后,如何检查两个列表,以查看两个列表中元素的位置是否相同,例如:
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. 然后检查一个列表中的元素是否具有相同的顺序,如果元素位置相同,则返回布尔值false,否则返回true。
PS: I though of just checking the position of the last element but im not sure if thats a good way of doing this. PS:虽然我只是检查最后一个元素的位置,但是我不确定这是否是一个好方法。
Just use first.equals(second)
after sorting one of the lists. 排序其中一个列表后,只需使用
first.equals(second)
。 If both have the same number of elements and the elements are pair-wise equal, you'll get true. 如果两个元素的数量相同,并且元素成对相等,那么您将获得true。
This relies, of course, on the element type of the Lists to override equals
properly. 当然,这依赖于列表的元素类型正确地覆盖
equals
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.