コード例 #1
0
  @Test
  public void whenGetTopUsingMultiSet_thenCorrect() {
    final Multiset<String> names = HashMultiset.create();
    names.add("John");
    names.add("Adam", 5);
    names.add("Jane");
    names.add("Tom", 2);

    final Set<String> sorted = Multisets.copyHighestCountFirst(names).elementSet();
    final List<String> topTwo = Lists.newArrayList(sorted).subList(0, 2);
    assertEquals(2, topTwo.size());
    assertEquals("Adam", topTwo.get(0));
    assertEquals("Tom", topTwo.get(1));
  }
コード例 #2
0
  @Test
  public void whenInsertDuplicatesInMultiSet_thenInserted() {
    final Multiset<String> names = HashMultiset.create();
    names.add("John");
    names.add("Adam", 3);
    names.add("John");

    assertEquals(2, names.count("John"));
    names.remove("John");
    assertEquals(1, names.count("John"));

    assertEquals(3, names.count("Adam"));
    names.remove("Adam", 2);
    assertEquals(1, names.count("Adam"));
  }