3

I need to sort an ArrayList and i can't use Collections.sort(), because Google App Engine's Java runtime environment doesn't support it. How can I do? Thanks! ^^


Does Google App Engine support TreeSet? If so, assuming there are no duplicates and the elements are Comparable, use a TreeSet.

List list = your ArrayList
List sortedList = new ArrayList(new TreeSet(list));

You can also supply a Comparator to TreeSet if the elements aren't Comparable. I left generics left out for simplicity.

4

2 回答 2

2

If your ArrayList has NO DUPLICATE VALUES..... and i am assuming even if it had, you didnt wanted to have redundant data..so its better to use TreeSet and Comparator together...

Eg:

ArrayList<MyObject> arr = new ArrayList<MyObject>();

TreeSet<MyObject> t = new TreeSet<MyObject>(Comparator c);

t.addAll(arr);
于 2012-07-07T18:50:34.733 回答
1

谷歌应用引擎支持TreeSet吗?如果是这样,假设没有重复并且元素是Comparable,请使用TreeSet.

List list = your ArrayList
List sortedList = new ArrayList(new TreeSet(list));

如果元素不是,您也可以提供Comparatorto 。为简单起见,我忽略了泛型。TreeSetComparable

于 2012-07-07T18:43:18.233 回答