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

Source Link

Document

Constructs an empty list.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List<String> sortedList = new LinkedList<String>();
    sortedList.addAll(Arrays.asList(new String[] { "a", "b", "c", "d" }));

    int index = Collections.binarySearch(sortedList, "c");

    index = Collections.binarySearch(sortedList, "cc");
}

From source file:Main.java

public static void main(String[] args) {
    List list = new LinkedList();

    DateFormatSymbols dfs = new DateFormatSymbols();
    String[] months = dfs.getMonths();
    for (int i = 0; i < months.length; i++) {
        String month = months[i];
        list.add(month);/*from  ww  w  .  j  ava2s  .co m*/
    }

    Collections.sort(list);
    System.out.println("Month Names = " + list);
    int index = Collections.binarySearch(list, "October");
    if (index > 0) {
        System.out.println("Found at index = " + index);
        String month = (String) list.get(index);
        System.out.println("Month = " + month);
    }
}

From source file:Main.java

public static void main(String[] args) {
    Queue<String> queue = new LinkedList<>();
    queue.add("Java");
    // offer() will work the same as add()
    queue.offer("SQL");
    queue.offer("CSS");
    queue.offer("XML");

    System.out.println("Queue: " + queue);

    // Let's remove elements until the queue is empty
    while (queue.peek() != null) {
        System.out.println("Head  Element: " + queue.peek());
        queue.remove();/*  w w w . ja v  a 2 s .c  o m*/
        System.out.println("Removed one  element from  Queue");
        System.out.println("Queue: " + queue);
    }
    System.out.println("queue.isEmpty(): " + queue.isEmpty());
    System.out.println("queue.peek(): " + queue.peek());
    System.out.println("queue.poll(): " + queue.poll());
    try {
        String str = queue.element();
        System.out.println("queue.element(): " + str);
        str = queue.remove();
        System.out.println("queue.remove(): " + str);
    } catch (NoSuchElementException e) {
        System.out.println("queue.remove(): Queue is  empty.");
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new LinkedList<String>();

    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    list.add("e");
    list.add("f");

    Collections.sort(list);//from  w  ww .  ja  v  a  2  s. c o m
    System.out.println(list);
    int index = Collections.binarySearch(list, "c");
    if (index > 0) {
        System.out.println("Found at index = " + index);
        String month = (String) list.get(index);
        System.out.println(month);
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<String> strings = new LinkedList<>();
    strings.add("a");
    strings.add("B");
    strings.add("ab");
    strings.add("abc");
    strings.add("ABC");

    ConcurrentMap<Integer, List<String>> byLength = strings.parallelStream()
            .collect(Collectors.groupingByConcurrent(String::length));

    System.out.println(byLength);
}

From source file:Main.java

public static void main(String args[]) {
    // create Linked List
    List<Integer> list = new LinkedList<Integer>();

    // populate list
    list.add(5);/*from  www .  j  a  v a  2s . co  m*/
    list.add(2);
    list.add(1);
    list.add(-3);

    System.out.println("List before shuffle: " + list);

    // shuffle the list
    Collections.shuffle(list, new Random());

    System.out.println("List after shuffle: " + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // create a new collection and add some elements
    Collection collection = new ArrayList();
    collection.add("One");
    collection.add("Two");
    collection.add("Three");

    // append the collection in the LinkedList
    list.addAll(collection);/* w  w w. j a  v  a 2 s .c  o m*/

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:Main.java

public static void main(String[] args) {

    // create a LinkedList
    LinkedList<String> list = new LinkedList<String>();

    // add some elements
    list.add("Hello");
    list.add("from java2s.com");
    list.add("10");

    // print the list
    System.out.println("LinkedList:" + list);

    // create a new collection and add some elements
    Collection collection = new ArrayList();
    collection.add("One");
    collection.add("Two");
    collection.add("Three");

    // add the collection in the LinkedList at index 2
    list.addAll(2, collection);//from   w  ww  .ja  va2 s .com

    // print the new list
    System.out.println("LinkedList:" + list);
}

From source file:LinkedListExample.java

public static void main(String[] args) {
    // Create a new LinkedList
    LinkedList<Integer> list = new LinkedList<Integer>();

    // Add Items to the array list
    list.add(new Integer(1));
    list.add(new Integer(2));
    list.add(new Integer(3));
    list.add(new Integer(4));
    list.add(new Integer(5));
    list.add(new Integer(6));
    list.add(new Integer(7));
    list.add(new Integer(8));
    list.add(new Integer(9));
    list.add(new Integer(10));

    // Use iterator to display the values
    for (Iterator i = list.iterator(); i.hasNext();) {
        Integer integer = (Integer) i.next();
        System.out.println(integer);
    }//from ww w .j  a va2s  .  c  o m

    // Remove the element at index 5 (value=6)
    list.remove(5);

    // Set the value at index 5, this overwrites the value 7
    list.set(5, new Integer(66));

    // Use the linked list as a queue:
    // add an object to the end of the list (queue)
    // remove an item from the head of the list (queue)
    list.addLast(new Integer(11));
    Integer head = (Integer) list.removeFirst();
    System.out.println("Head: " + head);

    // Use iterator to display the values
    for (Iterator i = list.iterator(); i.hasNext();) {
        Integer integer = (Integer) i.next();
        System.out.println(integer);
    }
}

From source file:Main.java

public static void main(String[] args) {
    String[] coins = { "A", "B", "C", "D", "E" };

    List src = new LinkedList();
    for (int i = 0; i < coins.length; i++) {
        src.add(coins[i]);// w  w  w.ja  v  a 2 s .  co  m
    }

    System.out.println(src);

    Collections.fill(src, "java2s.com");

    Iterator liter = src.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());

}