Collection
List类
Java API中所用的集合类,都是实现了Collection接口,他的一个类继承结构如下:
Collection<--List<--Vector
Collection<--List<--ArrayList
Collection<--List<--LinkedList
Collection<--Set<--HashSet
Collection<--Set<--HashSet<--LinkedHashSet
Collection<--Set<--SortedSet<--TreeSet
Vector :
基于Array的List,其实就是封装了Array所不具备的一些功能方便我们使用,它不可能不受Array的限制。性能也就不可能超越Array。所以,在可能的情况下,我们要多运用Array。
ArrayList:
同Vector一样是一个基于Array上的链表,他就像我们在数据结构中说到的线性表。他在内存中的存储是顺序存储的,每个元素都是相连的,当删除一个元素时,其后所有的元素要前移, 当添加一个元素时,其后所有的元素要后移。他的缺点就是删除、添加效率慢。优点是遍历快。
LinkedList:
LinkedList不同于前面两种List,它不是基于Array的,所以不受Array性能的限制。它每一个节点(Node)都包含两方面的内容:1.节点本身的数据(data);2.下一个节点的信息(nextNode)。所以当对LinkedList做添加,删除动作的时候就不用像基于Array的List一样,必须进行大量的数据移动。只要更改nextNode的相关信息就可以实现了。所以优点是插入删除快,但是遍历慢。
这里以ArrayList为例说明用法:
public class TestArrayList {
public static void main(String[] args) {
//创建集合list1,和list2.集合用来存储字符串类型。
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
//将输入的字符串添加进集合list1中。
Scanner scanner = new Scanner(System.in);
for(int i = 0; i<10; i++){
String in = scanner.next();
list1.add(in);
}
//将“添加”字符串天加到集合位置为1 的位置。
list1.add(1, "添加");
//将list1 的内容全部添加进list2 中。
list2.addAll(list1);
//遍历list1 集合。
for (int i = 0; i < list1.size(); i++) {
System.out.print(list1.get(i));
}
System.out.println();
//for each 循环遍历list2
for(String s:list2){
System.out.println(s);
}
}
}
Set类
HashSet:
HashSet是无序的。虽然Set同List都实现了Collection接口,但是他们的实现方式却大不一样。List基本上都是以Array为基础。但是Set则是在HashMap的基础上来实现的,这个就是Set和List的根本区别。HashSet的存储方式是把HashMap中的Key作为Set的对应存储项。看看HashSet的add(Object obj)方法的实现就可以一目了然了。
public boolean add(Object obj)
{
return map.put(obj, PRESENT) == null;
}
这个也是为什么在Set中不能像在List中一样有重复的项的根本原因,因为HashMap的key是不能有重复的。
TreeSet:
SortedSet的子类,它不同于HashSet的根本就是TreeSet是有序的。它是通过SortedMap来实现的。
public class TestHashSet {
public static void main(String[] args) {
//新建一个哈希表
HashSet<Integer> set = new HashSet<>();
//建立一个随机对象。
Random random = new Random();
int count = 0;
while (set.size()<10) {
//随机产生的都是两位数。
int i = random.nextInt(90)+10;
System.out.println(count++ +"随机得到的数据为:"+i);
//将产生的随机数添加进集合中。
set.add(i);
}
System.out.println("已经放进去了"+set.size());
//实现Set的遍历。
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Map
HashMap类
HashMap类是基于key和value键值的。key和value可以为null。
HashTable类
HashTable类也是基于key和value键值的。key和value不同的是它们不可以为null。
public class TestHashMap {
public static void main(String[] args) {
//创建hashmap对象。
HashMap<String,Student> hashmap = new HashMap();
//创建学生对象。
Student zhangsan = new Student("张三");
Student lisi = new Student("李四");
Student wangwu = new Student("王五");
//将对象放入hashmap中。
hashmap.put("1", zhangsan);
hashmap.put("2", lisi);
hashmap.put("3", wangwu);
//输出键值key为1的学生对象的名字。
System.out.println(hashmap.get("1").getName());
//实现hashmap的遍历。
//获得所有键值,将其存储在String类型的集合中。
Set<String> keys = hashmap.keySet();
//利用集合Set的迭代器实现遍历。
Iterator<String> it = keys.iterator();
while(it.hasNext()){
String key = it.next();
System.out.println(hashmap.get(key).getName());
}
}
}