Map集合
特点:
该集合存储键值对,一对一对的往里存,并且键是唯一的。
特点:要保证map集合中键的唯一性。
Map : Hashtable 底层是哈希表数据结构,不可以存入null键null值,该集合是线程同步的
hashmap 底层是哈希表数据结构,允许使用null值和null键,是不同步的,效率高
获取和遍历方法测试:
HasMap使用:
TreeMap集合
HashMap和Hashtable区别:
特点:
该集合存储键值对,一对一对的往里存,并且键是唯一的。
特点:要保证map集合中键的唯一性。
Map : Hashtable 底层是哈希表数据结构,不可以存入null键null值,该集合是线程同步的
hashmap 底层是哈希表数据结构,允许使用null值和null键,是不同步的,效率高
TreeMap 底层是二叉树结构,线程不同步,可以用于给Map集合中的键进行排序。
常用方法
测试demo:
package map;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author Angus
*
* 需求:有如下学生,学号和学生一一对应
* 学号1 学生1
* 学号2 学生2
* 学号3 学生3
*
* Map和Collection区别:
*
* Map:是键值对 队列形式的集合,键是唯一的,不能重复,值可以重复
* Collection:是单列值的集合,List儿子可以重复,Set儿子唯一。。
*
*Map接口的功能
* 增加
* put(K key,V value) 当key不存在时添加元素,当key存在替换元素
* 删除
* void clear() 移除此 set 中的所有元素
* remove(Object key) 根据指定的键删除键值队
* 判断
* boolean containsKey(Object key)如果此映射包含指定键的映射关系,则返回 true。更确切地讲
* boolean containsValue(Object value)如果此映射将一个或多个键映射到指定值,则返回 true
* boolean isEmpty() 判断是否为空
* 获取
* Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图 键值队
* get(Object key) 根据键获取值
* Set<K> keySet() 返回键的集合
* 长度
* int size() 长度
* values() 所有值的集合
*
*/
public class MapDemo {
public static void main(String[] args) {
//创建集合 实现自己子类
Map<String,String> map = new HashMap<String,String>();
//创建元素对象 添加元素
map.put("hehe001", "wsj001");
map.put("hehe002", "wsj002");
map.put("hehe003", "wsj003");
//添加元素,如果键不存在,返回null
System.out.println(map.put("heh004", "wsj004"));//null
//删除
// map.clear(); //清楚所有
System.out.println(map.remove("hehe002"));
System.out.println(map.remove("hehe007"));//null
//判断
System.out.println(map.containsKey("hehe001"));//true
System.out.println(map.containsKey("hehe007"));//false
//长度
System.out.println(map.size());//键值队数量
System.out.println(map);
}
}
获取和遍历方法测试:
package map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*
* @author Angus
*
*
*Map接口的功能st3
* 获取
* Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图 键值队
* get(Object key) 根据键获取值
* Set<K> keySet() 返回键的集合
*
* 遍历:
* 1:把所有键集合到一起,根据键找值
* 2:Set<Map.Entry<K,V>> entrySet()
*/
public class MapDemo {
public static void main(String[] args) {
//创建集合 实现自己子类
Map<String,String> map = new HashMap<String,String>();
//创建元素对象 添加元素
map.put("hehe001", "wsj001");
map.put("hehe002", "wsj002");
map.put("hehe003", "wsj003");
//获取:
//键的集合
Set<String> set = map.keySet();
for(String st :set){
System.out.println(st);
}
System.out.println("------------------");
//值的集合
Collection<String> co = map.values();
for(String st2 : co){
System.out.println(st2);
}
System.out.println("------------------");
//根据键获取值
System.out.println(map.get("hehe001"));
System.out.println(map.get("hehe002"));
System.out.println("------------------");
//遍历
//方式1
Set<String> set2 = map.keySet();
for(String st3 : set2){
String value = map.get(st3);
System.out.println(st3+"****"+value);
}
System.out.println("------------------");
//方式二
Set<Entry<String, String>> entrySet = map.entrySet();
for(Entry<String, String> me : entrySet){
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"*****"+value);
}
}
}
HasMap使用:
package map;
import java.util.HashMap;
import java.util.Set;
/**
* HashMap存储键值 遍历
* @author Angus
*
*/
public class HashMapDemo {
public static void main(String[] args) {
//创建集合
HashMap<String, Student> hm = new HashMap<String, Student>();
Student s1 = new Student("hehe1", 11);
Student s2 = new Student("hehe2", 12);
Student s3 = new Student("hehe3", 13);
hm.put("001", s1);
hm.put("002", s2);
hm.put("003", s3);
Set<String> keySet = hm.keySet();
for(String key : keySet){
Student value = hm.get(key);
System.out.println(key+"---"+value);
}
}
}
TreeMap集合
package map;
import java.util.Set;
import java.util.TreeMap;
/**
*
* @author Angus
* TreeMap 存储字符串
*/
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<String, String> tm = new TreeMap<String, String>();
tm.put("hehe1", "11");
tm.put("hehe2", "12");
tm.put("hehe3", "13");
//遍历
Set<String> key = tm.keySet();
for(String s :key){
String value = tm.get(s);
System.out.println(s+value);
}
}
}
存储自定义对象:
package map;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
/**
*
* @author Angus
* TreeMap 存储自定义对象
* 就必须实现排序
* A:让自定义对象所属类实现Comparable接口
* B:使用带参构造接受Comparator接口
*
*/
public class TreeMapDemo2 {
public static void main(String[] args) {
TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
//年龄排序
int num = s1.getAge()-s2.getAge();
int num2 = (num==0)?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
Student s1 = new Student("hehe1",11);
Student s2 = new Student("hehe2",12);
Student s3 = new Student("hehe3",13);
Student s4 = new Student("hehe4",14);
tm.put(s1, "11");
tm.put(s2, "12");
tm.put(s3, "13");
Set<Student> key = tm.keySet();
for(Student s :key){
String value = tm.get(s);
System.out.println(s+value);
//Exception in thread "main" java.lang.ClassCastException: map.Student cannot be cast to java.lang.Comparable
//直接输出报错。。。。
//使用带参构造接受Comparator接口
}
}
}
HashMap和Hashtable区别:
package map;
import java.util.HashMap;
import java.util.Hashtable;
/**
*
* @author Angus
* HashMap 和Hashtable 区别
* A HashMap 线程不安全,效率高,允许null键和值
* B Hashtable 线程安全,效率低,不允许null键和值
*/
public class HastableDemo {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<>();
hm.put("hehe", null);
hm.put(null, null);
hm.put(null, null);
System.out.println(hm); //{null=null, hehe=null}
//没有问题
Hashtable<String, String> ht = new Hashtable<>();
ht.put("hehe", null);
ht.put(null, null);
ht.put(null, null);
System.out.println(ht); //java.lang.NullPointerException
//每一行都报空指针
}
}
最后附上JDK使用文档API 下载