------Java培训、Android培训、iOS培训、.Net培训、期待与您交流------
1.Map双列集合的概述
Map双列集合
Map: 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
Map集合的数据结构只和键有关系和值没有关系.
Map集合和Collection集合集合的区别:
Map集合是双列集合,而Collection的集合是单列集合
Map集合的数据结构只对键有效,而Collection集合的数据结构针对存储的的元素有效
Map集合的键是唯一性,而Collection集合中的Set集合中的元素是唯一的
Map就像是夫妻对 , Collection就是光棍
Map集合的功能概述:
a: 添加
V put(K key, V value): 添加元素
b: 删除
void clear(): 删除
V remove(Object key): 根据键进行删除
c: 获取
Set> entrySet()
V get(Object key): 根据键获取值
Set keySet(): 获取所有的键的一个Set集合
Collection values(): 获取所有的值
d: 判断
boolean containsKey(Object key): 判断是否包含这个key
boolean containsValue(Object value): 判断是否包含对应的值
boolean isEmpty(): 判断集合是否为空
e: 长度
int size(): 长度
2. 遍历Map集合
遍历Map集合:
(1).根据键获取值
a: 获取所有的键的一个集合
b: 遍历上边的集合,获取每一个键
c: 根据当前遍历的键获取对应的值
(2)通过 Set> entrySet(): 返回一个键值对的Set集合
比喻:
entrySet获取其实就相当于所有的结婚证的集合
Map集合的第二种遍历: 根据键值对儿对象获取对应的键和值
a: 通过entrySet获取所有的键值对儿对应的Set集合
b: 遍历键值对儿对应的Set集合,获取每一个键值对儿对象
c: 根据键值对儿对象对应的
K getKey()返回与此项对应的键。
V getValue()返回与此项对应的值。
代码实现为:
public class HashMapTest {
public static void main(String[] args) {
//创建Map集合
Map hm = new HashMap();
//添加对象(明星夫妻吧)
hm.put("黄晓明", "杨颖");
hm.put("邓超", "孙俪");
hm.put("吴奇隆", "刘诗诗");
//使用第一种遍历
//获取所有的键对应的Set集合
Set keys = hm.keySet();
//遍历
for (String key : keys) {
//根据键获取值
String value = hm.get(key);
//输出
System.out.println(key + "----" + value);
}
System.out.println("--------------------");
//使用第二种遍历
//返回一个键值对的Set集合
Set> en = hm.entrySet();
//遍历键值对
for (Entry e : en) {
//获取键
String key = e.getKey();
//获取值
String value = e.getValue();
//输出
System.out.println(key + "----" + value);
}
}
}
3. 使用HashMap存储元素,键是Student类型,值是String类型
Student: 我们认为姓名和年龄都是相同的时候,认为是同一个对象
Map集合的数据结构只和键有关系,而HashMap集合的数据结构是哈希表,哈希表要保证元素的唯一性,依赖于两个方法
一个是hashCode方法,一个是equals方法.
HashMap底层的数据结构是哈希表. Map集合的数据结构只针对键有效和值没有关系
线程不安全 , 效率高
并允许使用 null 值和 null 键
Hashtable 底层的数据结构是哈希表
线程安全,效率低
不允许使用null 值和null 键
LinkedHashMap: 底层是链表和哈希表 , 有序,并且唯一
有序: 依赖的是链表
唯一: 依赖的是哈希表
public class LinkedHashMapTest {
public static void main(String[] args) {
//创建对象
LinkedHashMap lh = new LinkedHashMap();
//添加元素 : 相同键的时候值会覆盖,所以 "java" 对应的值是 "爪哇2"
lh.put("hello", "你好");
lh.put("world", "世界");
lh.put("java", "爪哇");
lh.put("java", "爪哇2");
//使用第一种输出
Set keys = lh.keySet();
//遍历
for (String key : keys) {
//根据键获取值
String value = lh.get(key);
//输出
System.out.println(key + "----" + value);
}
}
}
4 . TreeMap: 底层的数据结构是二叉树.
可以对元素进行排序:
a: 自然排序
对元素有要求: 要求元素必须实现Comparable接口,复写compareTo方法
b: 比较器排序
第一种自然排序法:Student实现Comparable接口,复写compareTo方法
可以对元素进行排序:
a: 自然排序
对元素有要求: 要求元素必须实现Comparable接口,复写compareTo方法
b: 比较器排序
第一种自然排序法:Student实现Comparable接口,复写compareTo方法
public class Student implements Comparable{
private String name ;
private int age ;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
// 按照年龄
int num = this.getAge() - o.getAge() ;
// 按照姓名比较
int num2 = (num == 0) ? this.getName().compareTo(o.getName()) : num ;
return num2;
}
}
//测试类:
public class TreeMapTest01 {
public static void main(String[] args) {
//创建TreeMap对象
TreeMap tm =new TreeMap();
// 创建学生对象
Student s1 = new Student("刘亦菲" , 23) ;
Student s2 = new Student("范冰冰" , 28) ;
Student s3 = new Student("李冰冰" , 21) ;
Student s4 = new Student("赵丽颖" , 18) ;
Student s5 = new Student("杨颖" , 22) ;
Student s6 = new Student("刘亦菲" , 23) ;
// 把元素添加到集合中
tm.put(s1, "神仙姐姐") ;
tm.put(s2, "范爷") ;
tm.put(s3, "女神") ;
tm.put(s4, "花千骨") ;
tm.put(s5, "baby") ;
tm.put(s6, "小龙女") ;
//使用第一种遍历:得到键的集合
Set keys = tm.keySet();
for (Student s : keys) {
//根据键得到值
String value = tm.get(s);
//输出结果
System.out.println(s.getName() + "---" + s.getAge() + "---" + value);
}
}
}
第二种比较器排序:
//普通的学生类
public class Student {
private String name ;
private int age ;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//使用匿名内部类的测试类:
public class TreeMapTest02 {
public static void main(String[] args) {
//采用匿名内部类方法创建TreeMap集合
TreeMap tm = new TreeMap(new Comparator() {
@Override
public int compare(Student s1, Student s2) {
// TODO Auto-generated method stub
// 按照年龄
int num = s1.getAge() - s2.getAge() ;
// 按照姓名比较
int num2 = (num == 0) ? s1.getName().compareTo(s2.getName()) : num ;
return num2;
}
});
// 创建学生对象
Student s1 = new Student("刘亦菲" , 23) ;
Student s2 = new Student("范冰冰" , 28) ;
Student s3 = new Student("李冰冰" , 21) ;
Student s4 = new Student("赵丽颖" , 18) ;
Student s5 = new Student("杨颖" , 22) ;
Student s6 = new Student("刘亦菲" , 23) ;
// 把元素添加到集合中
tm.put(s1, "神仙姐姐") ;
tm.put(s2, "范爷") ;
tm.put(s3, "女神") ;
tm.put(s4, "花千骨") ;
tm.put(s5, "baby") ;
tm.put(s6, "小龙女") ;
//使用第二种方式遍历集合:获取键值对儿
Set> en = tm.entrySet();
for (Entry e : en) {
//获取键
Student s = e.getKey();
//获取值
String value = e.getValue();
//输出
System.out.println(s.getName() + "---" + s.getAge() + "---" + value);
}
}
}