OnJava8-Examples/containers/ListFeatures.java

93 lines
3.2 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// containers/ListFeatures.java
2015-11-14 16:18:05 -08:00
// <20>2016 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.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
import typeinfo.pets.*;
import java.util.*;
public class ListFeatures {
public static void main(String[] args) {
Random rand = new Random(47);
List<Pet> pets = Pets.arrayList(7);
2015-11-03 12:00:44 -08:00
System.out.println("1: " + pets);
2015-06-15 17:47:35 -07:00
Hamster h = new Hamster();
pets.add(h); // Automatically resizes
2015-11-03 12:00:44 -08:00
System.out.println("2: " + pets);
System.out.println("3: " + pets.contains(h));
2015-06-15 17:47:35 -07:00
pets.remove(h); // Remove by object
Pet p = pets.get(2);
2015-11-03 12:00:44 -08:00
System.out.println("4: " + p + " " + pets.indexOf(p));
2015-06-15 17:47:35 -07:00
Pet cymric = new Cymric();
2015-11-03 12:00:44 -08:00
System.out.println("5: " + pets.indexOf(cymric));
System.out.println("6: " + pets.remove(cymric));
2015-06-15 17:47:35 -07:00
// Must be the exact object:
2015-11-03 12:00:44 -08:00
System.out.println("7: " + pets.remove(p));
System.out.println("8: " + pets);
2015-06-15 17:47:35 -07:00
pets.add(3, new Mouse()); // Insert at an index
2015-11-03 12:00:44 -08:00
System.out.println("9: " + pets);
2015-06-15 17:47:35 -07:00
List<Pet> sub = pets.subList(1, 4);
2015-11-03 12:00:44 -08:00
System.out.println("subList: " + sub);
System.out.println("10: " + pets.containsAll(sub));
2015-06-15 17:47:35 -07:00
Collections.sort(sub); // In-place sort
2015-11-03 12:00:44 -08:00
System.out.println("sorted subList: " + sub);
2015-06-15 17:47:35 -07:00
// Order is not important in containsAll():
2015-11-03 12:00:44 -08:00
System.out.println("11: " + pets.containsAll(sub));
2015-06-15 17:47:35 -07:00
Collections.shuffle(sub, rand); // Mix it up
2015-11-03 12:00:44 -08:00
System.out.println("shuffled subList: " + sub);
System.out.println("12: " + pets.containsAll(sub));
2015-06-15 17:47:35 -07:00
List<Pet> copy = new ArrayList<>(pets);
sub = Arrays.asList(pets.get(1), pets.get(4));
2015-11-03 12:00:44 -08:00
System.out.println("sub: " + sub);
2015-06-15 17:47:35 -07:00
copy.retainAll(sub);
2015-11-03 12:00:44 -08:00
System.out.println("13: " + copy);
2015-06-15 17:47:35 -07:00
copy = new ArrayList<>(pets); // Get a fresh copy
copy.remove(2); // Remove by index
2015-11-03 12:00:44 -08:00
System.out.println("14: " + copy);
2015-06-15 17:47:35 -07:00
copy.removeAll(sub); // Only removes exact objects
2015-11-03 12:00:44 -08:00
System.out.println("15: " + copy);
2015-06-15 17:47:35 -07:00
copy.set(1, new Mouse()); // Replace an element
2015-11-03 12:00:44 -08:00
System.out.println("16: " + copy);
2015-06-15 17:47:35 -07:00
copy.addAll(2, sub); // Insert a list in the middle
2015-11-03 12:00:44 -08:00
System.out.println("17: " + copy);
System.out.println("18: " + pets.isEmpty());
2015-06-15 17:47:35 -07:00
pets.clear(); // Remove all elements
2015-11-03 12:00:44 -08:00
System.out.println("19: " + pets);
System.out.println("20: " + pets.isEmpty());
2015-06-15 17:47:35 -07:00
pets.addAll(Pets.arrayList(4));
2015-11-03 12:00:44 -08:00
System.out.println("21: " + pets);
2015-06-15 17:47:35 -07:00
Object[] o = pets.toArray();
2015-11-03 12:00:44 -08:00
System.out.println("22: " + o[3]);
2015-06-15 17:47:35 -07:00
Pet[] pa = pets.toArray(new Pet[0]);
2015-11-03 12:00:44 -08:00
System.out.println("23: " + pa[3].id());
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
1: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug]
2: [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Hamster]
3: true
4: Cymric 2
5: -1
6: false
7: true
8: [Rat, Manx, Mutt, Pug, Cymric, Pug]
9: [Rat, Manx, Mutt, Mouse, Pug, Cymric, Pug]
subList: [Manx, Mutt, Mouse]
10: true
sorted subList: [Manx, Mouse, Mutt]
11: true
shuffled subList: [Mouse, Manx, Mutt]
12: true
sub: [Mouse, Pug]
13: [Mouse, Pug]
14: [Rat, Mouse, Mutt, Pug, Cymric, Pug]
15: [Rat, Mutt, Cymric, Pug]
16: [Rat, Mouse, Cymric, Pug]
17: [Rat, Mouse, Mouse, Pug, Cymric, Pug]
18: false
19: []
20: true
21: [Manx, Cymric, Rat, EgyptianMau]
22: EgyptianMau
23: 14
2015-09-07 11:44:36 -06:00
*/