Apache的commons组件的Map、Bag、Buffer等一些新奇的用法
2013-09-24 15:19
274 查看
1
2
package com.commons.components.collection; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.apache.commons.collections.Bag; import org.apache.commons.collections.Buffer; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.IterableMap; import org.apache.commons.collections.MapIterator; import org.apache.commons.collections.OrderedMap; import org.apache.commons.collections.bag.HashBag; import org.apache.commons.collections.buffer.UnboundedFifoBuffer; import org.apache.commons.collections.map.HashedMap; import org.apache.commons.collections.map.LinkedMap; /** * 测试包下的主要类 * * @author chaigw * */ public class MapTest { private static void testHashedMap() { IterableMap map = new HashedMap(); map.put("a", "1"); map.put("b", new Integer(2)); MapIterator it = map.mapIterator(); while (it.hasNext()) { Object key = it.next(); Object value = it.getValue(); System.out.println(">>key>>" + key + ">>value>>" + value); } } private static void testLinkedMap() { OrderedMap map = new LinkedMap(); map.put("FIVE", "5"); map.put("SIX", "6"); map.put("SEVEN", "7"); map.firstKey(); // returns "FIVE" map.nextKey("FIVE"); // returns "SIX" map.nextKey("SIX"); // returns "SEVEN" System.out.println(map.firstKey() + ">>" + map.nextKey("FIVE") + ">>" + map.nextKey("SIX")); } private static void testUnboundedFifoBuffer() { Buffer buffer = new UnboundedFifoBuffer(); buffer.add("ONE"); buffer.add("TWO"); buffer.add("THREE"); System.out.println(buffer); System.out.println(buffer.remove()); // removes and returns the next in System.out.println(buffer.remove()); // removes and returns the next in System.out.println(buffer); } /** * 测试两个集合中相同的数据 */ private static void testCollectionUtilsRetainAll() { List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add("2"); list1.add("3"); List<String> list2 = new ArrayList<String>(); list2.add("2"); list2.add("3"); list2.add("5"); Collection c = CollectionUtils.retainAll(list1, list2); System.out.println(c); } private static void testHashBag() { Bag bag = new HashBag(); bag.add("ONE", 6); // add 6 copies of "ONE" bag.remove("ONE", 2); // removes 2 copies of "ONE" System.out.println(bag.getCount("ONE")); // returns 4, the number of // copies in the bag (6 - 2) for (Iterator ite = bag.iterator(); ite.hasNext();) { System.out.println(ite.next()); } } public static void main(String[] args) { testHashedMap(); testLinkedMap(); testUnboundedFifoBuffer(); testCollectionUtilsRetainAll(); testHashBag(); } }
2
>>key>>a>>value>>1 >>key>>b>>value>>2 FIVE>>SIX>>SEVEN [ONE, TWO, THREE] ONE TWO [THREE] [2, 3] 4 ONE ONE ONE ONE
相关文章推荐
- Apache组件commons的Configuration包的用法实例
- Apache组件commons的MultiHashMap的一对多集合用法
- apache.commons.lang.StringUtils 的一些用法
- Apache组件commons的HttpClient的用法实例
- apache.commons.lang的EqualsBuilder和HashCodeBuilder用法
- org.apache.commons包 一些应用
- NoClassDefFoundError: org/apache/commons/collections/map/LRUMap
- java中Set,Map,Stack一些简单用法
- org.apache.commons.beanutils.BeanUtils用法
- React-Native中一些组件的用法(二)
- com.sun.org.apache.commons.beanutils.BeanUtils的用法简介
- java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap 解决方法 .
- Bag集合工具类(apache-commons-collections3.2工具包)在java中的使用
- 本地保存List(Map(String, String))数据及JSON的一些用法
- Apache组件commons的Codec包的使用实例
- org.apache.commons.lang.builder包下的一些常用类的研究与应用
- java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap 解决方法
- 对于org.apache.commons.lang.StringUtils 一些总结
- java.lang.ClassNotFoundException: org.apache.commons.collections.map.ListOrderedMap解决方法
- spring整合hibernate出现NoClassDefFoundError: org/apache/commons/collections/map/LRUMap