当前位置:网站首页>guava: Multiset的使用
guava: Multiset的使用
2022-07-06 13:10: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中会存在重复元素,比如下面的"abc"
multiSet.add("abc");
multiSet.add("abc");
multiSet.add("def");
//添加元素时可以直接指定某个元素的重复次数
multiSet.add("ghi",4);
//可以按次数移除某个指定元素
multiSet.remove("ghi",1);
//可以直接设置指定元素的个数
multiSet.setCount("ghi",5);
//将元素个数设置为0,则相当于把该元素删除了
multiSet.setCount("def",0);
//size()方法会返回所有元素的个数,包含重复的元素
System.out.println(multiSet.size());
//返回某个元素的重复次数
System.out.println(multiSet.count("abc"));
//返回类似map的EntrySet
System.out.println(multiSet.entrySet());
//返回不重复的所有元素
System.out.println(multiSet.elementSet());
System.out.println("________________________________________________________________");
//获取迭代器,遍历
Iterator<String> iterator = multiSet.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
边栏推荐
- What's the best way to get TFS to output each project to its own directory?
- Thinking about agile development
- It's almost the new year, and my heart is lazy
- C # use Oracle stored procedure to obtain result set instance
- 面试官:Redis中有序集合的内部实现方式是什么?
- 【mysql】游标的基本使用
- Seven original sins of embedded development
- 968 edit distance
- Why does MySQL index fail? When do I use indexes?
- JS operation DOM element (I) -- six ways to obtain DOM nodes
猜你喜欢
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
Manifest of SAP ui5 framework json
Absolute primes (C language)
监控界的最强王者,没有之一!
Opencv learning example code 3.2.3 image binarization
Aiko ai Frontier promotion (7.6)
Z function (extended KMP)
互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
966 minimum path sum
随机推荐
Web开发小妙招:巧用ThreadLocal规避层层传值
VIM basic configuration and frequently used commands
Nodejs tutorial expressjs article quick start
爱可可AI前沿推介(7.6)
string的底层实现
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
Proxy and reverse proxy
在最长的距离二叉树结点
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
js中,字符串和数组互转(一)——字符串转为数组的方法
监控界的最强王者,没有之一!
3D人脸重建:从基础知识到识别/重建方法!
ICML 2022 | flowformer: task generic linear complexity transformer
[redis design and implementation] part I: summary of redis data structure and objects
Forward maximum matching method
每个程序员必须掌握的常用英语词汇(建议收藏)
One line by line explanation of the source code of anchor free series network yolox (a total of ten articles, you can change the network at will after reading it, if you won't complain to me)
el-table表格——sortable排序 & 出现小数、%时排序错乱
How do I remove duplicates from the list- How to remove duplicates from a list?
技术分享 | 抓包分析 TCP 协议