Collection:
list:有序,可重复
ArrayList: 底层是数组,查询快,增删慢,不安全,效率高
Vector: 底层是数组,查询快,增删慢,安全,效率低
LinkedList:底层是链表,查询慢,增删快,不安全,效率高
set;无序,唯一
HashSet:底层是哈希表,
A:底层数据结构是哈希表(是一个元素为链表的数组)
B:哈希表底层依赖两个方法:hashCode()和equals()
执行顺序:
首先比较哈希值是否相同
相同:继续执行equals()方法
返回true:元素重复了,不添加
返回false:直接把元素添加到集合
不同:就直接把元素添加到集合
C:如何保证元素唯一性的呢?
由hashCode()和equals()保证的
D:开发的时候,代码非常的简单,自动生成即可。
E:HashSet存储字符串并遍历
F:HashSet存储自定义对象并遍历(对象的成员变量值相同即为同一个元素)
LinkedHashSet
底层数据结构是链表和哈希表
由链表保证元素有序
由哈希表保证元素唯一
TreeSet: A:底层数据结构是红黑树(是一个自平衡的二叉树)
B:保证元素的排序方式
a:自然排序(元素具备比较性)
让元素所属的类实现Comparable接口
b:比较器排序(集合具备比较性)
让集合构造方法接收Comparator的实现类对象
Map(双列集合)
A:Map集合的数据结构仅仅针对键有效,与值无关。
B:存储的是键值对形式的元素,键唯一,值可重复。
HashMap
底层数据结构是哈希表。线程不安全,效率高
哈希表依赖两个方法:hashCode()和equals()
执行顺序:
首先判断hashCode()值是否相同
是:继续执行equals(),看其返回值
是true:说明元素重复,不添加
是false:就直接添加到集合
否:就直接添加到集合
最终:
自动生成hashCode()和equals()即可
LinkedHashMap
底层数据结构由链表和哈希表组成。
由链表保证元素有序。
由哈希表保证元素唯一。
Hashtable
底层数据结构是哈希表。线程安全,效率低
哈希表依赖两个方法:hashCode()和equals()
执行顺序:
首先判断hashCode()值是否相同
是:继续执行equals(),看其返回值
是true:说明元素重复,不添加
是false:就直接添加到集合
否:就直接添加到集合
最终:
自动生成hashCode()和equals()即可
TreeMap
底层数据结构是红黑树。(是一种自平衡的二叉树)
如何保证元素唯一性呢?
根据比较的返回值是否是0来决定
如何保证元素的排序呢?
两种方式
自然排序(元素具备比较性)
让元素所属的类实现Comparable接口
比较器排序(集合具备比较性)
让集合接收一个Comparator的实现类对象
到底使用那种集合(自己补齐)
看需求。
是否是键值对象形式:
是:Map
键是否需要排序:
是:TreeMap
否:HashMap
不知道,就使用HashMap。
否:Collection
元素是否唯一:
是:Set
元素是否需要排序:
是:TreeSet
否:HashSet
不知道,就使用HashSet
否:List
要安全吗:
是:Vector(其实我们也不用它,后面我们讲解了多线程以后,我在给你回顾用谁)
否:ArrayList或者LinkedList
增删多:LinkedList
查询多:ArrayList
不知道,就使用ArrayList
不知道,就使用ArrayList
Map和Collection的区别?
A:Map 存储的是键值对形式的元素,键唯一,值可以重复。
B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。
集合的常见方法及遍历方式
Collection:
add()
remove()
contains()
iterator()
size()
遍历:
普通for for(int i=0;i<list.size();i++)
增强for for(E s:list)
迭代器 Iterator<E> it=list.iterator();
while(it.hasNext()){
E s1=it.next();}
如果是集合嵌套就用增强for
|--List
get()
遍历:
普通for
|--Set
Map:
put()
remove()
containskey(),containsValue()
keySet()
get()
value()
entrySet()
size()
遍历:
根据键找值
Set<E> set=map.keySet();
for(E s:set){
E s1=map.get(s);}
根据键值对对象分别找键和值
Set<Map.Entry<K,V>> set=map.entrySet();
for(Map.Entry<K,V> s:set){
K s1=s.getKey();
V s2=s.getValue();}
如果是集合嵌套,那就用根据键找值
遍历实例:
Collection:
public class CollectionDemo2 {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<Student>();
Student s1=new Student("老大",27);
Student s2=new Student("老二",26);
Student s3=new Student("老三",25);
Student s4=new Student("老四",24);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//迭代器遍历
Iterator<Student> it=list.iterator();
while(it.hasNext())
{
Student s=it.next();
System.out.println(s); //由于Student类中重写了toString方法,打印的是一个Student数组
System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge());
}
System.out.println("-------------------------");
//普遍for遍历
for(int i=0;i<list.size();i++)
{
Student s=list.get(i);
System.out.println(s); //由于Student类中重写了toString方法,打印的是一个Student数组
System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge());
}
//增强for遍历
System.out.println("-------------------------");
for(Student s:list)
{
System.out.println(s); //由于Student类中重写了toString方法,打印的是一个Student数组
System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge());
}
}
}
集合嵌套:
//增强for遍历
for(ArrayList<Student> array:list)
{
for(Student s:array)
{
System.out.println(s); //由于Student类中重写了toString方法,打印的是一个Student数组
System.out.println("姓名:"+s.getName()+",年龄:"+s.getAge());
}
}
Map:
public class MapDemo2 {
public static void main(String[] args) {
Map<String ,Student> map=new HashMap<String,Student>();
Student t1=new Student("张三",27);
Student t2=new Student("李四",26);
Student t3=new Student("王五",25);
Student t4=new Student("孙六",24);
map.put("张家", t1);
map.put("李家", t2);
map.put("王家", t3);
map.put("孙家", t4);
Set<String> set1=map.keySet();
for(String key:set1)
{
Student s=map.get(key);
System.out.println("家族:"+key+",姓名:"+s.getName()+",年龄:"+s.getAge());
}
System.out.println("--------------------");
Set<Map.Entry<String,Student>> set2=map.entrySet();
for(Map.Entry<String,Student> str:set2)
{
String name=str.getKey();
Student stu=str.getValue();
System.out.println("家族:"+name+",姓名:"+stu.getName()+",年龄:"+stu.getAge());
}
System.out.println("--------------------");
//集合的嵌套遍历
Map<String ,HashMap<String,Student>> map2=new HashMap<String,HashMap<String,Student>>();
HashMap<String,Student> map3=new HashMap<String,Student>();
Student s1=new Student("老大",27);
Student s2=new Student("老二",26);
Student s3=new Student("老三",25);
Student s4=new Student("老四",24);
map3.put("张家", s1);
map3.put("李家", s2);
map3.put("王家", s3);
map3.put("孙家", s4);
map2.put("1", map3);
Set<String> set3=map2.keySet();
for(String s:set3)
{
HashMap<String,Student> hm=map2.get(s);
Set<String> set4=hm.keySet();
for(String key:set4)
{
Student st=map3.get(key);
System.out.println("家族:"+key+",姓名:"+st.getName()+",年龄:"+st.getAge());
}
}
}
} |
|