Java集合类相当于一个容器,包括Collection接口,List集合,Set集合,Map集合。
一Collection接口是List接口和Set接口的父接口,通常情况下不被直接使用。当然他还有一些通用方法。对List接口和Set接口也适用。
二 List集合
分为ArrayList集合和LinkedList集合。
List<String> l=new ArrayList<String>();//利用ArrayList类实例化List集合
List<String> l2=new LinkedList<String>();//利用LinkedList类实例化List集合
List接口提供的适合于自身的常用方法均与索引有关。List集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象。
ArrayList 实现了List接口,由ArrayList类实现的List集合采用数组结构保存对象。
优点:如果经常需要根据索引位置访问集合中的对象,使用由ArrayList类实现的List集合的效率比较好。
缺点:向指定位置插入对象和删除指定索引位置对象的速度较慢。
LinkedList 实现了List接口,由LinkedList类实现的List集合采用链表结构保存对象。
优点:便于向集合中中插入和删除对象。
缺点:随机访问对象的速度较慢。
三 Set集合
分为HashSet集合和TreeSet集合
Set集合为集合类型,集是最简单的一种集合,存放于集合中的对象不按特定方式排序,只是简单的把对象加入集合中。对集中存放的对象的访问和操作是通过对象的引用进行的,所以在集合中不能存放重复对象。
Set<String> s1=new HashSet<String>();//利用HashSet类实例化Set集合
Set<String> s2=new TreeSet<String>();//利用TreeSet类实例化Set集合
HashSet
优点:能够快速定位集合中的元素,要求集合中的对象必须唯一,集合中的对象时无序的。这里的无序并不是完全的无序,它只是不像List集合那样按对象的插入顺序取保存对象。
TreeSet
优点:是有序的,从而保证在遍历集合时按照递增的顺序获得对象。遍历对象时可能按照自然顺序递增排列,所以存入由TreeSet类实现的Set集合的对象时必须实现Comparable接口。
也可能按照制定的比较器递增排列,即可以通过比较器对由TreeSet类实现的Set集合中的对象进行排序。
四 Map集合 <key,Value>
分为HashMap和TreeMap,如果希望在遍历集合时是有序的,则应该使用TreeMap类,否则使用HashMap类,HashMap类对于天津和删除映射关系更高效。
Map集合为映射类型,映射与集和列表有明显的区别,映射中的每个对象都是成对存在的。
Map<Integer,String>m1=new HashMap<>()://利用HashMap类实例化Map集合
Map<Integer,String>m2=new TreeMap<>();//利用TreeMap类实例化Map集合
映射中存储的每一个对象都有一个相应的键(key)对象,在检索对象时必须通过相应的键对象来获取(value)对象。类似于在字典中查找单词一样,所以就要求键对象必须是唯一的
五 实例分析
public class StudentInformation {
private String name;
private long id;
public StudentInformation(String name, long id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
public static void main(String[] args) {
System.out.println("开始");
StudentInformation s1 = new StudentInformation("lzl", 20171831);
StudentInformation s2 = new StudentInformation("cn", 20171770);
StudentInformation s3 = new StudentInformation("mxy", 20171772);
Map<Number, StudentInformation> map = new HashMap<Number, StudentInformation>();
map.put(s1.getId(), s1);
map.put(s2.getId(), s2);
map.put(s3.getId(), s3);
System.out.println("由HashMap类实现的Map集合,无序:");
for (Iterator<Number> it = map.keySet().iterator(); it.hasNext();) {
StudentInformation s = map.get(it.next());
System.out.println(s.getId() + " " + s.getName());
}
System.out.println("由TreeMap类实现的Map集合,键对象升序:");
TreeMap<Number, StudentInformation> treeMap = new TreeMap<Number, StudentInformation>();
treeMap.putAll(map);
for(Iterator<Number> it=treeMap.keySet().iterator();it.hasNext();) {
StudentInformation s=treeMap.get(it.next());
System.out.println(s.getId()+" "+s.getName());
}
System.out.println("由TreeMap类实现的Map集合,键对象降序:");
TreeMap<Number, StudentInformation> treeMap2 = new TreeMap<Number, StudentInformation>(Collections.reverseOrder());
treeMap2.putAll(map);
for (Iterator<Number> it = treeMap2.keySet().iterator(); it.hasNext();) {
StudentInformation s = treeMap2.get(it.next());
System.out.println(s.getId() + " " + s.getName());
}
}
结果显示:
开始
由HashMap类实现的Map集合,无序:
20171831 lzl
20171770 cn
20171772 mxy
由TreeMap类实现的Map集合,键对象升序:
20171770 cn
20171772 mxy
20171831 lzl
由TreeMap类实现的Map集合,键对象降序:
20171831 lzl
20171772 mxy
20171770 cn