處理集合Collection的Java開源工具包,Commons Collections 4.0 發布

jopen 11年前發布 | 30K 次閱讀 Apache Commons Collections

Apache Commons Collections是一個用來處理集合Collection的開源工具包,比如你可以用來將一個對象拷貝多份并存放到一個Bag對象中(這個看來沒有多大用處),得到兩個集合里相同的元素,刪除一個集合里的元素并返回刪除的元素,還有除了通過一個集合里的key得到value外,還可以通過value 得到key,也就是說這個集合里的value是唯一的,另外還可以將一個集合里的key和value值對調,得到一個集合里的某一key之后的另一個 key值等等。

官方網址是:http://commons.apache.org/collections/
使用示例:

/**

* 通過Iterator遍歷得到一個map里的key和value

*/

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);

}

}



/**

* 得到集合里按順序存放的key之后的某一Key

*/

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"));

}



/**

* 通過key得到value

* 通過value得到key

* 將map里的key和value對調

*/

private static void testTreeBidiMap(){

BidiMap bidi = new TreeBidiMap();

bidi.put("SIX", "6");

bidi.get("SIX");  // returns "6"

bidi.getKey("6");  // returns "SIX"

//     bidi.removeValue("6");  // removes the mapping

BidiMap inverse = bidi.inverseBidiMap();  // returns a map with keys and values swapped

System.out.println(inverse);

}



/**

* 移除集合某一元素并返回移除的元素

*/

private static void testUnboundedFifoBuffer(){

Buffer buffer = new UnboundedFifoBuffer();

buffer.add("ONE");

buffer.add("TWO");

buffer.add("THREE");

System.out.println(buffer.remove());  // removes and returns the next in order, "ONE" as this is a FIFO

System.out.println(buffer.remove());  // removes and returns the next in order, "TWO" as this is a FIFO

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);

}



/**

* 復制多個對象保存到Bag對象中

*/

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());

}

}



/**

* @param args

*/

public static void main(String[] args) {

//     testLinkedMap();

//     testTreeBidiMap();

testUnboundedFifoBuffer();

//     testHashBag();

//     testCollectionUtilsRetainAll();

}

Apache Commons Collections 4.0 發布,主要改進內容包括:

  • 要求 Java 5 支持
  • 源碼和二進制不兼容 3.x 版本
  • 使用了泛型以及 Java 5 的其他語言特性,如可變參數和迭代
  • 移除了廢棄的類和方法,這些類和方法已經被新的 JDK 所支持
  • 使用 java.util.Queue 替代 Buffer 接口
  • Added concept of split maps with respective interfaces Put / Get (see also package splitmap)
  • Added new Trie interface together with an implementation of a Patricia Trie

完整的改進記錄請看:

http://commons.apache.org/collections/changes-report.html
https://dist.apache.org/repos/dist/release/commons/collections/RELEASE-NOTES.txt

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!