首页 > 编程语言 > 详细

Java学习笔记32(集合框架六:Map接口)

时间:2018-01-14 00:45:47      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:hashtable   code   rri   输出   clas   bsp   pan   ons   spa   

Map接口与Collection不同:

Collection中的集合元素是孤立的,可理解为单身,是一个一个存进去的,称为单列集合

Map中的集合元素是成对存在的,可理解为夫妻,是一对一对存进去的,称为双列集合

Map中存入的是:键值对,键不可以重复,值可以重复

 

Map接口中的常用集合:

1.HashMap:哈希表的存储结构,但是无法保证存取顺序

2.LinkedHashMap:存储数据采用的是哈希表和链表,可以有顺序

 

Map接口的常用方法:

示例:

package demo;

import java.util.HashMap;
import java.util.Map;

public class MapDemo {
    public static void main(String[] args) {
        function1();
        function2();
        function3();
    }

    public static void function1() {
        // 将键值对存储到集合中
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("c", 4);
        System.out.println(map);
        // {b=2, c=4, a=1}
        // =连接键值对,存入重复键,则会覆盖
    }

    public static void function2() {
        //通过键获取值
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        String value = map.get(1);
        System.out.println(value);//a
        //有则返回值,不存在返回null
    }
    public static void function3(){
        //移除集合中的键值对
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        map.remove(3);
        System.out.println(map);
        //{1=a, 2=b}
    }
}

 

Map接口的遍历:

第一种方式:

package demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }

    public static void function1() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 4);
        Set<String> set = map.keySet();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String key = it.next();
            Integer value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
    }

    public static void function2() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);
        map.put("d", 4);
        for (String key : map.keySet()) {
            Integer value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
    }
}
// 遍历输出的无序

 

第二种方式(根据映射关系):

package demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");
        Set<Entry<Integer, String>> set = map.entrySet();
        Iterator<Entry<Integer, String>> it = set.iterator();
        while (it.hasNext()) {
            Entry<Integer, String> entry = it.next();
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "<==>" + value);
        }
    }
}

 

存储自定义对象:

 

package demo;

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}
package demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

//存储自定义类型
public class HashMapDemo {
    public static void main(String[] args) {
        function1();
        function2();
    }

    public static void function1() {
        // 自定义类型作为值出现
        HashMap<String, Person> map = new HashMap<String, Person>();
        map.put("a", new Person("1", 20));
        map.put("b", new Person("2", 20));
        map.put("c", new Person("3", 20));
        // 利用两种遍历
        for (String key : map.keySet()) {
            Person value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
        for (Entry<String, Person> entry : map.entrySet()) {
            String key = entry.getKey();
            Person value = entry.getValue();
            System.out.println(key + "<==>" + value);
        }
    }

    public static void function2() {
        // 自定义类型作为键出现
        // 保证键的唯一性,需要重写hashcode和equals方法
        HashMap<Person, String> map = new HashMap<Person, String>();
        map.put(new Person("a", 20), "a");
        map.put(new Person("b", 20), "a");
        map.put(new Person("c", 20), "a");
        map.put(new Person("c", 20), "a");
        // 两种遍历
        for (Person key : map.keySet()) {
            String value = map.get(key);
            System.out.println(key + "<==>" + value);
        }
        for (Entry<Person, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "<==>" + entry.getValue());
        }
    }

}

 

LinkedHashMap集合:

package demo;

import java.util.LinkedHashMap;

public class LinkedHashMapDemo {
    public static void main(String[] args) {
        LinkedHashMap<String, String> link = new LinkedHashMap<String, String>();
        link.put("1", "a");
        link.put("2", "a");
        link.put("3", "a");
        link.put("4", "a");
        System.out.println(link);
        //{1=a, 2=a, 3=a, 4=a}
        //存取顺序一致
    }
}

 

set接口下还有一个hashtable集合,但是过时了,现在由hashmap取代

不过,要注意一个问题:

HashMap允许存储null值,HashTable不允许存储null值,两种都不允许存储null键

 

Java学习笔记32(集合框架六:Map接口)

标签:hashtable   code   rri   输出   clas   bsp   pan   ons   spa   

原文:https://www.cnblogs.com/xuyiqing/p/8280596.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号