Example usage for java.util LinkedList LinkedList

List of usage examples for java.util LinkedList LinkedList

Introduction

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

Prototype

public LinkedList(Collection<? extends E> c) 

Source Link

Document

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Usage

From source file:Main.java

public static <E, V extends Comparable<? super V>, K> Map<K, V> sortByValueAsc(Map<K, V> map, int limit) {
    List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }//from w  w w  . j  av  a2  s .  c o m
    });

    Map<K, V> result = new LinkedHashMap<K, V>();
    int counter = 0;
    for (Map.Entry<K, V> entry : list) {
        if (limit > 0 && counter >= limit) {
            break;
        }
        result.put(entry.getKey(), entry.getValue());
        counter++;
    }
    return result;
}

From source file:Main.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static List<Set<Entry<String, Object>>> sortMapByKey(Map<String, ? extends Object> map) {
    if (map == null) {
        return null;
    }//  w  w w .  j  a  v a  2s  .  co  m
    List<Set<Entry<String, Object>>> returnlist = new LinkedList(map.entrySet());
    Collections.sort(returnlist, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o1)).getKey()).compareTo(((Map.Entry) (o2)).getKey());
        }
    });
    return returnlist;
}

From source file:MyComparator.java

public static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap) {

    List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());

    Collections.sort(list, new MyComparator());

    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Entry<String, Integer> entry : list) {
        sortedMap.put(entry.getKey(), entry.getValue());
    }//ww w . java 2s  .  c  o  m
    return sortedMap;
}

From source file:Main.java

public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortMapByValue(Map<K, V> map,
        final boolean descending) {
    List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {

        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            int comp = (o1.getValue()).compareTo(o2.getValue());

            if (descending) {
                comp = comp * (-1);//w  w w  .  j  ava 2  s.co m
            }

            return comp;
        }
    });

    LinkedHashMap<K, V> result = new LinkedHashMap<K, V>();
    for (Map.Entry<K, V> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

From source file:Main.java

/**
 * Sort a map according to its values in ascending order when asc=true and i descending order otherwise.
 * @param unsortedMap /*from w ww  .  ja v a2  s  . c om*/
 * @param asc
 * @return
 */
public static Map<String, Integer> sortUsingComparator(Map<String, Integer> unsortedMap, final boolean asc) {

    // Convert Map to List
    List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortedMap.entrySet());

    // Sort a list using custom comparator, to compare the Map values
    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            if (asc) {
                return (o1.getValue()).compareTo(o2.getValue());
            } else {
                return -1 * (o1.getValue()).compareTo(o2.getValue());
            }
        }
    });

    // Convert the sorted map back to a Map
    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
    for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
        Map.Entry<String, Integer> entry = it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}

From source file:Main.java

public static <A> boolean isSuffixOf(final List<A> ls1, final List<A> ls2) {
    if (ls1 == null) {
        return true;
    }//from w w w .j a  va2  s  .c  o m
    if (ls2 == null) {
        return false;
    }
    if (ls1.size() > ls2.size()) {
        return false;
    }
    final List<A> ls1Rev = new LinkedList<A>(ls1);
    final List<A> ls2Rev = new LinkedList<A>(ls2);
    Collections.reverse(ls1Rev);
    Collections.reverse(ls2Rev);
    return isPrefixOf(ls1Rev, ls2Rev);
}

From source file:Main.java

public static <T> Collection<? extends T> maskedCopyOf(final Collection<? extends T> source,
        final Collection<? extends T> mask) {
    final Collection<T> copy = new LinkedList<T>(source);
    copy.retainAll(new ArrayList<T>(mask));
    return copy;/*from   w ww  .j ava  2 s.  c o  m*/
}

From source file:Main.java

/**
 * Parses the XPath expression in a string array containing the XPath elements. The expressiong must be a simple /a/b/c for
 * and it cannot contain any wild-cards or attributes.
 * //from  w  w  w  . j a  va  2s  .c o  m
 * @param sPath Path to be parsed.
 * @return The XPath expression in a string array.
 * @throws IllegalArgumentException Thrown if the parsing failed.
 */
public static String[] parsePath(String sPath) throws IllegalArgumentException {
    if (sPath.indexOf("//") >= 0) {
        throw new IllegalArgumentException("Destination path cannot have wild-cards.");
    }

    List<String> lElemList = new LinkedList<String>(Arrays.asList(sPath.split("/")));

    for (ListIterator<String> iIter = lElemList.listIterator(); iIter.hasNext();) {
        String sElem = iIter.next();

        if (sElem.equals(".")) {
            throw new IllegalArgumentException("Destination path cannot have wild-cards.");
        }

        int iIndex = sElem.indexOf("@");

        if (iIndex >= 0) {
            sElem = ((iIndex > 0) ? sElem.substring(0, iIndex) : "");
        }

        if (sElem.length() == 0) {
            iIter.remove();
        }
    }

    return lElemList.toArray(new String[lElemList.size()]);
}

From source file:Main.java

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

From source file:Main.java

private static Map sortByComparator(Map unsortMap) {
    List list = new LinkedList(unsortMap.entrySet());
    Collections.sort(list, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
        }/*  ww  w  . j a v a 2s.  c o  m*/
    });
    Map sortedMap = new LinkedHashMap();
    for (Iterator it = list.iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        sortedMap.put(entry.getKey(), entry.getValue());
    }
    return sortedMap;
}