Example usage for java.util LinkedHashSet LinkedHashSet

List of usage examples for java.util LinkedHashSet LinkedHashSet

Introduction

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

Prototype

public LinkedHashSet() 

Source Link

Document

Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).

Usage

From source file:Main.java

/**
 * Adds the value to map. If the key does not exists new value set is created and the value is added to it. The
 * method ignores null values//from   w w  w  . ja v a2  s  .c  o m
 *
 * @param <K>
 *            the key type
 * @param <V>
 *            the value type
 * @param map
 *            the map
 * @param key
 *            the key
 * @param newValue
 *            the new value
 */
public static <K, V> void addValueToSetMap(Map<K, Set<V>> map, K key, V newValue) {
    if (newValue == null) {
        return;
    }
    map.computeIfAbsent(key, k -> new LinkedHashSet<>()).add(newValue);
}

From source file:Main.java

/**
 * Returns only the valid child nodes of a node. Eliminates the nodes from
 * the list of child nodes of the input node if the child node type is same
 * with any of <code>ignoreNodeTypes</code>
 * /*ww w  . j  a v a 2  s. c  o  m*/
 * @param node
 * @param maintainOrder
 * @param ignoreNodeTypes
 * @return HashSet<Node>
 */
public static HashSet<Node> getValidChildNodes(Node node, boolean maintainOrder, short... ignoreNodeTypes) {
    NodeList childNodes = node.getChildNodes();
    HashSet<Node> filteredChildNodes = new HashSet<Node>();
    if (maintainOrder) {
        filteredChildNodes = new LinkedHashSet<Node>();
    }
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node thisNode = childNodes.item(i);
        boolean allowAdd = true;
        for (int j = 0; j < ignoreNodeTypes.length; j++) {
            if (ignoreNodeTypes[j] == thisNode.getNodeType()) {
                allowAdd = false;
                break;
            }
        }
        if (allowAdd) {
            filteredChildNodes.add(thisNode);
        }
    }
    return filteredChildNodes;
}

From source file:Main.java

public static <K, V> Set<V> getSet(Map<K, Set<V>> map, K key) {
    Set<V> set = map.get(key);
    if (set == null) {
        set = new LinkedHashSet<V>();
        map.put(key, set);/* w w  w  .j  av  a 2 s. com*/
    }

    return set;
}

From source file:Main.java

public static <ELEMENT> LinkedHashSet<ELEMENT> newLinkedHashSet() {
    return new LinkedHashSet<ELEMENT>();
}

From source file:Main.java

