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

Map接口与Collection不同:

Java学习笔记32(集合框架六:Map接口),学习笔记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键

 

http://www.bkjia.com/Javabc/1292818.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1292818.htmlTechArticleJava学习笔记32(集合框架六:Map接口),学习笔记map
Map接口与Collection不同:
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键

 

admin

网站地图xml地图