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 args[]) {
    // create linked list object        
    List<Integer> list = new LinkedList<Integer>();

    // populate the list 
    list.add(-2);/*from w ww .  ja  va2 s.  co m*/
    list.add(2);
    list.add(-12);
    list.add(8);

    // create comparator for reverse order
    Comparator cmp = Collections.reverseOrder(Collections.reverseOrder());

    // sort the list
    Collections.sort(list, cmp);

    System.out.println(list);

}

From source file:Main.java

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

    // populate the list 
    list.add(-8);/*  w ww  . java2 s . c om*/
    list.add(2);
    list.add(-2);
    list.add(8);

    // create comparator for reverse order
    Comparator<Integer> cmp = Collections.reverseOrder();

    // sort the list
    Collections.sort(list, cmp);

    System.out.println("List sorted in ReverseOrder: ");
    System.out.println(list);

}

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  ww .java2  s . c  o m*/

    List dst = new ArrayList();
    for (int i = 0; i < coins.length; i++)
        dst.add("");

    Collections.copy(dst, src);

    ListIterator liter = dst.listIterator();

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

From source file:Main.java

public static void main(String[] args) {
    List<Integer> first = Arrays.asList(2, 3, 4, 5);
    List<Integer> second = Arrays.asList(1, 3, 4, 6);

    List<Integer> missing = new LinkedList<Integer>();
    List<Integer> added = new LinkedList<Integer>(second);

    for (Integer i : first) {
        if (!added.remove(i)) {
            missing.add(i);//from  w w w .j ava 2s.c o  m
        }
    }

    System.out.println("Missing ints in second: " + missing);
    System.out.println("New ints in second: " + added);
}

From source file:MainClass.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]);//from  w ww .ja v  a  2 s .co  m

    List dst = new ArrayList();
    for (int i = 0; i < coins.length; i++)
        dst.add("");

    Collections.copy(dst, src);

    ListIterator liter = dst.listIterator();

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

    Collections.fill(src, "no coins");

    liter = src.listIterator();

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

From source file:Main.java

public static void main(String... args) {
    final int MAX_VAL = 99999;
    List<Integer> linkedList = new LinkedList<Integer>();
    List<Integer> arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);/*from   www  . j a va2s . c  o m*/
        arrayList.add(i);
    }
    long time = System.nanoTime();

    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(MAX_VAL / 2, i);
    }
    System.out.println("LinkedList add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    for (int i = 0; i < MAX_VAL; i++) {
        arrayList.add(MAX_VAL / 2, i);
    }
    System.out.println("ArrayList add:" + (System.nanoTime() - time));

    // Reset the lists
    linkedList = new LinkedList<Integer>();
    arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);
        arrayList.add(i);
    }

    time = System.nanoTime();
    ListIterator<Integer> li = linkedList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        li.add(i);
    }
    System.out.println("LinkedList iterator add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    ListIterator<Integer> ali = arrayList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        ali.add(i);
    }
    System.out.println("ArrayList iterator add:" + (System.nanoTime() - time));
}

From source file:Main.java

public static void main(String[] args) {
    List<Task> tasks = new LinkedList<Task>();
    String[] tasksLabels = new String[] { "A", "B", "C" };
    Clock clock = new Clock(tasksLabels.length, new Counter());
    for (String s : tasksLabels) {
        Task task = new Task(s, clock);
        tasks.add(task);//from   w ww.  j  a v  a 2  s. c om
        task.start();
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);/*from  w ww  .ja va  2s .c o m*/
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    System.out.print("List sorted in reverse: ");
    for (int i : ll) {
        System.out.print(i + " ");
    }

    System.out.println();

    Collections.shuffle(ll);

    System.out.print("List shuffled: ");
    for (int i : ll)
        System.out.print(i + " ");

    System.out.println();

    System.out.println("Minimum: " + Collections.min(ll));
    System.out.println("Maximum: " + Collections.max(ll));
}

From source file:MainClass.java

public static void main(String args[]) {

    LinkedList<String> ll = new LinkedList<String>();

    ll.add("B");/*w w w.j  a v  a  2 s  .  c o  m*/
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");

    ll.add(1, "A2");

    System.out.println("Original contents of ll: " + ll);

    ll.remove("F");
    ll.remove(2);

    System.out.println("Contents of ll after deletion: " + ll);

    ll.removeFirst();
    ll.removeLast();

    System.out.println("ll after deleting first and last: " + ll);

    String val = ll.get(2);
    ll.set(2, val + " Changed");

    System.out.println("ll after change: " + ll);
}

From source file:AllFieldsSnippet.java

public static void main(String[] args) {
    Object obj = new Object();
    //start extract AllFieldsSnippet

    Class cls = obj.getClass();/* www .j  ava 2  s . c o m*/
    List accum = new LinkedList();
    while (cls != null) {
        Field[] f = cls.getDeclaredFields();
        for (int i = 0; i < f.length; i++) {
            accum.add(f[i]);
        }
        cls = cls.getSuperclass();
    }
    Field[] allFields = (Field[]) accum.toArray(new Field[accum.size()]);
    //stop extract AllFieldsSnippet
}