public static <E> Set<E> copyToSet(Iterable<? extends E> elements) {
    Set<E> set = new LinkedHashSet<E>();
    addAll(set, elements);// ww  w. j a  v  a  2  s. com
    return set;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static boolean addToSetInMap(Object key, Object value, Map theMap) {
    if (theMap == null)
        return false;
    Set theSet = (Set) theMap.get(key);
    if (theSet == null) {
        theSet = new LinkedHashSet();
        theMap.put(key, theSet);/*from www  .  j  a  v a2 s.c o  m*/
    }
    return theSet.add(value);
}

From source file:Main.java

/**
 * Reorder a list of elements by another list. Trying to keep absolute order of initial list in alphabetical order
 * but reorder regarding to provided relative order list.
 * E.g. initial was [1, 2, 3, 4, 5] - calling reorder with list [2, 5, 4] will generate list
 * [1, 2, 3, 5, 4]/*from w w  w  .ja  va 2  s.c o  m*/
 * @param elements - initial list
 * @param order - list describing relative order
 * @param <T> - Class of comparable object
 * @return - new reordered list
 */
public static <T extends Comparable> List<T> mergeReorder(List<T> elements, List<T> order) {
    if (order.size() == 0) {
        return elements;
    }
    if (elements.size() == 0) {
        return order;
    }
    Set<T> merged = new LinkedHashSet<>();
    Set<T> elementsSet = new HashSet<>(elements);
    int i = 0;
    int j = 0;
    T currElement = elements.get(i);
    T currOrder = order.get(j);
    while (i < elements.size() || j < order.size()) {
        if (j >= order.size()) {
            merged.addAll(elements.subList(i, elements.size()));
            break;
        }
        currElement = i < elements.size() ? elements.get(i) : currElement;
        currOrder = j < order.size() ? order.get(j) : currOrder;
        if (currElement.compareTo(currOrder) < 0) {
            merged.add(currElement);
            i++;
        }
        if (currOrder.compareTo(currElement) < 0 || i >= elements.size()) {
            if (merged.contains(currOrder)) {
                merged.remove(currOrder);
            }
            if (elementsSet.contains(currOrder)) {
                merged.add(currOrder);
            }
            j++;
        }
        if (currElement.compareTo(currOrder) == 0) {
            merged.add(currElement);
            i++;
            j++;
        }
    }
    return new ArrayList<>(merged);
}

From source file:Main.java

/**
 * Given any of the known collection types, this method will return an instance of the collection.
 * @param collectionType    the type of the collection
 * @return the collection instance//from   w w  w  .j  a v  a2s  .  com
 */
public static Collection<?> getCollection(Class<?> collectionType) {
    if (HashSet.class.equals(collectionType)) {
        return new HashSet<Object>();
    } else if (TreeSet.class.equals(collectionType)) {
        return new TreeSet<Object>();
    } else if (CopyOnWriteArraySet.class.equals(collectionType)) {
        return new CopyOnWriteArraySet<Object>();
    } else if (LinkedHashSet.class.equals(collectionType)) {
        return new LinkedHashSet<Object>();
    } else if (ArrayList.class.equals(collectionType)) {
        return new ArrayList<Object>();
    } else if (LinkedList.class.equals(collectionType)) {
        return new LinkedList<Object>();
    } else if (Vector.class.equals(collectionType)) {
        return new Vector<Object>();
    } else if (Stack.class.equals(collectionType)) {
        return new Stack<Object>();
    } else if (PriorityQueue.class.equals(collectionType)) {
        return new PriorityQueue<Object>();
    } else if (PriorityBlockingQueue.class.equals(collectionType)) {
        return new PriorityBlockingQueue<Object>();
    } else if (ArrayDeque.class.equals(collectionType)) {
        return new ArrayDeque<Object>();
    } else if (ConcurrentLinkedQueue.class.equals(collectionType)) {
        return new ConcurrentLinkedQueue<Object>();
    } else if (LinkedBlockingQueue.class.equals(collectionType)) {
        return new LinkedBlockingQueue<Object>();
    } else if (LinkedBlockingDeque.class.equals(collectionType)) {
        return new LinkedBlockingDeque<Object>();
    } else if (List.class.equals(collectionType)) {
        return new LinkedList<Object>();
    } else if (Set.class.equals(collectionType)) {
        return new HashSet<Object>();
    } else if (Queue.class.equals(collectionType)) {
        return new PriorityQueue<Object>();
    } else if (Deque.class.equals(collectionType)) {
        return new ArrayDeque<Object>();
    } else if (Collection.class.equals(collectionType)) {
        return new LinkedList<Object>();
    }
    throw new IllegalArgumentException("Unsupported collection type: " + collectionType);
}

From source file:Main.java

private static Set<String> filter(final List<String> enabledCiphers, final Set<String> supportedCiphers) {
    Set<String> result = new LinkedHashSet<String>();
    for (String enabledCipher : enabledCiphers) {
        if (supportedCiphers.contains(enabledCipher)) {
            result.add(enabledCipher);//from  www  . j  a v  a 2 s.  c o  m
        }
    }
    return result;
}

From source file:Main.java

public static Set<String> getQueryParameterNames(Uri uri) {
    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }/*from  w  ww .j  a v  a  2  s .  co m*/

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(uri.decode(name));

        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}