Java List Sort toSortedList(Collection collection)

Here you can find the source of toSortedList(Collection collection)

Description

to Sorted List

License

Apache License

Declaration

public static <T extends Comparable<T>> List<T> toSortedList(Collection<T> collection) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import java.util.List;

public class Main {
    public static <T extends Comparable<T>> List<T> toSortedList(Collection<T> collection) {
        List<T> list = new ArrayList<T>();
        list.addAll(collection);/*w  ww.  j av a2  s .  c  om*/
        Collections.sort(list);
        return list;
    }

    public static <T extends Comparable<T>> List<T> toSortedList(Collection<T> values, int top) {

        List<T> list = toSortedList(values);

        List<T> topList = new ArrayList<T>();
        for (int i = 0; i < top && i < list.size(); i++) {
            topList.add(list.get(i));
        }

        return topList;
    }

    public static <T> void addAll(T[] array, List<T> list) {
        for (T t : array) {
            list.add(t);
        }
    }
}

Related

  1. stringListSort(List list)
  2. subtractSortedLists(List a, List b, Comparator comparator)
  3. toSortedList(Collection in)
  4. toSortedList(Collection collection)
  5. toSortedList(Collection collection)