Java 學習記錄72 — Sets & HashSet — 4/5

張小雄
7 min readJul 2, 2021

--

SetMain.java

import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
Set<Integer> squares = new HashSet<>();
Set<Integer> cubes = new HashSet<>();
for (int i = 1; i <= 100; i++) {
squares.add(i * i);
cubes.add(i * i * i);
}
System.out.println("Squares size is: " + squares.size());
System.out.println("cubes size is: " + cubes.size());
Set<Integer> union = new HashSet<>(squares);
union.addAll(cubes);
System.out.println("Union size is: " + union.size());
}
}

輸出結果:

Squares size is: 100

cubes size is: 100

Union size is: 196

1 個 set 為 1~100的次方

另 1 個 set 為 1~100的立方

196 為 兩個 set 裡出現的所有數字,總和有 196 個

The Set Interface

Set Interface Bulk Operations

Bulk operations are particularly well suited to Sets; when applied, they perform standard set-algebraic operations. Suppose s1 and s2 are sets. Here's what bulk operations do:

  • s1.addAll(s2) — transforms s1 into the union of s1 and s2. (The union of two sets is the set containing all of the elements contained in either set.)
  • s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2. (The intersection of two sets is the set containing only the elements common to both sets.)
  • s1.removeAll(s2) — transforms s1 into the (asymmetric) set difference of s1 and s2. (For example, the set difference of s1 minus s2 is the set containing all of the elements found in s1 but not in s2.)
  • s1.containsAll(s2) — returns true if s2 is a subset of s1. (s2 is a subset of s1 if set s1 contains all of the elements in s2.)

SetMain.java 改

import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
Set<Integer> squares = new HashSet<>();
Set<Integer> cubes = new HashSet<>();
for (int i = 1; i <= 100; i++) {
squares.add(i * i);
cubes.add(i * i * i);
}
System.out.println("Squares size is: " + squares.size());
System.out.println("cubes size is: " + cubes.size());
Set<Integer> union = new HashSet<>(squares);
union.addAll(cubes);
System.out.println("Union size is: " + union.size());
Set<Integer> intersection = new HashSet<>(squares);
intersection.retainAll(cubes);
System.out.println("Intersections size is: " + intersection.size());
for (int i : intersection
) {
System.out.println(i + " is the square of " + Math.sqrt(i) + " and cube of " + Math.cbrt(i));
}
}
}

輸出結果:

Squares size is: 100

cubes size is: 100

Union size is: 196

Intersections size is: 4

4096 is the square of 64.0 and cube of 16.0

1 is the square of 1.0 and cube of 1.0

64 is the square of 8.0 and cube of 4.0

729 is the square of 27.0 and cube of 9.0

這次找兩邊都一起出現過的數字

Joe

The simplest way to put it is:

symmetric = same

asymmetric = not the same

If we had two sets:

Set<String> one = new HashSet<>();
one.add("Jon");
one.add("Rob");
one.add("Tom");

Set<String> two = new HashSet<>();
two.add("Jon");
two.add("Lee");
two.add("Tom");

and we wanted to return a set that was symmetric, we would use retainAll:

Set<String> intersection = new HashSet<>(one);
intersection.retainAll(two);
System.out.println(intersection); // [Tom, Jon]

As you can see both sets contain the elements in the set above, hence — same.

If we wanted to return a set that was asymmetric, we would use removeAll:

Set<String> difference = new HashSet<>(one);
difference.removeAll(two);
System.out.println(difference); // [Rob]

The set above contains the difference between the two sets — so the element that one contains that is not in two, and hence - not the same.

If we did it the other way — the element that two contains that is not in one:

Set<String> difference = new HashSet<>(two);
difference.removeAll(one);
System.out.println(difference); // [Lee]

