OnJava8-Examples/containers/LinkedListFeatures.java

48 lines
1.6 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// containers/LinkedListFeatures.java
2015-11-14 16:18:05 -08:00
// <20>2016 MindView LLC: see Copyright.txt
2015-06-15 17:47:35 -07:00
import typeinfo.pets.*;
import java.util.*;
public class LinkedListFeatures {
public static void main(String[] args) {
LinkedList<Pet> pets =
new LinkedList<>(Pets.arrayList(5));
2015-11-03 12:00:44 -08:00
System.out.println(pets);
2015-06-15 17:47:35 -07:00
// Identical:
2015-11-03 12:00:44 -08:00
System.out.println("pets.getFirst(): " + pets.getFirst());
System.out.println("pets.element(): " + pets.element());
2015-06-15 17:47:35 -07:00
// Only differs in empty-list behavior:
2015-11-03 12:00:44 -08:00
System.out.println("pets.peek(): " + pets.peek());
2015-06-15 17:47:35 -07:00
// Identical; remove and return the first element:
2015-11-03 12:00:44 -08:00
System.out.println("pets.remove(): " + pets.remove());
System.out.println("pets.removeFirst(): " + pets.removeFirst());
2015-06-15 17:47:35 -07:00
// Only differs in empty-list behavior:
2015-11-03 12:00:44 -08:00
System.out.println("pets.poll(): " + pets.poll());
System.out.println(pets);
2015-06-15 17:47:35 -07:00
pets.addFirst(new Rat());
2015-11-03 12:00:44 -08:00
System.out.println("After addFirst(): " + pets);
2015-06-15 17:47:35 -07:00
pets.offer(Pets.randomPet());
2015-11-03 12:00:44 -08:00
System.out.println("After offer(): " + pets);
2015-06-15 17:47:35 -07:00
pets.add(Pets.randomPet());
2015-11-03 12:00:44 -08:00
System.out.println("After add(): " + pets);
2015-06-15 17:47:35 -07:00
pets.addLast(new Hamster());
2015-11-03 12:00:44 -08:00
System.out.println("After addLast(): " + pets);
System.out.println("pets.removeLast(): " + pets.removeLast());
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[Rat, Manx, Cymric, Mutt, Pug]
pets.getFirst(): Rat
pets.element(): Rat
pets.peek(): Rat
pets.remove(): Rat
pets.removeFirst(): Manx
pets.poll(): Cymric
[Mutt, Pug]
After addFirst(): [Rat, Mutt, Pug]
After offer(): [Rat, Mutt, Pug, Cymric]
After add(): [Rat, Mutt, Pug, Cymric, Pug]
After addLast(): [Rat, Mutt, Pug, Cymric, Pug, Hamster]
pets.removeLast(): Hamster
2015-09-07 11:44:36 -06:00
*/