OnJava8-Examples/holding/SetOperations.java

33 lines
1.0 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: holding/SetOperations.java
import java.util.*;
import static net.mindview.util.Print.*;
public class SetOperations {
public static void main(String[] args) {
2015-05-05 11:20:13 -07:00
Set<String> set1 = new HashSet<>();
2015-04-20 15:36:01 -07:00
Collections.addAll(set1,
"A B C D E F G H I J K L".split(" "));
set1.add("M");
print("H: " + set1.contains("H"));
print("N: " + set1.contains("N"));
2015-05-05 11:20:13 -07:00
Set<String> set2 = new HashSet<>();
2015-04-20 15:36:01 -07:00
Collections.addAll(set2, "H I J K L".split(" "));
print("set2 in set1: " + set1.containsAll(set2));
set1.remove("H");
print("set1: " + set1);
print("set2 in set1: " + set1.containsAll(set2));
set1.removeAll(set2);
print("set2 removed from set1: " + set1);
Collections.addAll(set1, "X Y Z".split(" "));
print("'X Y Z' added to set1: " + set1);
}
} /* Output:
H: true
N: false
set2 in set1: true
2015-05-05 11:20:13 -07:00
set1: [D, E, F, G, A, B, C, L, M, I, J, K]
2015-04-20 15:36:01 -07:00
set2 in set1: false
2015-05-05 11:20:13 -07:00
set2 removed from set1: [D, E, F, G, A, B, C, M]
'X Y Z' added to set1: [D, E, F, G, A, B, C, M, Y, X, Z]
2015-04-20 15:36:01 -07:00
*///:~