package java_test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class mapDemo {
public void mapAction(){//map基本操作
Map<String,String> map =new HashMap<String,String>();
map.put("a", "this is a");//往map里添加一堆键值对
int len=map.size();//一共有多少对键值对
System.out.println(len);//输出1
String a=map.get("a");//获取key为a的value值
System.out.println(a);//输出this is a
System.out.println(map.get("b"));//获取一个不存在的key的value值时,返回值为null,并不会抛出异常
map.put("a", "this is the second a");//添加一个已经存在key,后面添加的value值会覆盖前面已经存在的value
System.out.println(map.get("a"));//输出this is the second a
boolean c=map.containsKey("a");//判断map中是否存在key a
System.out.println(c);//输出true
boolean v=map.containsValue("this is the second a");//判断map中是否存在给定的value
System.out.println(v);//输出true
Map<String,String> m1=new HashMap<String,String>();
m1.put("a", "this is the third a");
m1.put("d", "this is d");
System.out.println(m1);
map.putAll(m1);//取map与m1的并集,如果m1中已经在map中存在的key,则m1中的value会覆盖map中的存在key的value,并集的值会赋值给map
System.out.println(map);
}
public void loopMap(){
Map<String,String> map=new HashMap<String,String>();
map.put("a", "this is a");
map.put("b", "this is b");
/**
* set可以理解为一直特殊的List,只是里面的元素对象是不允许重复的,keySet是将key都放到一个集合里面去
* HashMap是无序排列的keySet后,set对象也是无序的
*/
Set<String> set=map.keySet();
for(String s:set){
System.out.println(map.get(s));//循环输出map的value
}
}
public void loopRemoveMap(){
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(1, "this is a");
map.put(2, "this is b");
/**
* set虽然可以看做是一个特殊的list,但是set没有像list一样有一个通过get取值的方式
* 要想取值,可以将set转为list,或者转为iterator
*/
Set<Integer> set =map.keySet();
List<Integer> list=new ArrayList<Integer>(set);//set转为list
for(int i=0;i<list.size();i++){
map.remove(list.get(i));
}
System.out.println(map);//输出{}
}
public void clearMap(){//清空map
Map<Integer,String> map=new HashMap<Integer,String>();
map.put(1, "this is a");
map.put(2, "this is b");
boolean e =map.isEmpty();//判断map里是否存在键值对
System.out.println(e);//输出true
map.clear();//清除map里所有的键值对
e=map.isEmpty();
System.out.println(e);//输出true
}
public void sortMapByKey(){//简单排序
Map<String,String> map=new HashMap<String,String>();
map.put("a", "this is a");
map.put("c", "this is c");
map.put("b", "this is b");
System.out.println(map);
/**
* TreeMap是一个按key进行圣墟排列的一个Map实现类
* TreeMap会自动的把里面的键值对进行排序
* 利用这一点,将HashMap转换为TreeMap,即可实现Map按key进行排序
*/
Map<String,String> tm=new TreeMap<String,String>();
tm.putAll(map);
map=tm;
System.out.println(map);
}
public void sortFuZaMapByKey(){
Map<String,String> map=new HashMap<String,String>();
map.put("a", "this is a");
map.put("c", "this is c");
map.put("b", "this is b");
/**
* LonkedHashMap是会记录你put进去的顺序,输出时,会按照你put进去的顺序进行输出
* 利用这一点,将HashMap的key按要求排列号,然后再put进一个LinkedHashMap即可实现map的复杂排序
*/
Map<String,String> lm= new HashMap<String,String>();
List<String> list =new ArrayList<String>(map.keySet());
Collections.sort(list,new Comparator<String>)(){
}
}
public static void main(String[] args) {
mapDemo m=new mapDemo();
m.mapAction();
m.loopMap();
m.loopRemoveMap();
m.sortMapByKey();
}
}