Java_Map集合(双列集合)

一、导读

        这一章我们学习Map集合,我们学习HashMap、LinkedHashMap、TreeMap、Properties.

二、HashMap

认识HashMap

package 基础.week2.day12.hashMap;

import java.util.HashMap;

public class Demo_example14 {
    public static void main(String[] args) {
        //HashSet介绍
        HashMap map=new HashMap();
        map.put("1","张三");
        map.put("2","李四");
        map.put("3","王五");
        System.out.println(map);
        map.put("3","赵六");
        System.out.println(map);
    }
}

{1=张三, 2=李四, 3=王五}
{1=张三, 2=李四, 3=赵六}

Process finished with exit code 0
 

遍历HashMap中的键和值

 

package 基础.week2.day12.hashMap;

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

public class Demo_example15 {
    public static void main(String[] args) {
        //HashMap遍历所有的键和值的方法
        //第一种方式:先遍历map集合中所有的键,再根据键拿值
        HashMap map=new HashMap();
        map.put("1","张三");
        map.put("2","李四");
        map.put("3","王五");
        Set set = map.keySet();//键的集合
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Object key = iterator.next();
            Object value= map.get(key);//键拿值
            System.out.println(key+":"+value);
        }
        System.out.println("======");

        //第二种方式:先获取集合中所有的映射关系,再从映射关系中拿键和值
        Set set1 = map.entrySet();
        Iterator iterator1 = set1.iterator();
        while (iterator1.hasNext()){
            Map.Entry next = (Map.Entry) iterator1.next();
            Object key = next.getKey();
            Object value = next.getValue();
            System.out.println(key+":"+value);
        }
    }
}

1:张三
2:李四
3:王五
======
1:张三
2:李四
3:王五
 

HashMap其他常用方法

package 基础.week2.day12.hashMap;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class Demo_example17 {
    public static void main(String[] args) {
        //HashMap中的其他方法
        HashMap map=new HashMap();
        map.put("1","张三");
        map.put("2","李四");
        map.put("3","王五");
        map.put("4","赵六");

        System.out.println("集合大小:"+map.size());
        System.out.println("判断是否包含传入的键:"+map.containsKey("1"));
        System.out.println("判断是否包含传入的值:"+map.containsValue("张三"));
        System.out.println("删除键为1的值:"+map.remove("1"));
        Collection values = map.values();
        Iterator iterator = values.iterator();
        while (iterator.hasNext()){
            Object value = iterator.next();
            System.out.println(value);
        }
    }
}

 

集合大小:4
判断是否包含传入的键:true
判断是否包含传入的值:true
删除键为1的值:张三
李四
王五
赵六

Process finished with exit code 0
 

三、LinkedHashMap

认识LinkedHashMap

LinkedHashMap加入有序,HashMap加入无序,看一段代码:

package 基础.week2.day12.hashMap;

import java.util.*;

public class Demo_example18 {
    public static void main(String[] args) {
        //初认识LinkedHashMap
        System.out.println("===HashMap的遍历方式===");
        HashMap map1=new HashMap();
        map1.put("2","李四");
        map1.put("1","张三");
        map1.put("3","王五");
        Set set1 = map1.entrySet();
        Iterator it1 = set1.iterator();
        while (it1.hasNext()){
            Map.Entry entry = (Map.Entry) it1.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key+":"+value);
        }

        System.out.println("===LinkedHashMap的遍历方式===");
        LinkedHashMap map=new LinkedHashMap();
        map.put("2","王五");
        map.put("1","张三");
        map.put("3","王五");
        Set set = map.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()){
            Map.Entry entry= (Map.Entry) it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key+":"+value);
        }

    }
}

 

===HashMap的遍历方式===
1:张三
2:李四
3:王五
===LinkedHashMap的遍历方式===
2:王五
1:张三
3:王五

Process finished with exit code 0
 

 四、TreeMap

认识TreeMap

package 基础.week2.day12.hashMap;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class Demo_example19 {
    public static void main(String[] args) {
        //初认识TreeMap
        TreeMap map=new TreeMap();
        map.put("2","张三");
        map.put("1","李四");
        map.put("4","王五");
        Set set = map.keySet();
        Iterator it = set.iterator();
        while (it.hasNext()){
            Object key = it.next();
            Object value = map.get(key);
            System.out.println(key+":"+value);

        }
    }
}

1:李四
2:张三
4:王五

Process finished with exit code 0
 

TreeMap的比较排序法

student类:
package 基础.week2.day12.hashMap;

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

    public Student() {
    }

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

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Student{name = " + name + ", age = " + age + "}";
    }
}
 测试类:
package 基础.week2.day12.hashMap;

import java.util.*;

public class Demo_example20 {
    public static void main(String[] args) {
        TreeMap map = new TreeMap(new Comparator<Student>() {
            @Override
            public int compare(Student student1, Student student2) {
                //需求:先按照年龄进行排序,年龄相同再按照姓名进行排序
                int num1= student1.getName().compareTo(student2.getName());//按照姓名进行排序
                int num2=student1.getAge()-student2.getAge();//按照年龄进行排序
                return num2==0?num1:num2;//年龄相同就按照姓名排序
            }
        });
        map.put(new Student("张三", 23), "1");
        map.put(new Student("李四", 25), "2");
        map.put(new Student("王五", 45), "3");
        map.put(new Student("李其", 25), "4");
        Set set = map.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key + ":" + value);
        }

    }
}

Student{name = 张三, age = 23}:1
Student{name = 李其, age = 25}:4
Student{name = 李四, age = 25}:2
Student{name = 王五, age = 45}:3

Process finished with exit code 0
 

五、Properties

认识properties

package 基础.week2.day12.hashMap;

import java.util.Enumeration;
import java.util.Properties;

public class Demo_example21 {
    public static void main(String[] args) {
        //认识Properties
        //Properties在实际开发中主要用来存储应用的配置项
        Properties p=new Properties();
        p.setProperty("Brand","xiaomi");
        p.setProperty("Color","white");
        p.setProperty("Memory","16+512G");
        Enumeration names = p.propertyNames();//获取所有键的枚举
        while (names.hasMoreElements()){
            String key = (String)names.nextElement();
            String value = p.getProperty(key);
            System.out.println(key+":"+value);

        }
    }
}

 

Brand:xiaomi
Color:white
Memory:16+512G

Process finished with exit code 0
 

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值