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

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键

 

相关文章