在Android中,我有一个Set,我想将其转换为已排序的List

Collections.sort()怎么做?

protected List<PackageInfo> doInBackground(Void... params) {

        Set<PackageInfo> adPackages = new HashSet<PackageInfo>();
        //ArrayList<PackageInfo> adPackages = new ArrayList<PackageInfo>();

        [..............]

        return new ArrayList<PackageInfo>(adPackages);
        //return adPackages;
    }

最佳答案

List有一个构造函数,可让您使用任何Collection创建它。然后使用Collections.sort(List list)方法。

例如。

Set<String> set = new HashSet<String>();
set.add("This is a unsorted set");
List<String> list = new ArrayList<String>(set);
Collections.sort(list); // or Collections.sort(list, Comparator);


干杯。

09-27 02:02
查看更多