commons-collections使用介绍之Bag

简介:

在commons-collections中为我们提供了一些关于集合的实现,今天为大家介绍一下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 remove(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的更多内容,可以去官网查看相关的帮助文档。

目录
相关文章
Guava - Maps.difference
Guava - Maps.difference
433 0
|
22天前
Collection和Collections区别
Collection和Collections区别
|
7月前
|
Java
Guava Lists工具类
Guava Lists工具类
42 0
|
12月前
|
Java API
Why Java Collections Framework does not contain Tree and Graph
Why Java Collections Framework does not contain Tree and Graph
47 0
|
12月前
|
存储 Java 索引
Arrays Collections工具类
Arrays Collections工具类
commons-collections常用工具类
commons-collections常用工具类
79 0
|
API
Google Guava之Maps&Lists&Sets
日常开发中,使用最多的就是集合了,所以避免不了对集合的各种操作,本篇文章来看一下,Guava中都有哪些常用的集合操作的API可以简化我们的代码。
197 0
Google Guava之Maps&Lists&Sets
|
安全 容器
Collection和Collections的区别
Collection是一个接口,它是Set、List等容器的父接口;Collections是个一个工具类,提供了一系列的静态方法来辅助容器操作,这些方法包括对容器的搜索、排序、线程安全化等等。
1257 0
|
Java Apache API
Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用
Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用 Bag 是在 org.apache.commons.collections 包中定义的接口 ,也是集合的一种扩充工具类,当然结合用JDK中的map类进行相应的逻辑处理,也能实现Bag类的功能,但apache推出来肯定有它的原因和用处,知道有这么一个类了解它大概的用法,开发的时候真遇到这种情况,知道有这么个工具在你身边等着你用呢。
2692 0

热门文章

最新文章