OnJava8-Examples/collectiontopics/CollectionMethods.java

136 lines
3.0 KiB
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/CollectionMethods.java
2016-12-30 17:23:13 -08:00
// (c)2017 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.
2016-01-25 18:05:55 -08:00
// Things you can do with all Collections
2015-06-15 17:47:35 -07:00
import java.util.*;
import static onjava.HTMLColors.*;
2015-06-15 17:47:35 -07:00
public class CollectionMethods {
public static void main(String[] args) {
Collection<String> c =
new ArrayList<>(LIST.subList(0, 4));
2015-06-15 17:47:35 -07:00
c.add("ten");
c.add("eleven");
show(c);
border();
2015-06-15 17:47:35 -07:00
// Make an array from the List:
Object[] array = c.toArray();
// Make a String array from the List:
String[] str = c.toArray(new String[0]);
// Find max and min elements; this means
// different things depending on the way
// the Comparable interface is implemented:
2015-12-02 09:20:27 -08:00
System.out.println(
"Collections.max(c) = " + Collections.max(c));
System.out.println(
"Collections.min(c) = " + Collections.min(c));
border();
2015-06-15 17:47:35 -07:00
// Add a Collection to another Collection
Collection<String> c2 =
new ArrayList<>(LIST.subList(10, 14));
2015-06-15 17:47:35 -07:00
c.addAll(c2);
show(c);
border();
c.remove(LIST.get(0));
show(c);
border();
2015-06-15 17:47:35 -07:00
// Remove all components that are
// in the argument collection:
c.removeAll(c2);
show(c);
border();
2015-06-15 17:47:35 -07:00
c.addAll(c2);
show(c);
border();
2015-06-15 17:47:35 -07:00
// Is an element in this Collection?
String val = LIST.get(3);
2015-12-02 09:20:27 -08:00
System.out.println(
"c.contains(" + val + ") = " + c.contains(val));
2015-06-15 17:47:35 -07:00
// Is a Collection in this Collection?
2015-12-02 09:20:27 -08:00
System.out.println(
"c.containsAll(c2) = " + c.containsAll(c2));
2015-06-15 17:47:35 -07:00
Collection<String> c3 =
((List<String>)c).subList(3, 5);
// Keep all the elements that are in both
// c2 and c3 (an intersection of sets):
c2.retainAll(c3);
show(c2);
2015-06-15 17:47:35 -07:00
// Throw away all the elements
// in c2 that also appear in c3:
c2.removeAll(c3);
2017-01-22 16:48:11 -08:00
System.out.println(
"c2.isEmpty() = " + c2.isEmpty());
border();
// Functional operation:
c = new ArrayList<>(LIST);
c.removeIf(s -> !s.startsWith("P"));
c.removeIf(s -> s.startsWith("Pale"));
// Stream operation:
c.stream().forEach(System.out::println);
2015-06-15 17:47:35 -07:00
c.clear(); // Remove all elements
2015-11-03 12:00:44 -08:00
System.out.println("after c.clear():" + c);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
AliceBlue
AntiqueWhite
Aquamarine
Azure
ten
eleven
2017-04-16 10:18:21 -07:00
******************************
2015-06-15 17:47:35 -07:00
Collections.max(c) = ten
Collections.min(c) = AliceBlue
2017-04-16 10:18:21 -07:00
******************************
AliceBlue
AntiqueWhite
Aquamarine
Azure
ten
eleven
Brown
BurlyWood
CadetBlue
Chartreuse
2017-04-16 10:18:21 -07:00
******************************
AntiqueWhite
Aquamarine
Azure
ten
eleven
Brown
BurlyWood
CadetBlue
Chartreuse
2017-04-16 10:18:21 -07:00
******************************
AntiqueWhite
Aquamarine
Azure
ten
eleven
2017-04-16 10:18:21 -07:00
******************************
AntiqueWhite
Aquamarine
Azure
ten
eleven
Brown
BurlyWood
CadetBlue
Chartreuse
2017-04-16 10:18:21 -07:00
******************************
c.contains(Azure) = true
2015-06-15 17:47:35 -07:00
c.containsAll(c2) = true
c2.isEmpty() = true
2017-04-16 10:18:21 -07:00
******************************
PapayaWhip
PeachPuff
Peru
Pink
Plum
PowderBlue
Purple
2015-06-15 17:47:35 -07:00
after c.clear():[]
2015-09-07 11:44:36 -06:00
*/