Example usage for java.util TreeSet TreeSet

List of usage examples for java.util TreeSet TreeSet

Introduction

In this page you can find the example usage for java.util TreeSet TreeSet.

Prototype

public TreeSet(SortedSet<E> s) 

Source Link

Document

Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

Usage

From source file:Main.java

public static <K, V extends Comparable<? super V>> SortedSet<Entry<K, V>> sortMapByValueDesc(Map<K, V> map) {
    SortedSet<Entry<K, V>> sorted = new TreeSet<>(new Comparator<Entry<K, V>>() {
        @Override//from   w ww.j a va 2s.  c o m
        public int compare(Entry<K, V> e1, Entry<K, V> e2) {
            int r = e2.getValue().compareTo(e1.getValue());
            return r == 0 ? 1 : r;
        }
    });

    for (Entry<K, V> entry : map.entrySet())
        sorted.add(new SimpleEntry(entry));
    return sorted;
}

From source file:Main.java

public static <T> Set<T> asTreeSet(T... ts) {

    return new TreeSet<T>(Arrays.asList(ts));
}

From source file:Main.java

public static String[][] returnTable(Collection E) {
    if ((E == null) || E.isEmpty()) {
        System.out.println("The collection is empty!");
    }/* w  w w .j a v a2  s  .co m*/

    Set<Field> collectionFields = new TreeSet<>(new Comparator<Field>() {
        @Override
        public int compare(Field o1, Field o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    for (Object o : E) {
        Class c = o.getClass();
        createSetOfFields(collectionFields, c);
        while (c.getSuperclass() != null) {
            c = c.getSuperclass();
            createSetOfFields(collectionFields, c);
        }
    }
    String[][] exitText = new String[E.size() + 1][collectionFields.size() + 1];
    exitText[0][0] = String.format("%20s", "Class");
    int indexOfColumn = 0;
    for (Field f : collectionFields) {
        exitText[0][indexOfColumn + 1] = String.format("%20s", f.getName());
        indexOfColumn++;

    }
    int indexOfRow = 0;
    for (Object o : E) {
        indexOfColumn = 0;
        exitText[indexOfRow + 1][0] = String.format("%20s", o.getClass().getSimpleName());
        for (Field field : collectionFields) {
            try {
                field.setAccessible(true);
                if (field.get(o) instanceof Date) {
                    exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20tD", field.get(o));
                } else {
                    String temp = String.valueOf(field.get(o));
                    exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", temp);
                }
            } catch (Exception e) {
                exitText[indexOfRow + 1][indexOfColumn + 1] = String.format("%20s", "-");
            }
            indexOfColumn++;
        }
        indexOfRow++;
    }
    return exitText;
}

From source file:Main.java

/**
 * Sorts entry set by values in ascending order.
 * /*  w w w  .  java  2  s .com*/
 * @param map
 * @return sorted entry set.
 */
public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> sortByValuesAscending(
        Map<K, V> map) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() {
        @Override
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
            int result = e1.getValue().compareTo(e2.getValue());
            return result != 0 ? result : 1; // saves equal entries
        }
    });
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}

From source file:Main.java

/**
 * Sorts entry set by values in descending order.
 * /*w w  w  .  j av a2 s.c  o  m*/
 * @param map
 * @return sorted entry set.
 */
public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> sortByValuesDescending(
        Map<K, V> map) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() {
        @Override
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
            int result = e2.getValue().compareTo(e1.getValue());
            return result != 0 ? result : 1; // saves equal entries
        }
    });
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}

From source file:Main.java

public static <E> TreeSet<E> getTreeSet(Comparator<? super E> comparator) {
    return new TreeSet<E>(comparator);
}

From source file:Main.java

public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>(setA);
    tmp.removeAll(setB);/*from  ww  w  . j  av a 2 s. c  o m*/
    return tmp;
}

From source file:Main.java

public static <E> TreeSet<E> getTreeSet(SortedSet<E> set) {
    return new TreeSet<E>(set);
}

From source file:Main.java

/**
 * Creates a map which is sorted by entry values.
 *   //from   ww w  . j a va 2  s.c om
 * @param map the original map
 * 
 * @param <K> the key type
 * @param <V> the value type
 * 
 * @return a map which is by entry values.
 */
public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> getEntriesSortedByValues(
        Map<K, V> map) {
    SortedSet<Map.Entry<K, V>> sortedEntries = new TreeSet<Map.Entry<K, V>>(new Comparator<Map.Entry<K, V>>() {
        @Override
        public int compare(Map.Entry<K, V> e1, Map.Entry<K, V> e2) {
            return e1.getValue().compareTo(e2.getValue());
        }
    });
    sortedEntries.addAll(map.entrySet());
    return sortedEntries;
}

From source file:Main.java

public static <T> TreeSet<T> createTreeSet(Comparator<? super T> comparator) {
    return new TreeSet<T>(comparator);
}