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() 

Source Link

Document

Constructs a new, empty tree set, sorted according to the natural ordering of its elements.

Usage

From source file:Main.java

public static <T> SortedSet<T> asSortedSet(T... args) {
    SortedSet<T> result = new TreeSet<T>();
    if (args != null) {
        for (T arg : args) {
            if (arg != null) {
                result.add(arg);//from  w  w w .j a v a 2s  .co  m
            }
        }
    }
    return result;
}

From source file:edu.temple.cis3238.wiki.utils.CollectionsUtilities.java

/**
 *
 * @param set/*from  w w  w  .j  av a 2  s . c o  m*/
 * @return
 */
public static String setToCSV(Set<String> set) {
    StringBuilder sb = new StringBuilder("");
    Set<String> tagNameSet = new TreeSet<>();
    for (String t : set) {
        if (t == null) {
            continue;
        }
        String tag = edu.temple.cis3238.wiki.utils.StringUtils.stripInvalidChars((String) t);
        tagNameSet.add(tag);
        sb.append(tag);
        sb.append(",");
    }
    return org.apache.commons.lang3.StringUtils.removeEndIgnoreCase(sb.toString(), ",");
}

From source file:Main.java

public static <T> TreeSet<T> treeSet(final T item) {
    final TreeSet<T> treeSet = new TreeSet<>();
    if (null != item) {
        treeSet.add(item);//from  ww  w.j a v a2 s  .c  o m
    }

    return treeSet;
}

From source file:Main.java

public static <K, V> Map<V, Set<K>> invertMapOfCollection(Map<K, ? extends Collection<V>> mapOfCollection) {
    Map<V, Set<K>> result = new TreeMap<V, Set<K>>();

    for (Entry<K, ? extends Collection<V>> inputEntry : mapOfCollection.entrySet()) {
        K inputKey = inputEntry.getKey();
        Collection<V> inputCollection = inputEntry.getValue();

        for (V inputValue : inputCollection) {
            Set<K> resultSet = result.get(inputValue);
            if (resultSet == null) {
                resultSet = new TreeSet<K>();
                result.put(inputValue, resultSet);
            }/* w  w  w.  j  ava  2s.co  m*/
            resultSet.add(inputKey);
        }
    }

    return result;
}

From source file:Main.java

public static <T> SortedSet<T> unionSortedSet(Set<T>... sets) {
    SortedSet<T> unionSet = new TreeSet<T>();
    for (Set<T> set : sets) {
        unionSet.addAll(set);//from  w w  w. j a  v a2s .  c o m
    }
    return unionSet;
}

From source file:Main.java

public static <E> TreeSet<E> newTreeSet() {
    return new TreeSet<E>();
}

From source file:Main.java

/**
 * internal method to handle the selections if items are added / removed
 *
 * @param positions     the positions map which should be adjusted
 * @param startPosition the global index of the first element modified
 * @param endPosition   the global index up to which the modification changed the indices (should be MAX_INT if we check til the end)
 * @param adjustBy      the value by which the data was shifted
 * @return the adjusted set/*from  w  w  w . j  a v a  2 s  . c  o m*/
 */
public static SortedSet<Integer> adjustPosition(Set<Integer> positions, int startPosition, int endPosition,
        int adjustBy) {
    SortedSet<Integer> newPositions = new TreeSet<>();

    for (Integer entry : positions) {
        int position = entry;

        //if our current position is not within the bounds to check for we can add it
        if (position < startPosition || position > endPosition) {
            newPositions.add(position);
        } else if (adjustBy > 0) {
            //if we added items and we are within the bounds we can simply add the adjustBy to our entry
            newPositions.add(position + adjustBy);
        } else if (adjustBy < 0) {
            //if we removed items and we are within the bounds we have to check if the item was removed
            //adjustBy is negative in this case
            if (position > startPosition + adjustBy && position <= startPosition) {
                ;//we are within the removed items range we don't add this item anymore
            } else {
                //otherwise we adjust our position
                newPositions.add(position + adjustBy);
            }
        }
    }

    return newPositions;
}

From source file:Main.java

public static <T> TreeSet<T> treeSet(final T[] items) {
    final TreeSet<T> treeSet = new TreeSet<>();
    if (null != items) {
        for (final T item : items) {
            if (null != item) {
                treeSet.add(item);/*from w w w.  j a v  a  2s .  c  o  m*/
            }
        }
    }

    return treeSet;
}

From source file:Main.java

/**
 * Parses a string parameter value into a set of strings.
 * //from  w w w . ja v a2 s. c om
 * @param values The values of the set.
 * @return The set.
 */
public static Set<String> parseParameterList(String values) {
    Set<String> result = new TreeSet<String>();
    if (values != null && values.trim().length() > 0) {
        // the spec says the scope is separated by spaces, but Facebook uses commas, so we'll include commas, too.
        String[] tokens = values.split("[\\s+,]");
        result.addAll(Arrays.asList(tokens));
    }
    return result;
}

From source file:Main.java

/**
 * Converts the given array of elements to a sortedset.
 *
 * @param elements The elements//from   w ww. j av a  2  s .c  o  m
 * @return The elements as a set, empty if elements was null
 */
public static <T> SortedSet<T> asSortedSet(T... elements) {
    SortedSet<T> result = new TreeSet<T>();
    if (elements == null) {
        return result;
    }
    result.addAll(asList(elements));
    return result;
}