Java Collection Sort sort(Collection collection)

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

Description

sort

License

Open Source License

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void sort(Collection collection) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Main {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void sort(Collection collection) {
        if (isNotEmpty(collection)) {
            Object[] objs = collectionToArray(collection);
            Arrays.sort(objs);// w  w w  .  java  2  s  .  com

            collection.clear();
            collection.addAll(arrayToCollection(objs));

        }
    }

    public static void sort(Object[] objs) {
        if (isNotEmpty(objs)) {
            Arrays.sort(objs);
        }

    }

    @SuppressWarnings("rawtypes")
    public static boolean isNotEmpty(Map map) {
        return !isEmpty(map);
    }

    @SuppressWarnings("rawtypes")
    public static boolean isNotEmpty(Collection collection) {
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(Object[] objs) {
        return !isEmpty(objs);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Object[] collectionToArray(Collection collection) {
        Object[] objs = null;
        if (isNotEmpty(collection)) {
            objs = new Object[collection.size()];
            collection.toArray(objs);
        }
        return objs;

    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List arrayToCollection(Object[] objs) {

        return new ArrayList(Arrays.asList(objs));

    }

    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Map map) {
        if (map == null || map.size() == 0) {
            return true;
        }
        return false;
    }

    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Collection collection) {
        if (collection == null || collection.size() == 0) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(Object[] objs) {
        if (objs == null || objs.length == 0) {
            return true;
        }
        return false;
    }
}

Related

  1. getSorted(Collection collection)
  2. isSort(Collection c)
  3. sort(Collection collection, Comparator comparator)
  4. sort(Collection items)
  5. sortByDescendingOrder(Map collection)
  6. sortClassesByLevelOfInheritance(Collection> classes)