Java-9.9 set
In this chapter, we will discuss some sets.
Because the objects in the set are non-repetitive, it determines that the functions searched and queried in the set are frequently used. Therefore, we generally use hashset, because it performs special Optimization on the search.
1. HashSet
Because HashSet performs the hash operation on the object, because its search is based on the hash code, its output is unordered.
package com.ray.ch09;import java.util.Arrays;import java.util.HashSet;import java.util.Random;public class Test {public static void main(String[] args) {HashSet
set = new HashSet
();Random random = new Random();for (int i = 0; i < 10000; i++) {set.add(random.nextInt(30));}System.out.println(Arrays.toString(set.toArray()));}}
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 29, 28]
2. TreeSet
If you need to sort the results, you should use TreeSet, which places the object on the red/black tree.
package com.ray.ch09;import java.util.Arrays;import java.util.Random;import java.util.TreeSet;public class Test {public static void main(String[] args) {TreeSet
set = new TreeSet
();Random random = new Random();for (int i = 0; i < 10000; i++) {set.add(random.nextInt(30));}System.out.println(Arrays.toString(set.toArray()));}}
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
3. Non-repetitive demonstration of set
package com.ray.ch09;import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;public class Test {public static void main(String[] args) {HashSet
set = new HashSet
();for (int i = 0; i < 10; i++) {set.add(i);}System.out.println(Arrays.toString(set.toArray()));set.add(12);System.out.println(Arrays.toString(set.toArray()));ArrayList
list = new ArrayList
();list.add(1);set.addAll(list);System.out.println(Arrays.toString(set.toArray()));}}
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12]
4. contains Method
The non-repeatability of the set determines that the contains method is the most frequently used method in the set.
package com.ray.ch09;import java.util.HashSet;public class Test {public static void main(String[] args) {HashSet
set = new HashSet
();for (int i = 0; i < 10; i++) {set.add(i);}System.out.println(set.contains(2));System.out.println(set.contains(12));}}
Output:
True
False
5. When we need to use a sorted list of non-repetitive personnel, we can prioritize TreeSet. Note the comments in the code.
Package com. ray. ch09; import java. util. Arrays; import java. util. TreeSet; public class Test {public static void main (String [] args) {TreeSet
TreeSet = new TreeSet
(String. CASE_INSENSITIVE_ORDER); // mainly set the sorting attribute here, only valid String text = Aabbye, Caesar, abbe, Bairn, cais, Dagmar, baby; string [] names = text. split (,); for (int I = 0; I <names. length; I ++) {treeSet. add (names [I]);} System. out. println (Arrays. toString (treeSet. toArray ()));}}
Output:
[Aabbye, abbe, baby, Bairn, Caesar, cais, Dagmar]
Summary: This chapter describes how to use and pay attention to HashSet and TreeSet.