List 集合:
- 简介:List借口继承了Collection 接口,List接口还定义了以下两个重要的方法。get(int index) ,set(int index , Object obj)
- List 接口的常用实现类有 ArrayList 与 LinkedList .
- ArrayList 类实现了可变的数组。优点:允许保存所有元素,可根据索引位置对几何快速的随机访问。缺点是想知道的索引位置插入对象或删除对象的速度比较慢。
- LinkedList 类采用链表结果保存对象。优点:插入和删除对象效率比List集合的效率高。缺点是读取效率较低。
Set集合:
- 简介:由Set接口和Set接口的实现类组成,继承了Collection接口。
- Set接口常用的实现类有 HashSet 类 与 TreeSet 类。
- HashSet 类实现 Set 接口,由哈希表(实际上一个 HashMap 实例)支持。它不保证Set的迭代顺序。允许使用null字符串
- TreeSet 实现了Set接口,还是先了 java.util.SortedSet 接口,遍历时按照自然顺序递增排序,也可以按照比较器递增排序
package gather; import java.util.Iterator; import java.util.TreeSet; //TreeSet类实现的Set集合必须实现Comparable接口 public class Set implements Comparable<Object>{ String name ; long id; public Set(String name ,long id){ this.id = id; this.name = name; } @Override public int compareTo(Object o) { Set set = (Set)o; int result = id > set.id ? 1:(id == set.id ? 0:1); return result; } 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) { Set set = new Set("zwh",12); Set set1 = new Set("zwh1",112); Set set2 = new Set("zwh2",212); Set set3 = new Set("zwh3",312); TreeSet<Set> tree = new TreeSet<>(); tree.add(set); tree.add(set1); tree.add(set2); tree.add(set3); Iterator<Set> it = tree.iterator();//Set 集合中所有对象的迭代器 while(it.hasNext()){ Set set4 = (Set)it.next(); System.out.println(set4.getId()+""+set4.getName()); } //截取排在set2对象之前的对象headSet() it = tree.headSet(set2).iterator(); while(it.hasNext()){ Set set4 = (Set)it.next(); System.out.println(set4.getId()+""+set4.getName()); } //截取排在set与set1之间的对象 it = tree.subSet(set,set2).iterator(); while(it.hasNext()){ Set set4 = (Set)it.next(); System.out.println(set4.getId()+""+set4.getName()); } } }
Map集合:
- Map 接口常用的实现类有 HashMap 和 TreeMap。
- HashMap 类实现的Map集合添加和删除映射关系效率更高。TreeMap 中的映射关系存在一定的顺序,不允许存空的对象。