OnJava8-Examples/collections/LinkedListFeatures.java

55 lines
1.8 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collections/LinkedListFeatures.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
import reflection.pets.*;
2015-06-15 17:47:35 -07:00
import java.util.*;
public class LinkedListFeatures {
public static void main(String[] args) {
LinkedList<Pet> pets =
new LinkedList<>(new PetCreator().list(5));
2015-11-03 12:00:44 -08:00
System.out.println(pets);
2015-06-15 17:47:35 -07:00
// Identical:
2015-12-02 09:20:27 -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-12-02 09:20:27 -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);
pets.offer(new PetCreator().get());
2015-11-03 12:00:44 -08:00
System.out.println("After offer(): " + pets);
pets.add(new PetCreator().get());
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);
2015-12-02 09:20:27 -08:00
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, Rat]
After add(): [Rat, Mutt, Pug, Rat, Rat]
After addLast(): [Rat, Mutt, Pug, Rat, Rat, Hamster]
2015-06-15 17:47:35 -07:00
pets.removeLast(): Hamster
2015-09-07 11:44:36 -06:00
*/