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

/**
 * Get the sorted selected positions in a byte with the right most
 * position as zero.//from   ww  w.j  a va 2  s.  c  om
 * @param input Byte to be evaluated
 * @return Array of integer positions.
 */
public static SortedSet<Integer> getSelectedPCR(byte input) {
    ArrayList<Integer> arrayList = new ArrayList<Integer>();
    byte mask = 0x01;
    for (int i = 0; i <= 7; i++) {
        int value = (input >>> i) & mask;
        if (value == 1) {
            arrayList.add(i);
        }
    }
    Collections.sort(arrayList);
    return Collections.unmodifiableSortedSet(new TreeSet<Integer>(arrayList));
}

From source file:Main.java

public static <T, V extends T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, V... args) {
    TreeSet<T> set = new TreeSet<T>(comparator);

    if (args != null) {
        for (V v : args) {
            set.add(v);//from w w w .j  a  v a2s  .c  om
        }
    }

    return set;
}

From source file:Main.java

public static <T, V extends T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, V... args) {
    TreeSet<T> set = new TreeSet<T>(comparator);
    if (args != null) {
        for (V v : args) {
            set.add(v);/*w w w  . j a v  a 2 s. co m*/
        }
    }
    return set;
}

From source file:Main.java

public static Date get(List<Date> otherDates, Date dateToApproach) {
    final TreeSet<Date> set = new TreeSet<Date>(otherDates);
    set.add(dateToApproach);// w w  w.j  ava 2  s. c  om
    final ArrayList<Date> list = new ArrayList<Date>(set);
    final int indexOf = list.indexOf(dateToApproach);
    if (indexOf == 0)
        return null;
    return list.get(indexOf - 1);
}

From source file:Main.java

public static <E> TreeSet<E> newTreeSet(final Collection<? extends E> c) {
    return new TreeSet<E>(c);
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> entriesSortedByValues(
        Map<K, V> map) {//from  w  w  w.ja v  a 2  s  .  c o m

    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 res = e1.getValue().compareTo(e2.getValue());
            /* Preserve items with equal values */
            return res != 0 ? res : 1;
        }
    });
    sortedEntries.addAll(map.entrySet());

    return sortedEntries;
}

From source file:Main.java

public static <K, V> Map<K, V> valueSortedMap(Map<K, V> map, Comparator<Entry<K, V>> comparator) {
    Set<Entry<K, V>> valueSortedEntries = new TreeSet<Entry<K, V>>(comparator);

    for (Entry<K, V> entry : map.entrySet()) {
        valueSortedEntries.add(entry);/*from  w  w w  . j av  a2  s . c  om*/
    }

    Map<K, V> sortedMap = new LinkedHashMap<K, V>(map.size());
    for (Entry<K, V> entry : valueSortedEntries) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }

    return sortedMap;
}

From source file:Main.java

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

From source file:Main.java

public static <E> TreeSet<E> newTreeSet(final Comparator<? super E> c) {
    return new TreeSet<E>(c);
}

From source file:Main.java

public static <T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, Iterable<? extends T> c) {
    TreeSet<T> set = new TreeSet<T>(comparator);

    iterableToCollection(c, set);//from w w w  . j  av  a2  s . c o  m

    return set;
}