zoukankan      html  css  js  c++  java
  • Java API —— HashMap类 & LinkedHashMap类

    1、HashMap类
      1)HashMap类概述
            键是哈希表结构,可以保证键的唯一性
      2)HashMap案例
            HashMap<String,String>
            HashMap<Integer,String>
            HashMap<String,Student>
            HashMap<Student,String>
    例子1:
    package hashmapdemos;
    import java.util.HashMap;
    import java.util.Set;
    /**
     * Created by gao on 15-12-21.
     */
    /*
     * HashMap:是基于哈希表的Map接口实现。
     * 哈希表的作用是用来保证键的唯一性的。
     *
     * HashMap<String,String>
     * 键:String
     * 值:String
     */
    public class HashMapDemo01 {
        public static void main(String[] args) {
            // 创建集合对象
            HashMap<String, String> hm = new HashMap<String, String>();
            // 创建元素并添加元素
            hm.put("it001", "马云");
            hm.put("it003", "马化腾");
            hm.put("it004", "乔布斯");
            hm.put("it005", "张朝阳");
            hm.put("it002", "裘伯君"); // wps
            hm.put("it001", "比尔盖茨");
            // 遍历
            Set<String> set = hm.keySet();
            for (String key : set) {
                String value = hm.get(key);
                System.out.println(key + "---" + value);
            }
        }
    }

    例子2:

    package hashmapdemos;
    import java.util.HashMap;
    import java.util.Set;
    /**
     * Created by gao on 15-12-21.
     */
    /*
     * HashMap<Integer,String>
     * 键:Integer
     * 值:String
     */
    public class HashMapDemo02 {
        public static void main(String[] args) {
            // 创建集合对象
            HashMap<Integer, String> hm = new HashMap<Integer, String>();
            // 创建元素并添加元素
            hm.put(27, "林青霞");
            hm.put(30, "风清扬");
            hm.put(28, "刘意");
            hm.put(29, "林青霞");
            // 下面的写法是八进制,因为以0开头,不能出现8以上的单个数据
            // hm.put(003, "hello");
            // hm.put(006, "hello");
            // hm.put(007, "hello");
            // hm.put(008, "hello");
            // 遍历
            Set<Integer> set = hm.keySet();
            for (Integer key : set) {
                String value = hm.get(key);
                System.out.println(key + "---" + value);
            }
        }
    }
    例子3:
    学生类:
    package hashmapdemos;
    /**
     * Created by gao on 15-12-21.
     */
    public class Student {
        private String name;
        private int age;
        public Student() {
        }
        public Student(String name, int age) {
            this.name = name;
            this.age = 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;
        }
    }

     测试类:

    package hashmapdemos;
    import java.util.HashMap;
    import java.util.Set;
    /**
     * Created by gao on 15-12-21.
     */
    /*
     * HashMap<String,Student>
     * 键:String    学号
     * 值:Student 学生对象
     */
    public class HashMapDemo03 {
        public static void main(String[] args) {
            // 创建集合对象
            HashMap<String, Student> hm = new HashMap<String, Student>();
            // 创建学生对象
            Student s1 = new Student("周星驰", 58);
            Student s2 = new Student("刘德华", 55);
            Student s3 = new Student("梁朝伟", 54);
            Student s4 = new Student("刘嘉玲", 50);
            // 添加元素
            hm.put("9527", s1);
            hm.put("9522", s2);
            hm.put("9524", s3);
            hm.put("9529", s4);
            // 遍历
            Set<String> set = hm.keySet();
            for (String key : set) {
                Student value = hm.get(key);
                System.out.println(key + "--" + value.getName() + "---" + value.getAge());
            }
        }
    }
    输出结果:
    9524--梁朝伟---54
    9522--刘德华---55
    9527--周星驰---58
    9529--刘嘉玲---50
    例子4:
    学生类:重写hashCode方法和equals方法
    package hashmapdemos;
    /**
     * Created by gao on 15-12-21.
     */
    public class Student {
        private String name;
        private int age;
        public Student() {
        }
        public Student(String name, int age) {
            this.name = name;
            this.age = 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;
        }
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Student)) return false;
            Student student = (Student) o;
            if (age != student.age) return false;
            if (!name.equals(student.name)) return false;
            return true;
        }
        @Override
        public int hashCode() {
            int result = name.hashCode();
            result = 31 * result + age;
            return result;
        }
    }

     测试类:

    package hashmapdemos;
    import java.util.HashMap;
    import java.util.Set;
    /**
     * Created by gao on 15-12-21.
     */
    /*
     * HashMap<Student,String>
     * 键:Student
     *         要求:如果两个对象的成员变量值都相同,则为同一个对象。
     * 值:String
     */
    public class HashMapDemo04 {
        public static void main(String[] args) {
    // 创建集合对象
            HashMap<Student, String> hm = new HashMap<Student, String>();
            // 创建学生对象
            Student s1 = new Student("貂蝉", 27);
            Student s2 = new Student("王昭君", 30);
            Student s3 = new Student("西施", 33);
            Student s4 = new Student("杨玉环", 35);
            Student s5 = new Student("貂蝉", 27);
            // 添加元素
            hm.put(s1, "8888");
            hm.put(s2, "6666");
            hm.put(s3, "5555");
            hm.put(s4, "7777");
            hm.put(s5, "9999");
            // 遍历
            Set<Student> set = hm.keySet();
            for (Student key : set) {
                String value = hm.get(key);
                System.out.println(key.getName() + "---" + key.getAge() + "---" + value);
            }
        }
    }
    输出结果:
    貂蝉---27---9999
    西施---33---5555
    杨玉环---35---7777
    王昭君---30---6666
     
     
    2、LinkedHashMap类
        Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。
    package linkedhashmapdemos;
    import java.util.LinkedHashMap;
    import java.util.Set;
    /**
     * Created by gao on 15-12-22.
     */
    /*
     * LinkedHashMap:是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。
     * 由哈希表保证键的唯一性
     * 由链表保证键盘的有序(存储和取出的顺序一致)
     */
    public class LinkedHashMapDemo {
        public static void main(String[] args) {
            // 创建集合对象
            LinkedHashMap<String, String> lm = new LinkedHashMap<String, String>();
            // 创建并添加元素
            lm.put("2345", "hello");
            lm.put("1234", "world");
            lm.put("3456", "java");
            lm.put("1234", "javaee");
            lm.put("3456", "android");
            // 遍历
            Set<String> set = lm.keySet();
            for (String key : set) {
                String value = lm.get(key);
                System.out.println(key + "---" + value);
            }
        }
    }
    输出结果(唯一和有序):
    2345---hello
    1234---javaee   (注意这里的javaee把旧的值world覆盖)
    3456---android (注意这里的andorid把旧的值java覆盖)
     
  • 相关阅读:
    hihoCoder 1148 2月29日
    Java 之常用运算符(3)
    Java 之变量和常量(2)
    Codeforces Round #414 A. Bank Robbery
    Codeforces Round #413 B. T-shirt buying
    C++中 set(集合容器)的用法
    Codeforces Round #411 B. 3-palindrome
    Codeforces Round #411 A. Fake NP
    Codeforces Round #413 A. Carrot Cakes
    Codeforces Round #412 B. T-Shirt Hunt
  • 原文地址:https://www.cnblogs.com/yangyquin/p/5066134.html
Copyright © 2011-2022 走看看