码迷,mamicode.com
首页 > 编程语言 > 详细

java之map接口

时间:2020-01-03 23:12:50      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:keyset   str   turn   一个   oracle   ext   null   java   接口   

一。collection与map集合的区别

1.collection集合中的元素为孤立存在,map集合中的预算为成对存在(key键--vlaue值)

2.collection集合为单列集合,map集合为双列集合

3.collection集合中有重复的元素,也可以没有重复的元素,map集合中key值不能重复,但值可以重复

4.Map中常用的集合为HashMap集合(存储无顺序)、LinkedHashMap集合(存储有顺序)

5.键值遵循hashSet,不能重复,无序存储。linkedMap,有顺序。

二。如果存储的键为自定义数据类型,必须重写键的hashCode()方法、equals()方法。

person对象

 1 package com.oracle.demo01;
 2 
 3 public class Person {
 4  private String name;
 5  private Integer age;
 6 public String getName() {
 7     return name;
 8 }
 9 public void setName(String name) {
10     this.name = name;
11 }
12 public Integer getAge() {
13     return age;
14 }
15 public void setAge(Integer age) {
16     this.age = age;
17 }
18 public Person(String name, Integer age) {
19     super();
20     this.name = name;
21     this.age = age;
22 }
23 public Person() {
24     super();
25 }
26 @Override
27 public String toString() {
28     return "Person [name=" + name + ", age=" + age + "]";
29 }
30  @Override
31 public int hashCode() {
32     final int prime = 31;
33     int result = 1;
34     result = prime * result + ((age == null) ? 0 : age.hashCode());
35     result = prime * result + ((name == null) ? 0 : name.hashCode());
36     return result;
37 }
38  @Override
39 public boolean equals(Object obj) {
40     if (this == obj)
41         return true;
42     if (obj == null)
43         return false;
44     if (getClass() != obj.getClass())
45         return false;
46     Person other = (Person) obj;
47     if (age == null) {
48         if (other.age != null)
49             return false;
50     } else if (!age.equals(other.age))
51         return false;
52     if (name == null) {
53         if (other.name != null)
54             return false;
55     } else if (!name.equals(other.name))
56         return false;
57     return true;
58 }
59 }

 

 1 package com.oracle.demo01;
 2 
 3 import java.util.HashMap;
 4 import java.util.Set;
 5 
 6 //自定义类型Map集合中的键值
 7 public class Demo04 {
 8 public static void main(String[] args) {
 9     HashMap<Person,String>  map=new HashMap<Person,String>();
10     //自定义类使用对象的形式存储数值
11     map.put(new Person("一",18),"yi");
12     map.put(new Person("二",18),"er");
13     map.put(new Person("三",18),"san");
14     map.put(new Person("一",18),"wu");
15     //遍历
16     Set<Person> set=map.keySet();
17     for(Person p:set){
18         System.out.println(p+"---"+map.get(p));
19     }
20     //重写hashcode(),equals()   source___  hashcode()and equals()  
21 }
22 }

 

三。put  remove  get方法的使用

 1 package com.oracle.demo01;
 2 
 3 import java.util.HashMap;
 4 import java.util.HashSet;
 5 
 6 public class Demo01 {
 7     public static void main(String[] args) {
 8         HashMap<String, String> map = new HashMap<String, String>();
 9         // put存储键值对
10         map.put("yi", "123");
11         map.put("er", "456");
12         map.put("yi", "456");
13         map.put("san", "789");
14         //删除
15         System.out.println(map.remove("yi"));
16         // 取值 获得Value值    同一个key值,value值覆盖前面的内容,若无这个key值,返回null
17         System.out.println(map.get("yi"));
18     }
19 }

四。map集合中通过键查询value值的四种方法

keyset+迭代器或增强for

 1 package com.oracle.demo01;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Set;
 6 
 7 public class Demo02 {
 8     public static void main(String[] args) {
 9         HashMap<String, String> map = new HashMap<String, String>();
10         // put存储键值对,返回Value值
11         map.put("yi", "123");
12         map.put("er", "456");
13         map.put("yi", "456");
14         map.put("san", "789");
15         // keyset+增强for ,得到key值
16         // 导set包,
17         /*
18          * Set<String> ss=map.keySet(); //遍历 for(String s:ss){//获得S对应的key值
19          * System.out.println(map.get(s));//传入s值,得到value值 }
20          */
21         // keySet+迭代器
22         // 获得set集合,单列集合
23         Set<String> s1 = map.keySet();
24         // 创建迭代器
25         Iterator<String> s2 = s1.iterator();
26         // 遍历
27         while (s2.hasNext()) {
28             // 此时为两次next()
29             // System.out.println(ssss.next()+","+map.get(ssss.next()));
30             // 程序中最好出现一次next().
31             String a = s2.next();
32             System.out.println("键值key为" + a + ",value值为" + map.get(a));
33         }
34 
35     }
36 
37 }

enteyset+迭代器或增强for循环()

1.首先获得键值对对象map.entry<E,E>   2.再使用getkey/getvalue

 1 package com.oracle.demo01;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 import java.util.Set;
 7 
 8 public class Demo03 {
 9     public static void main(String[] args) {
10         //内部类的调用,内部类名.外部类名.方法/属性或创建对象
11         //Entry把集合中的键值对封装称为对象(整体)Entry<E,E>,
12         //然后通过getKey和GetValue,获得键值和value值
13         HashMap<String, String> map = new HashMap<String, String>();
14         // put存储键值对,返回Value值
15         map.put("yi", "123");
16         map.put("er", "456");
17         map.put("yi", "456");
18         map.put("san", "789");
19         //entry+增强for
20         //获得Map.Entry<String,String>对象的Set集合
21         //泛型可以为数据类型、对象、集合
22         Set<Map.Entry<String,String>> set=map.entrySet();
23         /*for(Map.Entry<String,String> s:set){//获得set集合中的每一个对象
24             String s1=s.getKey();
25             String s2=s.getValue();
26             System.out.println(s1+"---"+s2);
27         }*/
28         //获取迭代器
29         Iterator<Map.Entry<String,String>> i=set.iterator();
30     while(i.hasNext()){
31         //得到每一个Entry对象
32         Map.Entry<String,String>  entry=i.next();
33         String s1=entry.getKey();
34         String s2=entry.getValue();
35         System.out.println(entry);//默认调用Entry中的tostring()方法
36         System.out.println(s1+"/"+s2);
37     }
38     }
39 }

java之map接口

标签:keyset   str   turn   一个   oracle   ext   null   java   接口   

原文地址:https://www.cnblogs.com/mlf19920916/p/12147263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!