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平台的输入/输出系统。
闽公网安备 35060202000074号