collections - Set

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值