Java Collection更有效管理Elements
来源:互联网 发布:清华同方知网数据库 编辑:程序博客网 时间:2024/05/09 19:43
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>Sets在Java Collection结构中,一个set就是众多元素中的一个collection,它确定了含有等同元素的精确的set模型,Set界面拓展了collention界面,意思就是说你可以在set中增加Object、删除object以及重新迭代等等。Set界面增加了两种名称均为排列的方法,而且可以把一个set转换成一列objects。SortedSetsSortedSets是实现按从小到大顺序排列元素这一迭代过程的set。Set中的元素都按自然顺序或者比较法进行分类。ComparatorsComparators是定义比较函数或等函数的界面,这样运行它的object结果是实现比较或等价功能,Comparators被传递给分类法来控制众多元素的分类程序。Maps与Set不同,Map并不是由Collection生成,相反,它提供了用某些键输入、输出值的界面,与java.util.Hashtable非常类似。Map是包含一列键/值对的对象,Map不包含复制键,而且每个键也只能有一个值,Map可以实现恢复一套键、一组值以及一系列mapping的功能。Sorting对于collection结构有两种基本的分类方式可以采用Collection类中两种静态法的任意一种运行列表界面来对Object进行分类。一种方法是获得运行比较界面的列表参数,另一种方法就是获得列表参数、比较参数并把采用比较对象的列表元素进行分类。还可以把比较界面功能加到Collention类中,在类中加入了比较法,得到的结果就是从第一个参数中减去第二个参数,然后把Collection对象传递给运行比较界面的对象。 表A中的例子证明了对MySortedMapComparator对象进行分类的比较界面。Listing Aclass MySortedMapComparatorimplements Comparator{public MySortedMapComparator(){}public int compare(Object o1, Object o2){return ((MySortedMapImpl)o1).compareTo((MySortedMapImpl)o2);}public boolean equals(Object obj){if (!(obj instanceof MySortedMapComparator))return false;return false;}}SortedMapsSortedMaps就是能够提供按从小到大排列元素功能的map,这些元素都按自然顺序或者比较法进行分类。见表B中的例子。Listing Bimport java.util.*;public class MySortedMapImplimplements SortedMap, Comparable{private java.util.HashMap hashMap = new java.util.HashMap();private Comparator comparator = null;public MySortedMapImpl(){this.comparator = new MySortedMapComparator();}public MySortedMapImpl(HashMap hashMap){this.hashMap = hashMap;this.comparator = new MySortedMapComparator();}public void clear(){hashMap.clear();}public boolean containsKey(Object key){return hashMap.containsKey(key);}public boolean containsValue(Object value){return hashMap.containsValue(value);}public Set entrySet(){return hashMap.entrySet();}public boolean equals(Object o){return hashMap.equals(o);}public Object get(Object key){return hashMap.get(key);}public int hashCode(){return hashMap.hashCode();}public boolean isEmpty(){return hashMap.isEmpty();}public Set keySet(){return hashMap.keySet();}public Object put(Object key, Object value){return hashMap.put(key, value);}public void putAll(Map t){hashMap.putAll(t);}public Object remove(Object key){return hashMap.remove(key);}public int size(){return hashMap.size();}public Collection values(){return hashMap.values();}public Comparator comparator(){return this.comparator;}public java.util.SortedMap subMap(Object fromKey, Object toKey){if (toKey == null)throw new NullPointerException("toKey == null");if (!(toKey instanceof MySortedMapImpl))throw new ClassCastException("toKey not a comparable object");HashMap aHashMap = new HashMap();Iterator keys = keySet().iterator();while (keys.hasNext()){Object key = keys.next();if ((comparator.compare(key, fromKey) >= 0) &&(comparator.compare(key, toKey) < 0)){Object value = hashMap.get(key);aHashMap.put(key, value);}}if (aHashMap.isEmpty())throw new IllegalArgumentException("toKey is out of range.");MySortedMapImpl aSubMap = new MySortedMapImpl(aHashMap);return aSubMap;}public java.util.SortedMap headMap(Object toKey){if (toKey == null)throw new NullPointerException("toKey == null");if (!(toKey instanceof MySortedMapImpl))throw new ClassCastException("toKey not a comparable object");HashMap aHashMap = new HashMap();Iterator keys = keySet().iterator();while (keys.hasNext()){Object key = keys.next();if (comparator.compare(key, toKey) < 0){Object value = hashMap.get(key);aHashMap.put(key, value);}}if (aHashMap.isEmpty())throw new IllegalArgumentException("toKey is out of range.");MySortedMapImpl aHeadMap = new MySortedMapImpl(aHashMap);return aHeadMap;}public java.util.SortedMap tailMap(Object fromKey){if (fromKey == null)throw new NullPointerException("fromKey == null");if (!(fromKey instanceof MySortedMapImpl))throw new ClassCastException("fromKey not a comparable object");HashMap aHashMap = new HashMap();Iterator keys = keySet().iterator();while (keys.hasNext()){Object key = keys.next();if (comparator.compare(key, fromKey) >= 0){Object value = hashMap.get(key);aHashMap.put(key, value);}}if (aHashMap.isEmpty())throw new IllegalArgumentException("fromKey is out of range.");MySortedMapImpl aTailMap = new MySortedMapImpl(aHashMap);return aTailMap;}public Object firstKey(){if (isEmpty())throw new NoSuchElementException("Map is empty");return keySet().iterator().next();}public Object lastKey(){if (isEmpty())throw new NoSuchElementException("Map is empty");Object key = null;Iterator keys = keySet().iterator();while (keys.hasNext()){key = keys.next();}return key;}public int compareTo(Object o){// simple compares shown for demo purposesif (!(o instanceof MySortedMapImpl))return -1;if (hashMap.equals(((MySortedMapImpl)o).hashMap) == false)return 1;if (comparator.equals(((MySortedMapImpl)o).comparator()) == false)return 1;return 0;}}public class SortedMapExample{public static void main(String args[]){MySortedMapImpl aSortedMap = new MySortedMapImpl();aSortedMap.put("1", "ItemOne");aSortedMap.put("2", "ItemTwo");aSortedMap.put("3", "ItemThree");Iterator keys = aSortedMap.keySet().iterator();while (keys.hasNext()){Object key = keys.next();Object value = aSortedMap.get(key);System.out.println("key: " key.toString() ", Value: " value.toString());}aSortedMap.remove(aSortedMap.lastKey());keys = aSortedMap.keySet().iterator();while (keys.hasNext()){Object key = keys.next();Object value = aSortedMap.get(key);System.out.println("key: " key.toString() ", Value: " value.toString());}}}Sorting it all outJava Collection结构为表示分类集合和未分类集合的核心Java APIs增加了兼容的标准API。因为Collection结构的API都是相互兼容的,因此一旦学会了结构中的一部分,就会理解很多概念。这样就会让你少走很多弯路。下一篇文章中将要开始讨论Java平台的输入/输出系统。