Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1504529
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 23:15:04

一起学习
Sets 在Java Collection结构中,一个set就是众多元素中的一个collection,它确定了含有等同元素的精确的set模型,Set界面拓展了collention界面,意思就是说你可以在set中增加Object、删除object以及重新迭代等等。Set界面增加了两种名称均为排列的方法,而且可以把一个set转换成一列objects。 SortedSets SortedSets是实现按从小到大顺序排列元素这一迭代过程的set。Set中的元素都按自然顺序或者比较法进行分类。 Comparators Comparators是定义比较函数或等函数的界面,这样运行它的object结果是实现比较或等价功能,Comparators被传递给分类法来控制众多元素的分类程序。 Maps 与Set不同,Map并不是由Collection生成,相反,它提供了用某些键输入、输出值的界面,与java.util.Hashtable非常类似。 Map是包含一列键/值对的对象,Map不包含复制键,而且每个键也只能有一个值,Map可以实现恢复一套键、一组值以及一系列mapping的功能。 Sorting 对于collection结构有两种基本的分类方式 可以采用Collection类中两种静态法的任意一种运行列表界面来对Object进行分类。一种方法是获得运行比较界面的列表参数,另一种方法就是获得列表参数、比较参数并把采用比较对象的列表元素进行分类。 还可以把比较界面功能加到Collention类中,在类中加入了比较法,得到的结果就是从第一个参数中减去第二个参数,然后把Collection对象传递给运行比较界面的对象。 表A中的例子证明了对MySortedMapComparator对象进行分类的比较界面。 Listing A class MySortedMapComparator implements 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; } } SortedMaps SortedMaps就是能够提供按从小到大排列元素功能的map,这些元素都按自然顺序或者比较法进行分类。见表B中的例子。 Listing B import java.util.*; public class MySortedMapImpl implements 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 purposes if (!(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 out Java Collection结构为表示分类集合和未分类集合的核心Java APIs增加了兼容的标准API。因为Collection结构的API都是相互兼容的,因此一旦学会了结构中的一部分,就会理解很多概念。这样就会让你少走很多弯路。下一篇文章中将要开始讨论Java平台的输入/输出系统。 下载本文示例代码


Java Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理ElementsJava Collection更有效管理Elements
阅读(228) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~