Apache集合处理工具类的使用

Commons Collections增强了Java Collections Framework。 它提供了几个功能,使收集处理变得容易。 它提供了许多新的接口,实现和实用程序。 Commons Collections的主要功能如下

Bag - Bag界面简化了每个对象具有多个副本的集合。

BidiMap - BidiMap接口提供双向映射,可用于使用值使用键或键查找值。

MapIterator - MapIterator接口提供简单而容易的迭代迭代。

Transforming Decorators - 转换装饰器可以在将集合添加到集合时更改集合的每个对象。

Composite Collections - 在需要统一处理多个集合的情况下使用复合集合。

Ordered Map - 有序地图保留添加元素的顺序。

Ordered Set - 有序集保留了添加元素的顺序。

Reference map - 参考图允许在密切控制下对键/值进行垃圾收集。

Comparator implementations - 可以使用许多Comparator实现。

Iterator implementations - 许多Iterator实现都可用。

Adapter Classes - 适配器类可用于将数组和枚举转换为集合。

Utilities - 实用程序可用于测试测试或创建集合的典型集合论属性,例如union,intersection。 支持关闭。

Commons Collections - Bag
Bag定义了一个集合,用于计算对象在集合中出现的次数。 例如,如果Bag包含{a,a,b,c},则getCount(“a”)将返回2,而uniqueSet()将返回唯一值。

import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
public class BagTester {
   
   public static void main(String[] args) {
   
      Bag<String> bag = new HashBag<>();
      //add "a" two times to the bag.
      bag.add("a" , 2);
      //add "b" one time to the bag.
      bag.add("b");
      //add "c" one time to the bag.
      bag.add("c");
      //add "d" three times to the bag.
      bag.add("d",3);
      //get the count of "d" present in bag.
      System.out.println("d is present " + bag.getCount("d") + " times.");
      System.out.println("bag: " +bag);
      //get the set of unique values from the bag
      System.out.println("Unique Set: " +bag.uniqueSet());
      //remove 2 occurrences of "d" from the bag
      bag.remove("d",2);
      System.out.println("2 occurences of d removed from bag: " +bag);
      System.out.println("d is present " + bag.getCount("d") + " times.");
      System.out.println("bag: " +bag);
      System.out.println("Unique Set: " +bag.uniqueSet());
   }
}

它将打印以下结果:

d is present 3 times.
bag: [2:a,1:b,1:c,3:d]
Unique Set: [a, b, c, d]
2 occurences of d removed from bag: [2:a,1:b,1:c,1:d]
d is present 1 times. bag: [2:a,1:b,1:c,1:d] Unique Set: [a, b, c, d]

Commons Collections - BidiMap
使用双向映射,可以使用值查找键,并且可以使用键轻松查找值。

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;
public class BidiMapTester {
   
   public static void main(String[] args) {
   
      BidiMap<String, String> bidi = new TreeBidiMap<>();
      bidi.put("One", "1");
      bidi.put("Two", "2");
      bidi.put("Three", "3")<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了许多集合类,每个类都有自己的拷贝方法。如果你想要一个通用的集合拷贝工具类,你可以自己编写一个或者使用Apache Commons Collections库中的CollectionUtils类。 下面是一个示例的集合拷贝工具类,它基于Java的原生集合类实现: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CollectionCopyUtils { public static <T> List<T> copyList(List<T> sourceList) { if (sourceList == null) { return null; } List<T> copyList = new ArrayList<>(sourceList.size()); for (T item : sourceList) { copyList.add(item); } return copyList; } public static <K, V> Map<K, V> copyMap(Map<K, V> sourceMap) { if (sourceMap == null) { return null; } Map<K, V> copyMap = new HashMap<>(sourceMap.size()); for (Map.Entry<K, V> entry : sourceMap.entrySet()) { copyMap.put(entry.getKey(), entry.getValue()); } return copyMap; } } ``` 使用示例: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) { // 测试List拷贝 List<String> sourceList = new ArrayList<>(); sourceList.add("A"); sourceList.add("B"); sourceList.add("C"); List<String> copiedList = CollectionCopyUtils.copyList(sourceList); System.out.println("Copied List: " + copiedList); // 测试Map拷贝 Map<Integer, String> sourceMap = new HashMap<>(); sourceMap.put(1, "One"); sourceMap.put(2, "Two"); sourceMap.put(3, "Three"); Map<Integer, String> copiedMap = CollectionCopyUtils.copyMap(sourceMap); System.out.println("Copied Map: " + copiedMap); } } ``` 这个示例工具类提供了copyList和copyMap方法,分别用于拷贝List和Map集合。你可以根据自己的需要进行相应的修改和扩展。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值