MAP

image.png
image.png

MAP接口的常用方法

image.png

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class HashMap01 {
  4. public static void main(String[] args) {
  5. Map map=new HashMap();
  6. map.put("cai","y");
  7. map.put("chun","a");
  8. map.put("mei","n");
  9. map.put("cai","cai");
  10. System.out.println(map);//{chun=a, mei=n, cai=cai}
  11. map.remove("cai");
  12. System.out.println(map);
  13. System.out.println(map.get("mei"));//n
  14. }
  15. }

MAP的遍历

  • 通过keySet集合遍历key 查找value
  • 直接通过values集合 遍历输出value
  • 通过entry集合里面的 Map.Entry (k,v)输出数据,注意Map.Entry和entry的转型

image.png

  1. import arraylist.Iterator01;
  2. import javax.swing.text.html.HTMLDocument;
  3. import java.util.*;
  4. public class HashMap02 {
  5. public static void main(String[] args) {
  6. Map map= new HashMap();
  7. map.put("cai","蔡");
  8. map.put("chun","春");
  9. map.put("mei","梅");
  10. //这是在keyset 集合里面通过遍历key 然后取出value
  11. Set set=map.keySet();
  12. for (Object key:set) {
  13. System.out.println(key+ "-" + map.get(key));
  14. }
  15. //用迭代器一定要调用!!
  16. Iterator iterator= set.iterator();
  17. while (iterator.hasNext()){
  18. Object key=iterator.next();
  19. System.out.println(key +"-" +map.get(key));
  20. }
  21. //通过遍历 values集合 ,获取value的值
  22. Collection values = map.values();
  23. for (Object value:values) {
  24. System.out.println(value);
  25. }
  26. //迭代器要引用
  27. Iterator iterator1= values.iterator();
  28. while (iterator1.hasNext()){
  29. Object value=iterator1.next();
  30. System.out.println(value);
  31. }
  32. //通过entry这个集合 来查找Map.Entry里面的(k,v)
  33. Set entrySet = map.entrySet();
  34. for (Object entry :entrySet) {
  35. Map.Entry m=(Map.Entry)entry;//向下转型,entry(object)
  36. System.out.println(m.getKey() +"-"+ m.getValue());
  37. }
  38. Iterator iterator2 = entrySet.iterator();
  39. while (iterator2.hasNext()){
  40. Map.Entry m=(Map.Entry)iterator2.next();//向下转型,iterator(objec)
  41. System.out.println(m.getKey() +"-"+m.getValue());
  42. }
  43. }
  44. }

image.png

  1. import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
  2. import java.security.KeyStore;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7. public class HashMapexer01 {
  8. public static void main(String[] args) {
  9. Map map = new HashMap();
  10. map.put(1,new Person(1,"cai",12000));
  11. map.put(2,new Person(2,"cai1",1200));
  12. map.put(3,new Person(3,"cai2",12001));
  13. Set keySet = map.keySet();
  14. //用增强for循环
  15. for (Object key :keySet) {
  16. Person person=(Person) map.get(key);
  17. if (person.getSal()>1880){
  18. System.out.println(person);
  19. }
  20. }
  21. //用entrySet迭代器
  22. Set entrySet = map.entrySet();
  23. Iterator iterator = entrySet.iterator();
  24. while (iterator.hasNext()){
  25. Map.Entry entry=(Map.Entry)iterator.next();
  26. Person person=(Person) entry.getValue();
  27. if (person.getSal()>10000){
  28. System.out.println(person);
  29. }
  30. }
  31. }
  32. }
  33. class Person{
  34. private int id;
  35. private String name;
  36. private int sal;
  37. public int getId() {
  38. return id;
  39. }
  40. public void setId(int id) {
  41. this.id = id;
  42. }
  43. public String getName() {
  44. return name;
  45. }
  46. public void setName(String name) {
  47. this.name = name;
  48. }
  49. public int getSal() {
  50. return sal;
  51. }
  52. public void setSal(int sal) {
  53. this.sal = sal;
  54. }
  55. @Override
  56. public String toString() {
  57. return "Person{" +
  58. "id=" + id +
  59. ", name='" + name + '\'' +
  60. ", sal=" + sal +
  61. '}';
  62. }
  63. public Person(int id, String name, int sal) {
  64. this.id = id;
  65. this.name = name;
  66. this.sal = sal;
  67. }
  68. }

image.png
image.png