commons-collections使用介绍之Bag HashBag
2016-08-10 18:55
357 查看
在commons-collections中为我们提供了一些关于集合的实现,今天为大家介绍一下
假设你有一个包,包含{a, a, b, c}。调用getCount(a)方法将返回2,调用uniqueset()方法将返回{a, b, c}。
使用示例:
运行结果:
使用示例:
运行结果:
关于Bag的更多内容,可以去官网查看相关的帮助文档。
Bag相关类的用法。
概述
Bag继承自
Collection接口,定义了一个集合,该集合会记录对象在集合中出现的次数。
假设你有一个包,包含{a, a, b, c}。调用getCount(a)方法将返回2,调用uniqueset()方法将返回{a, b, c}。
API
方法摘要
返回值 | 方法名 | 说明 |
---|---|---|
boolean | add(Object object) | (违反)将指定的对象添加到该包中 |
boolean | add(Object object, int nCopies) | 将指定数量的对象添加到该包中 |
boolean | containsAll(Collection coll) | 判断包中是否含有集合中所有元素 |
int | getCount(Object object) | 获得对象在包中的数量 |
Iterator | iterator() | 获得包元素迭代器 |
boolean | r 4000 emove(Object object) | 移除包中元素 |
boolean | remove(Object object, int nCopies) | 移除指定数量的包中的元素 |
boolean | removeAll(Collection coll) | 移除包中包含的集合中的所有元素 |
boolean | retainAll(Collection coll) | 删除包中不在集合中的元素 |
int | size() | 获得包中元素个数 |
Set | uniqueSet() | 获得包中唯一元素集合(去重) |
HashBag
HashBag使用
HashMap作为数据存储,是一个标准的
Bag实现。
使用示例:
package com.gujin.collections.bag; import java.util.Iterator; import org.apache.commons.collections.bag.HashBag; import org.junit.Test; public class HashBagBagTest { @Test public void test() { HashBag hashBag = new HashBag(); String s1 = "s1"; String s2 = "s2"; hashBag.add(s1); hashBag.add(s1); hashBag.add(s2, 3); // 获得包中元素迭代器 Iterator<?> iterator = hashBag.iterator(); System.out.println("包中元素为:"); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("包中元素个数为:" + hashBag.size()); System.out.println("包中entity1个数为:" + hashBag.getCount(s1)); System.out.println("去重后个数为:" + hashBag.uniqueSet().size()); } }
运行结果:
包中元素为: s1 s1 s2 s2 s2 包中元素个数为:5 包中entity1个数为:2 去重后个数为:2
TreeBag
TreeBag使用
TreeMap作为数据存储,用法与
HashBag类似,只是
TreeBag会使用自然顺序对元素进行排序。
使用示例:
package com.gujin.collections.bag; import java.util.Iterator; import org.apache.commons.collections.bag.TreeBag; import org.junit.Test; public class TreeBagBagTest { @Test public void test() { TreeBag hashBag = new TreeBag(); String s1 = "s1"; String s2 = "s2"; String s3 = "s3"; hashBag.add(s3); hashBag.add(s1); hashBag.add(s2); // 获得包中元素迭代器 Iterator<?> iterator = hashBag.iterator(); System.out.println("包中元素为:"); while (iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println("包中元素个数为:" + hashBag.size()); System.out.println("包中entity1个数为:" + hashBag.getCount(s1)); System.out.println("去重后个数为:" + hashBag.uniqueSet().size()); } }
运行结果:
包中元素为: s1 s2 s3 包中元素个数为:3 包中entity1个数为:1 去重后个数为:3
关于Bag的更多内容,可以去官网查看相关的帮助文档。
相关文章推荐
- commons-collections使用介绍之List
- 第五十七篇:commons-collections使用介绍之Bag
- commons-collections使用介绍之Bag
- commons-collections使用介绍之Map
- 第五十九篇:commons-collections使用介绍之Map
- 第五十八篇:commons-collections使用介绍之List
- Google Guava Collections 使用介绍
- commons_dbutils的介绍与使用
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- commons-fileupload的详细介绍与使用
- Google Guava Collections 使用介绍
- commons-fileupload的详细介绍与使用
- apache commons collections CollectionUtils工具类简单使用
- Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用
- 第三章 Backbonejs中的Collections实践 上一节介绍了model的使用,model算是对现实中某一物体的抽象,比如你可以定义一本书的model,具有书名(title)还有书页(pag
- org.apache.commons.collectionsJDK中不是已经有了Java集合框架了吗,为什么还要使用Apache的集合呢?
- commons_dbutils的介绍与使用
- 关于使用工具类org.apache.commons.collections.ListUtils合并List的问题