2015-12-15 11:47:04 -08:00
|
|
|
// collections/SetOperations.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.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class SetOperations {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Set<String> set1 = new HashSet<>();
|
|
|
|
Collections.addAll(set1,
|
|
|
|
"A B C D E F G H I J K L".split(" "));
|
|
|
|
set1.add("M");
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("H: " + set1.contains("H"));
|
|
|
|
System.out.println("N: " + set1.contains("N"));
|
2015-06-15 17:47:35 -07:00
|
|
|
Set<String> set2 = new HashSet<>();
|
|
|
|
Collections.addAll(set2, "H I J K L".split(" "));
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
"set2 in set1: " + set1.containsAll(set2));
|
2015-06-15 17:47:35 -07:00
|
|
|
set1.remove("H");
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("set1: " + set1);
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
"set2 in set1: " + set1.containsAll(set2));
|
2015-06-15 17:47:35 -07:00
|
|
|
set1.removeAll(set2);
|
2015-12-02 09:20:27 -08:00
|
|
|
System.out.println(
|
|
|
|
"set2 removed from set1: " + set1);
|
2015-06-15 17:47:35 -07:00
|
|
|
Collections.addAll(set1, "X Y Z".split(" "));
|
2017-05-10 11:45:39 -06:00
|
|
|
System.out.println(
|
|
|
|
"'X Y Z' added to set1: " + set1);
|
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
|
|
|
H: true
|
|
|
|
N: false
|
|
|
|
set2 in set1: true
|
|
|
|
set1: [A, B, C, D, E, F, G, I, J, K, L, M]
|
|
|
|
set2 in set1: false
|
|
|
|
set2 removed from set1: [A, B, C, D, E, F, G, M]
|
2017-05-10 11:45:39 -06:00
|
|
|
'X Y Z' added to set1: [A, B, C, D, E, F, G, M, X, Y,
|
|
|
|
Z]
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|