key重复时会进行替换
package com.edu.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({"all"})
public class Demo01 {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("no1","韩顺平");
map.put("no2","张无忌");
//1、k-v最后都是 HashMap$Node node = newNode(hash,key,value,null)
//2、k-v为了方便遍历,还会创建 EntrySet集合,该集合存放的元素类型就是Entry,装的是Node<k v>地址指向
// 而一个Entry对象就有key value EntrySet<Entry<K,V>> 也就是Set<Map.Entry<K,V>> entrySet
//3、entrySet中,定义的类型是Map.Entry,但是实际上存放的还是HashMap$Node
// 因为 static class Node<K,v> implements Map.Entry<K,V>
//4、当把HashMap$Node 对象存放到 EntrySet 就方便我们的遍历,Map.Entry提供了两个重要的方
// getkey() 和 getValue()
//keySet和entrySet只是一个指向,并没有真实存储对象
Set set = map.entrySet();
System.out.println(set.getClass());//HashMap$EntrySet
for(Object obj:set){
System.out.println(obj.getClass());//HashMap$Node 接口实现 向上转型了
//1、向下转型 因为是Object接受的
Map.Entry entry = (Map.Entry)obj;
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
package com.edu.map;
import java.util.*;
public class Demo02 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("邓超","孙俪");
map.put("王宝强","马蓉");
map.put("宋杰","马蓉");
map.put("六子","null");
map.put("鹿晗","关晓彤");
//第一组:先取出key,再通过key获取对应的value
Set keyset = map.keySet();
//(1)增强for
for(Object obj:keyset){
System.out.println(obj+":"+map.get(obj));
}
//(2)迭代器
System.out.println("-----------------------------");
Iterator iterator = keyset.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
System.out.println(next+"-"+map.get(next));
}
//第二组:取出所有的values
Collection values = map.values();
//(1) 增强for
for (Object value:values){
System.out.println(value);
}
//(2)迭代器
//第三组:通过EntrySet来获取k-v
Set entrySet = map.entrySet();
//(1)增强for
for (Object o:entrySet){
Map.Entry obj = (Map.Entry)o;
System.out.println(obj.getKey()+"--"+obj.getValue());
}
//(2)迭代器
System.out.println("-----------------------------");
Iterator iterator1 = entrySet.iterator();
while (iterator1.hasNext()){
Object next = iterator1.next();
Map.Entry entry = (Map.Entry) next;
System.out.println(entry.getKey()+"--"+entry.getValue());
}
}
}
package com.edu.map;
import java.security.Key;
import java.util.*;
public class Demo03{
@SuppressWarnings({"all"})
public static void main(String[] args) {
Map map = new HashMap();
map.put("邓超","孙俪");
map.put("王宝强","马蓉");
map.put("宋杰","马蓉");
map.put("六子","null");
map.put("鹿晗","关晓彤");
System.out.println(getKey01(map,"马蓉"));
}
//获取一个key 最后一个匹配的
public static String getKey(Map<String,String> map,String str){
String resultKey = null;
for (String key:map.keySet()){
if (map.get(key).equals(str)){
resultKey = key;
}
}
return resultKey;
}
//获取一个列表
public static List<String> getKey01(Map<String,String> map,String str){
List<String> list = new ArrayList<>();
for (String key:map.keySet()){
if (map.get(key).equals(str)){
list.add(key);
}
}
return list;
}
}
package com.edu.map;
import javax.swing.*;
import java.util.*;
@SuppressWarnings({"all"})
public class Demo04 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("Apple",7299);
map.put("Banana",1211);
map.put("Cs",12122);
map.put("Deep",121);
map.put("Enda",2311);
System.out.println(map);
keySort(map);
System.out.println();
System.out.println(map);
System.out.println("------------------------");
valueSort(map);
System.out.println("------------------------");
valueSort02(map);
}
//按照key进行排序
public static void keySort(Map<String,Integer> map){
Set keySet = map.keySet();
Object[] arr = keySet.toArray();
Arrays.sort(arr);
for (Object i:arr){
System.out.print(i+"-"+map.get(i)+"\t");
}
}
//按value进行排序
//重写list.sort方法
public static void valueSort(Map<String,Integer> map){
Set entrySet = map.entrySet();
List<Map.Entry<String,Integer>> list = new ArrayList<>(entrySet);
list.sort(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o1.getValue()-o2.getValue();
}
});
for (int i =0;i<list.size();i++){
System.out.println(list.get(i).getKey()+":"+list.get(i).getValue());
}
}
// //重写Collections.sort方法
public static void valueSort02(Map<String,Integer> map){
Set entrySet = map.entrySet();
List<Map.Entry<String,Integer>> list = new ArrayList<>(entrySet);
Collections.sort(list,new Comparator<Object>(){
@Override
public int compare(Object O1,Object O2){
Map.Entry<String,Integer> o3 = (Map.Entry)O1;
Map.Entry<String,Integer> O4 = (Map.Entry)O2;
return o3.getValue()- O4.getValue();
}
});
for (Map.Entry<String,Integer> mapping:list){
System.out.println(mapping.getKey()+":"+mapping.getValue());
}
}
}