Apache HashBag Guava HashMultiset 和 Eclipse HashBag

Bag / ultiset 将每个对象与出现次数一起存储在集合中。界面上的额外方法允许一次添加或删除对象的多个副本。JDK analog 是 HashMap <T,Integer>,当值是此键的副本数时。

类型 番石榴 Apache Commons Collections GS 系列 JDK
排序未定义 HashMultiset HashBag HashBag HashMap 中
排序 TreeMultiset TreeBag TreeBag TreeMap 的
插入顺序 LinkedHashMultiset - - LinkedHashMap 的
并发变体 ConcurrentHashMultiset SynchronizedBag SynchronizedBag Collections.synchronizedMap(HashMap<String, Integer>)
并发和排序 - SynchronizedSortedBag SynchronizedSortedBag Collections.synchronizedSortedMap(TreeMap<String,Integer>)
永恒的收藏 ImmutableMultiset UnmodifiableBag UnmodifiableBag Collections.unmodifiableMap(HashMap<String, Integer)]
不可变和分类 ImmutableSortedMultiset UnmodifiableSortedBag UnmodifiableSortedBag Collections.unmodifiableSortedMap(TreeMap<String, Integer>

示例

1. 使用 Apache 的 SynchronizedSortedBag

    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = SynchronizedSortedBag.synchronizedBag(new TreeBag(Arrays.asList(INPUT_TEXT.split(" "))));

    // Print count words
    System.out.println(bag); // print [1:All!,2:Hello,1:Hi,2:World!]- in natural (alphabet) order
    // Print all unique words
    System.out.println(bag.uniqueSet());    // print [All!, Hello, Hi, World!]- in natural (alphabet) order

    // Print count occurrences of words
    System.out.println("Hello = " + bag.getCount("Hello"));    // print 2
    System.out.println("World = " + bag.getCount("World!"));    // print 2
    System.out.println("All = " + bag.getCount("All!"));    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));    // print 1
    System.out.println("Empty = " + bag.getCount("Empty"));    // print 0

    // Print count all words
    System.out.println(bag.size());    //print 6

    // Print count unique words
    System.out.println(bag.uniqueSet().size());    //print 4

2.使用 Eclipse 中的 TreeBag(GC)

    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    MutableSortedBag<String> bag =  TreeBag.newBag(Arrays.asList(INPUT_TEXT.split(" ")));

    // Print count words
    System.out.println(bag); // print [All!, Hello, Hello, Hi, World!, World!]- in natural order
    // Print all unique words
    System.out.println(bag.toSortedSet());    // print [All!, Hello, Hi, World!]- in natural order

    // Print count occurrences of words
    System.out.println("Hello = " + bag.occurrencesOf("Hello"));    // print 2
    System.out.println("World = " + bag.occurrencesOf("World!"));    // print 2
    System.out.println("All = " + bag.occurrencesOf("All!"));    // print 1
    System.out.println("Hi = " + bag.occurrencesOf("Hi"));    // print 1
    System.out.println("Empty = " + bag.occurrencesOf("Empty"));    // print 0

    // Print count all words
    System.out.println(bag.size());    //print 6

    // Print count unique words
    System.out.println(bag.toSet().size());    //print 4

3. 使用 Guava 的 LinkedHashMultiset

    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Multiset<String> multiset = LinkedHashMultiset.create(Arrays.asList(INPUT_TEXT.split(" ")));

    // Print count words
    System.out.println(multiset); // print [Hello x 2, World! x 2, All!, Hi]- in predictable iteration order
    // Print all unique words
    System.out.println(multiset.elementSet());    // print [Hello, World!, All!, Hi] - in predictable iteration order

    // Print count occurrences of words
    System.out.println("Hello = " + multiset.count("Hello"));    // print 2
    System.out.println("World = " + multiset.count("World!"));    // print 2
    System.out.println("All = " + multiset.count("All!"));    // print 1
    System.out.println("Hi = " + multiset.count("Hi"));    // print 1
    System.out.println("Empty = " + multiset.count("Empty"));    // print 0

    // Print count all words
    System.out.println(multiset.size());    //print 6

    // Print count unique words
    System.out.println(multiset.elementSet().size());    //print 4

更多例子:

I. Apache 集合:

  1. HashBag - 订单未定义
  2. SynchronizedBag - 并发和未定义的顺序
  3. SynchronizedSortedBag - - 并发和排序顺序
  4. TreeBag - 排序顺序

II。GS / Eclipse 集合

  1. MutableBag - 订单未定义
  2. MutableSortedBag - 排序顺序

III。番石榴

  1. HashMultiset - 订单未定义
  2. TreeMultiset - 排序顺序
  3. LinkedHashMultiset - 广告订单
  4. ConcurrentHashMultiset - 并发和未定义的顺序