The most common use for a Set is to test for membership, so you can easily ask whether an object is in a Set.
The HashSet in earlier versions of Java produced output on discernible order. A HashSet uses hashing for speed.
The order maintained by a HashSet is different from a TreeSet or a LinkedHashSet, since each implementation has a different way of storing elements.
TreeSet keeps elements sorted into a red-black tree data structure, whereas HashSet uses the hashing function. The hashing algorithm was changed now. TreeSet is not synchronized.
LinkedHashSet also uses hashing for lookup speed, but appears to maintain elements in insertion order using a linked list. LinkedHashSet started from since 1.4, maintains a doubly-linked list and it is not synchronized.
set example
case 1:
// collections/SetOfInteger.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
public class SetOfInteger {
public static void main(String[] args) {
Random rand = new Random(47);
Set<Integer> intset = new HashSet<>();
for (int i = 0; i < 10000; i++) {
intset.add(rand.nextInt(30));
}
System.out.println(intset);
}
}
/* Output: // 30 Integers.
[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]
*/
case 2:
// collections/UniqueWordsAlphabetic.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Producing an alphabetic listing
import java.nio.file.*;
import java.util.*;
public class UniqueWordsAlphabetic {
public static void main(String[] args) throws Exception {
// Final class Files and final class Paths started from 1.7
List<String> lines = Files.readAllLines(Paths.get("SetOperations.java"));
Set<String> words = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); // alphabetically order.
for (String line : lines) {
for (String word :
line.split("\\W+")) { // \\W+ means it splits on one or more non-word letters.
// System.out.println("word: " + word + ", " + word.length()); // word has empty string, uncommented this sentence to see this.
if (word.trim().length() > 0) {
words.add(word);
}
}
}
System.out.println(words);
}
}
/* My Output:
[2017, A, add, addAll, added, any, args, B, book, c, class, code, collections, com, contains, containsAll, Copyright, D, E, F, false, fit, for, from, G, guarantees, H, HashSe
t, http, I, import, in, information, is, J, java, K, L, LLC, M, main, make, MindView, more, N, new, no, OnJava8, out, Output, println, public, purpose, remove, removeAll, rem
oved, see, Set, set1, set2, SetOperations, split, static, String, System, that, this, to, true, txt, util, Visit, void, We, X, Y, Z]
*/
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/SetOfInteger.java
3. https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html
4. https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html
5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/SetOperations.java
6. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/UniqueWordsAlphabetic.java
7. https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html
8. https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html

本文深入探讨了Java集合框架中Set接口的不同实现,包括HashSet、TreeSet和LinkedHashSet的特点与应用场景。通过实例展示了如何使用这些集合来解决实际问题,如随机数去重和按字母顺序排列单词。

412

被折叠的 条评论
为什么被折叠?