SetMain.java 改

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
Set<Integer> squares = new HashSet<>();
Set<Integer> cubes = new HashSet<>();
for (int i = 1; i <= 100; i++) {
squares.add(i * i);
cubes.add(i * i * i);
}
// System.out.println("Squares size is: " + squares.size());
// System.out.println("cubes size is: " + cubes.size());
//
// Set<Integer> union = new HashSet<>(squares);
// union.addAll(cubes);
// System.out.println("Union size is: " + union.size());
//
// Set<Integer> intersection = new HashSet<>(squares);
// intersection.retainAll(cubes);
// System.out.println("Intersections size is: " + intersection.size());
// for (int i : intersection
// ) {
// System.out.println(i + " is the square of " + Math.sqrt(i) + " and cube of " + Math.cbrt(i));
// }
Set<String> words = new HashSet<>();
String sentence = "one day in the year of the fox";
String[] arrayWords = sentence.split(" ");
words.addAll(Arrays.asList(arrayWords));
for (String s : words) {
System.out.println(s);
}
}
}

輸出結果:

the

in

year

one

of

day

fox

Arrays asList()

SetMain.java 改

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
// Set<Integer> squares = new HashSet<>();
// Set<Integer> cubes = new HashSet<>();
//
// for (int i = 1; i <= 100; i++) {
// squares.add(i * i);
// cubes.add(i * i * i);
// }
// System.out.println("Squares size is: " + squares.size());
// System.out.println("cubes size is: " + cubes.size());
//
// Set<Integer> union = new HashSet<>(squares);
// union.addAll(cubes);
// System.out.println("Union size is: " + union.size());
//
// Set<Integer> intersection = new HashSet<>(squares);
// intersection.retainAll(cubes);
// System.out.println("Intersections size is: " + intersection.size());
// for (int i : intersection
// ) {
// System.out.println(i + " is the square of " + Math.sqrt(i) + " and cube of " + Math.cbrt(i));
// }
// Set<String> words = new HashSet<>();
// String sentence = "one day in the year of the fox";
// String[] arrayWords = sentence.split(" ");
// words.addAll(Arrays.asList(arrayWords));
//
// for (String s : words) {
// System.out.println(s);
// }
Set<String> nature = new HashSet<>();
Set<String> divine = new HashSet<>();
String[] natureWords = {"all", "nature", "is", "but", "art", "unknown", "to", "thee"};
nature.addAll(Arrays.asList(natureWords));
String[] divineWords = {"to", "err", "is", "human", "to", "forgive", "divine"};
divine.addAll(Arrays.asList(divineWords));
System.out.println("nature - divine:");
Set<String> diff1 = new HashSet<>(nature);
diff1.removeAll(divine);
printSet(diff1);
System.out.println("divine - nature:");
Set<String> diff2 = new HashSet<>(divine);
diff2.removeAll(nature);
printSet(diff2);

}
private static void printSet(Set<String> set) {
System.out.print("\t");
for (String s : set) {
System.out.println(s + " ");
}
System.out.println();
}
}

輸出結果:

nature — divine:

all but art thee nature unknown

divine — nature:

err forgive divine human

展示了差集

a 全部減去 b

b 全部減去 a

