目录
Map的接口特点
Map接口的常用方法
Map的遍历方法
根据value反向获取key
Map排序
HashMap底层
Hashtable
key重复时会进行替换value 但是不会替换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地址指向
// 而一个Entry对象就有key value EntrySet> 也就是Set> entrySet
//3、entrySet中,定义的类型是Map.Entry,但是实际上存放的还是HashMap$Node
// 因为 static class Node implements Map.Entry
//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 map,String str){
String resultKey = null;
for (String key:map.keySet()){
if (map.get(key).equals(str)){
resultKey = key;
}
}
return resultKey;
}
//获取一个列表
public static List getKey01(Map map,String str){
List 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 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 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 map){
Set entrySet = map.entrySet();
List> list = new ArrayList<>(entrySet);
list.sort(new Comparator>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
return o1.getValue()-o2.getValue();
}
});
for (int i =0;i map){
Set entrySet = map.entrySet();
List> list = new ArrayList<>(entrySet);
Collections.sort(list,new Comparator
package com.edu.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({"all"})
public class Demo05 {
public static void main(String[] args) {
Map map = new HashMap<>();
Member member1 = new Member(1,211111,"小明");
Member member2 = new Member(2,311111,"小红");
Member member3 = new Member(3,111,"小龙");
map.put(member1.getId(),member1);
map.put(member2.getId(),member2);
map.put(member3.getId(),member3);
Set keySet = map.keySet();
for (Object key:keySet){
if (map.get(key).getSalary()>18000){
System.out.println(map.get(key).toString());
}
}
Set entrySet = map.entrySet();
for (Object obj:entrySet){
Map.Entry OO = (Map.Entry) obj;
if (map.get(OO.getKey()).getSalary()>18000){
System.out.println(map.get(OO.getKey()).toString());
}
}
}
}
class Member{
private int id;
private int salary;
private String name;
public Member(int id, int salary, String name) {
this.id = id;
this.salary = salary;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSalary() {
return salary;
}
@Override
public String toString() {
return "Member{" +
"id=" + id +
", salary=" + salary +
", name='" + name + '\'' +
'}';
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.edu.collection;
import java.util.Properties;
public class Demo04 {
public static void main(String[] args) {
//Properties 继承 Hashtable
//可以通过k-v存放数据,key和value不能为null
Properties properties = new Properties();
properties.put("JOHN",100);
properties.put("JOHN1",100);
properties.put("JOHN2",100);
properties.put("JOHN3",100);
System.out.println(properties);
//通过key获取值
System.out.println(properties.get("JOHN"));
//删
properties.remove("JONH2");
//改
properties.put("JOHN",12);
}
}
如果创建TreeMap实例时没有传入Comparator比较器,那么程序会对键进行判断,判断它是否为空,如果为空,就会抛出空指针异常。如果我们有特殊需求,是可以自己实现Comparator接口,并重写compare方法去处理掉比较对象为空的情况,这样做也是可以实现在TreeMap中存入一个空值的键的。
注意:TreeSet的底层是用TreeMap实现,即将TreeSet的值作为键存入TreeMap集合中,所以TreeSet存值时跟TreeMap一样的规则
package com.edu.collection;
import java.util.Comparator;
import java.util.TreeSet;
@SuppressWarnings({"all"})
public class Demo05 {
public static void main(String[] args) {
//1 TreeSet使用无参构造器时,仍然是无序的
//2 传入一个比较器(匿名内部类) 指定排序规则
TreeSet treeSet = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
//return ((String)o1).compareTo((String)o2);//大-小 颠倒小-大
return ((String)o1).length() - ((String)o2).length();//长度相同无法加入
}
});
treeSet.add("jack");
treeSet.add("tom");
treeSet.add("alice");
treeSet.add("abc");
System.out.println(treeSet);
}
}
package com.edu.collection;
import java.util.Comparator;
import java.util.TreeMap;
@SuppressWarnings({"all"})
public class Demo05 {
public static void main(String[] args) {
TreeMap treeMap = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((String)o2).length() - ((String)o1).length();
}
});
treeMap.put("jack","杰克");
treeMap.put("tom","汤姆");
treeMap.put("kristina","克瑞斯提诺");
treeMap.put("smith","史密斯");
treeMap.put("jack","杰1克");//替换
treeMap.put("hsp","as"); //tom=as
System.out.println("treemap="+treeMap);
}
}
package com.edu.collection;
import java.util.*;
@SuppressWarnings({"all"})
public class Demo06 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("tom");
list.add("smith");
list.add("king");
list.add("milan");
list.add("xiaoli");
//Collections常用方法
//reverse(List) 反转List中的元素顺序
Collections.reverse(list);
System.out.println(list);
//shuffle 随机打乱
Collections.shuffle(list);
System.out.println(list);
//sort 排序 可以new Comparator
Collections.sort(list);
System.out.println(list);
Collections.sort(list, new Comparator
package com.edu.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@SuppressWarnings({"all"})
public class Demo07 {
public static void main(String[] args) {
NewWork demo1 = new NewWork("标题一");
NewWork demo2 = new NewWork("标题二");
List list = new ArrayList();
demo1.setContent("本套Java视频教程非常适合零基础的学员观看,本教程全面讲解了Java基础的方方面面。每一个知识点都讲解的非常细致,通俗易懂。");
demo2.setContent("既让初学者快速掌握Java,也能让Java老手更全面、系统的学习到技术细节, 是想转行 javaEE, 大数据 程序员的优质资源 韩顺平");
list.add(demo1);
list.add(demo2);
System.out.println("倒序前:"+list);
Collections.reverse(list);
System.out.println("倒序后:"+list);
for (int i=0;i
package com.edu.collection;
import com.sun.xml.internal.ws.encoding.HasEncoding;
import java.util.*;
public class Demo09 {
@SuppressWarnings({"all"})
public static void main(String[] args) {
TreeSet treeSet = new TreeSet();
treeSet.add(new Person("11"));
treeSet.add(new Person("122"));
System.out.println(treeSet);
}
}
class Person implements Comparable{ //实现Comparable
private String name;
public Person(String name) {
this.name = name;
}
@Override //重写compareTo
public int compareTo(Object o) {
return Objects.hash(o) ;
}
}