当前位置:网站首页>Guava: use of multiset

Guava: use of multiset

2022-07-06 21:30:00 amadeus_ liu2

package com.example.app;

import com.google.common.collect.*;

import java.util.Iterator;

public class MultiSetTest {
    public static void main(String[] args) {
        Multiset<String> multiSet = HashMultiset.create();
        //Multiset There will be duplicate elements in , Like the following "abc"
        multiSet.add("abc");
        multiSet.add("abc");
        multiSet.add("def");

        // When adding elements, you can directly specify the number of repetitions of an element 
        multiSet.add("ghi",4);

        // You can remove a specified element by the number of times 
        multiSet.remove("ghi",1);

        // You can directly set the number of specified elements 
        multiSet.setCount("ghi",5);

        // Set the number of elements to 0, It is equivalent to deleting the element 
        multiSet.setCount("def",0);

        //size() Method will return the number of all elements , Contains duplicate elements 
        System.out.println(multiSet.size());

        // Returns the number of repetitions of an element 
        System.out.println(multiSet.count("abc"));

        // Return similar map Of EntrySet
        System.out.println(multiSet.entrySet());

        // Return all elements without repetition 
        System.out.println(multiSet.elementSet());
        System.out.println("________________________________________________________________");

        // Acquisition iterator , Traverse 
        Iterator<String> iterator = multiSet.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

原网站

版权声明
本文为[amadeus_ liu2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061310311603.html