Collection |--List:元素是有序的,元素可以重复,因为该集合体系有索引 |--ArrayList:底层的数据结构使用的是数据结构。特点:查询速度快,但是增删稍慢,线程不相同。 |--LinkedList:底层使用的链表数据结构。特点:增删速度很快,查询稍慢,线程不相同。 |--Vector:底层是数据结构。线程同步,被ArrayList替代了,因为效率低! 注意:对于判断元素是否存在,以及删除等操作,依赖的方法是元素的equals方法! |--Set:元素是无序(存入和取出的顺序不一定一致),元素不可以重复。 |--HashSet:底层数据结构是哈希表。 HashSet是如何保证元素唯一性的呢? 是通过元素的两个方法,hashCode和equals来完成。 如果元素的hashCode值相同,才会判断equals是否为true. 如果元素的hashCode值不相同,不会调用equals方法。 注意:对于判断元素是否存在,以及删除等操作,依赖的方法是元素的hashCode和equals方法! |--TreeSet:可以对Set集合中的元素进行排序。 底层数据结构是二叉树。 保证元素唯一性的依据: compareTo方法return 0/1/-1; (原装比较器)TreeSet排序的第一种方式:让元素自身具备比较性。 元素要实现Comparable接口,覆盖compareTo方法。 这种方式也称为元素的自然顺序,或者元素默认顺序。
(比较器)TreeSet的第二中排序方式: 当元素自身不具备比较性时,或者具备的比较性不是所需要的。 这时就需要让集合自身具备比较性。 在集合初始化时,就有了比较方式。 1.Comparable接口是在java.lang类中的,而Comparator接口是在java.util类中的。 2.Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序, 所以,如想实现排序,就需要在集合外定义 Comparator 接口的方法或在集合内实现 Comparable 接口的方法。 |--Map |--Hashtable:底层是哈子表数据结构,不可以存入null键null值,该集合是线程同步的。jdk1.0效率低。 添加元素,如果出现添加时,那么后添加的值就会覆盖原有键的值,并put方法会返回被覆盖的值) 代码:(System.out.println(map.put("01","张三")) >>>> null; (System.out.println(map.put("01","李四")) >>>> 张三; |--HashMap:底层是哈子表数据结构,允许使用null键和null值,该集合是不同步的。jdk2.0效率高。 |--TreeMap:底层是二叉树数据结构,线程不同步,可以用于给map集合中的键进行排序。 Map集合的两种取出方式:(***---------->>>>>>>>map集合key不能为 int 类型,但是可以包装为 Integer 类型) 1.Set<k> keySet:将map中所有的键存入到Set集合,因为Set具备迭代器 所有可以迭代方式取出所有的键,在根据get方法,获取每一个键对应的值 Map集合的取出原理:将map集合转成set集合,在通过迭代器取出。 2.Set<Map.Entry<k,v>> entrySet: 将Map集合中的映射关系存入到了Set集合中,而这个关系的数据类型就是:Map.Entry Map.Entry其实Entry也是一个接口,它是Map接口中的一个内部接口。 和Set很像,其实Set底层就是使用了Map集合。
泛型:JDK_1.5版本以后出现新特性,用于解决安全问题,是一个类型安全机制。 好处: 1.将运行时期出现问题ClassCastException转移到了编译时期。 方便于程序员解决问题。让运行时期问题减少,安全! 2.避免了强制转换的麻烦! 问题1: 在使用java提供的对象时,什么地方定义泛型呢? 通常在集合框架定义泛型 只要见到<>就要定义泛型 其实<>就是用来接收类型的。 当使用集合时,将集合中要存储的数据类型作为参数传递到<>中集合。 问题2: 什么时候定义泛型类? 当类中要操作的 引用数据类型 不确定的时候, 早期定义Object来完成扩展,现在定义泛型来完成扩展。 泛型类定义的泛型,在整个类中有效,如果被方法使用, 那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了 为了让不同方法可以操作不同类型,而且类型还不确定。 那么可以将泛型定义在方法上。 特殊之处: 静态方法不可以访问类上定义的泛型。 如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。 接口的泛型 interface People<T>{ } 泛型限定 <T extends 父类> <? extends E>可以接受E类型或者E的子类型。————上限 <? super E> 可以接受E类型或者E的父类型。————下限
import java.util.*;
class Student implements Comparable
{
private int id;
private String name;
public int getId(){
return id;
}
public String getName(){
return name;
}
public Student(int id,String name){
this.id = id;
this.name = name;
}
//当直接打印Student时,或显示 id和name
public String toString(){
return id+"......"+name;
}
//如果用Hash表 装 数据就重写 hashCode和equals两方法
public int hashCode(){
return name.hashCode()+id*34;
}
public boolean equals(Object o){
if(!(o instanceof Student)){
//throw new RuntimeException("类型不匹配");
throw new ClassCastException("类型不匹配");
}
Student s = (Student)o;
return this.name.equals(s.name) && this.id==s.id;
}
//如果是二叉树 就得 重写 compareTo方法(那么就得实现Comparable接口)
public int compareTo(Object o){
if(!(o instanceof Student)){
//throw new RuntimeException("类型不匹配");
throw new ClassCastException("类型不匹配");
}
Student s = (Student)o;
int num = new Integer(this.id).compareTo(s.id);
if(num == 0)
return this.name.compareTo(s.name);
return num;
}
}
class MapTest
{
public static void main(String[] args)
{
//显示 map 与 list 嵌套
//showMap_and_List();
//显示 map 与 map 嵌套
showMap_and_Map();
}
//显示 map 与 list 嵌套
public static void showMap_and_List(){
HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
List<Student> first = new ArrayList<Student>();
List<Student> second = new ArrayList<Student>();
czbk.put("预热班",first);
czbk.put("就业班",second);
first.add(new Student(1, "张三"));
first.add(new Student(2, "李四"));
first.add(new Student(3, "王五"));
first.add(new Student(4, "赵六"));
second.add(new Student(1, "zhang"));
second.add(new Student(2, "li"));
second.add(new Student(3, "wang"));
second.add(new Student(4, "liu"));
Set<String> keySet = czbk.keySet();
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
String key = it.next();
List<Student> list = czbk.get(key);
getStudent(list);
}
}
public static void getStudent(List<Student> list){
Iterator<Student> it = list.iterator();
while(it.hasNext()){
Student stu = it.next();
System.out.println(stu);
}
}
//显示 map 与 map 嵌套
public static void showMap_and_Map(){
//map集合key不能为 int 类型,但是可以包装为 Integer 类型
HashMap<String,HashMap<Integer,String>> czbk = new HashMap<String,HashMap<Integer,String>>();
HashMap<Integer,String> first = new HashMap<Integer,String>();
HashMap<Integer,String> second = new HashMap<Integer,String>();
czbk.put("预热班",first);
czbk.put("就业班",second);
first.put(1, "张三");
first.put(2, "李四");
first.put(3, "王五");
first.put(4, "赵六");
second.put(1, "zhang");
second.put(2, "li");
second.put(3, "wang");
second.put(4, "liu");
Set<Map.Entry<String,HashMap<Integer,String>>> entrySet = czbk.entrySet();
Iterator<Map.Entry<String,HashMap<Integer,String>>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<String,HashMap<Integer,String>> me = it.next();
String key = me.getKey();
HashMap<Integer,String> hm = me.getValue();
System.out.println(key+"........"+hm);
getStudent1(hm);
}
}
public static void getStudent1(HashMap<Integer,String> hashMap){
Set<Map.Entry<Integer,String>> entrySet = hashMap.entrySet();
Iterator<Map.Entry<Integer,String>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<Integer,String> me = it.next();
int key = me.getKey();
String value = me.getValue();
System.out.println(key +"..........."+ value);
}
}
}
Iterator迭代器的两种形式,获取对象,向上,就看getStudent() 和 getStudent1();