SetMain.java 改

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
// Set<Integer> squares = new HashSet<>();
// Set<Integer> cubes = new HashSet<>();
//
// for (int i = 1; i <= 100; i++) {
// squares.add(i * i);
// cubes.add(i * i * i);
// }
// System.out.println("Squares size is: " + squares.size());
// System.out.println("cubes size is: " + cubes.size());
//
// Set<Integer> union = new HashSet<>(squares);
// union.addAll(cubes);
// System.out.println("Union size is: " + union.size());
//
// Set<Integer> intersection = new HashSet<>(squares);
// intersection.retainAll(cubes);
// System.out.println("Intersections size is: " + intersection.size());
// for (int i : intersection
// ) {
// System.out.println(i + " is the square of " + Math.sqrt(i) + " and cube of " + Math.cbrt(i));
// }
// Set<String> words = new HashSet<>();
// String sentence = "one day in the year of the fox";
// String[] arrayWords = sentence.split(" ");
// words.addAll(Arrays.asList(arrayWords));
//
// for (String s : words) {
// System.out.println(s);
// }
Set<String> nature = new HashSet<>();
Set<String> divine = new HashSet<>();
String[] natureWords = {"all", "nature", "is", "but", "art", "unknown", "to", "thee"};
nature.addAll(Arrays.asList(natureWords));
String[] divineWords = {"to", "err", "is", "human", "to", "forgive", "divine"};
divine.addAll(Arrays.asList(divineWords));
// System.out.println("nature - divine:");
// Set<String> diff1 = new HashSet<>(nature);
// diff1.removeAll(divine);
// printSet(diff1);
//
// System.out.println("divine - nature:");
// Set<String> diff2 = new HashSet<>(divine);
// diff2.removeAll(nature);
// printSet(diff2);
Set<String> unionTest = new HashSet<>(nature);
unionTest.addAll(divine);
Set<String> intersectionTest = new HashSet<>(nature);
intersectionTest.retainAll(divine);
System.out.println("Symmetric difference");
unionTest.removeAll(intersectionTest);
printSet(unionTest);
} private static void printSet(Set<String> set) {
System.out.print("\t");
for (String s : set) {
System.out.print(s + " ");
}
System.out.println();
}
}

輸出結果:

Symmetric difference

all but art thee err nature forgive divine human unknown

輸出的是兩邊全部減掉兩邊共有

SetMain.java 改

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class SetMain {
public static void main(String[] args) {
// Set<Integer> squares = new HashSet<>();
// Set<Integer> cubes = new HashSet<>();
//
// for (int i = 1; i <= 100; i++) {
// squares.add(i * i);
// cubes.add(i * i * i);
// }
// System.out.println("Squares size is: " + squares.size());
// System.out.println("cubes size is: " + cubes.size());
//
// Set<Integer> union = new HashSet<>(squares);
// union.addAll(cubes);
// System.out.println("Union size is: " + union.size());
//
// Set<Integer> intersection = new HashSet<>(squares);
// intersection.retainAll(cubes);
// System.out.println("Intersections size is: " + intersection.size());
// for (int i : intersection
// ) {
// System.out.println(i + " is the square of " + Math.sqrt(i) + " and cube of " + Math.cbrt(i));
// }
// Set<String> words = new HashSet<>();
// String sentence = "one day in the year of the fox";
// String[] arrayWords = sentence.split(" ");
// words.addAll(Arrays.asList(arrayWords));
//
// for (String s : words) {
// System.out.println(s);
// }
Set<String> nature = new HashSet<>();
Set<String> divine = new HashSet<>();
String[] natureWords = {"all", "nature", "is", "but", "art", "unknown", "to", "thee"};
nature.addAll(Arrays.asList(natureWords));
String[] divineWords = {"to", "err", "is", "human", "to", "forgive", "divine"};
divine.addAll(Arrays.asList(divineWords));
// System.out.println("nature - divine:");
// Set<String> diff1 = new HashSet<>(nature);
// diff1.removeAll(divine);
// printSet(diff1);
//
// System.out.println("divine - nature:");
// Set<String> diff2 = new HashSet<>(divine);
// diff2.removeAll(nature);
// printSet(diff2);
Set<String> unionTest = new HashSet<>(nature);
unionTest.addAll(divine);
Set<String> intersectionTest = new HashSet<>(nature);
intersectionTest.retainAll(divine);
// System.out.println("Symmetric difference");
unionTest.removeAll(intersectionTest);
// printSet(unionTest);
if(nature.containsAll(divine)){
System.out.println("divine is a subset of nature");
}
if(nature.containsAll(intersectionTest)){
System.out.println("intersection is a subset of nature");
}
if(divine.containsAll(intersectionTest)){
System.out.println("intersection is a subset of divine");
}
} private static void printSet(Set<String> set) {
System.out.print("\t");
for (String s : set) {
System.out.print(s + " ");
}
System.out.println();
}
}

輸出結果:

intersection is a subset of nature

intersection is a subset of divine

測試了 b 是否為 a 的子集

本篇使用代碼

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet