V哥:Map介绍与使用

package com.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

public class MapTest01 {
    public static void main(String[] args) {
        Map persons = new HashMap();
        //put添加key-value
        persons.put("aaa",2);
        persons.put("ccc",6);
        persons.put("bbb",4);
        System.out.println(persons);
        //是否包含key
        System.out.println(persons.containsKey("aaa"));
        //是否包含value
        System.out.println(persons.containsValue("6"));
        //
        Set<Map.Entry> set= persons.entrySet();
        set.forEach(entry -> System.out.println(entry.getKey() + "=" + entry.getValue()));

        System.out.println("==============");
        //get得到的是值
        System.out.println(persons.get("aaa"));
        System.out.println("==============");
        //java8之前遍历我们的lap的办法
        System.out.println(persons.keySet());
        //增强遍历每一个元素
        for (Object key : persons.keySet()) {
            System.out.println(key+"="+persons.get(key));
        }
        System.out.println("==============");
        //putAll将m中的k-v值复制到调用的map中
        Map newps = new HashMap();
         newps.putAll(persons);
        System.out.println(persons);
        System.out.println(newps);
        System.out.println("==============");
        //remove删除对应的key值
        persons.remove("ccc");
        System.out.println(persons);
        System.out.println(persons.values());
        System.out.println("==============");
        //isempty判断是否为空,空为true,其他为false
        System.out.println(persons.isEmpty());
        System.out.println(persons);
        System.out.println("==============");
        
    }
}

package com.map;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

public class MapJava8 {
    public static void main(String[] args) {
        Map persons = new HashMap();
        //put添加key-value
        persons.put("aaa",2);
        persons.put("ccc",6);
        persons.put("bbb",4);

        //compute方法使用
        persons.compute("aaa", new BiFunction() {
            @Override
            public Object apply(Object key, Object value) {
                int oldValue = (int) value;

                if (oldValue < 5){
                    oldValue = oldValue + 5;
                }
                return oldValue;
            }
        });

        persons.put("rrr",null);
        //computeIfAbsent当value为空时执行
        persons.computeIfAbsent("rrr", new Function() {
            @Override
            public Object apply(Object o) {
                System.out.println(o);
                return 32;
            }
        });

        System.out.println(persons);
        System.out.println("====================");
        //foreach循环
        persons.forEach(new BiConsumer() {
            @Override
            public void accept(Object key, Object value) {
                persons.compute(key, new BiFunction() {
                    @Override
                    public Object apply(Object key, Object value) {
                        int oldValue = (int) value;

                        if (oldValue < 5){
                            oldValue = oldValue + 5;
                        }
                        return oldValue;
                    }
                });
            }
        });
        System.out.println(persons);
        System.out.println("============");
        //getOrDefault当出现是显示木有是显示default
        persons.getOrDefault("bbb",34);
        System.out.println("============");
        //replace对原key-value值进行计算然后将计算结果为value值返回
        persons.replaceAll(new BiFunction() {
            @Override
            public Object apply(Object key, Object value) {
                return  (int)value - 2;
            }
        });
        System.out.println(persons);
    }
}

 

 

 

 

 返回为true,因为通过equals来进行

package com.map;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public class LinkedHashMapTest {
    public static void main(String[] args) {
        //linkedhashmap将插入顺序维护
        Map persons = new LinkedHashMap();
        //put添加key-value
        persons.put("aaa",2);
        persons.put("ccc",6);
        persons.put("bbb",4);
        persons.forEach(new BiConsumer() {
            @Override
            public void accept(Object key, Object value) {
                System.out.println(key+"\t"+value);
            }
        });
    }
}

package com.map;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        //读取文件操作
        properties.load(new FileReader("C:\\Users\\V556U\\Desktop\\set\\test.ini"));
        System.out.println(properties);
        //getProperty将其读取
        System.out.println("username="+properties.getProperty("username"));
        System.out.println("password="+properties.getProperty("password"));
        //setProperty修改 必须是String类型
        properties.setProperty("username","root");
        properties.setProperty("password","0000");
        properties.setProperty("age","23");
        //在设定之后需要通过store写入新文件
        properties.store(new FileWriter("C:\\Users\\V556U\\Desktop\\set\\test1.ini"),"this is my Property");
    }
}

9、SoretedMap接口 和TreeMap ​​​​​​实现类

package com.map;

import java.util.Map;
import java.util.TreeMap;

public class TreeMapTest {
    private static class A implements Comparable{
        int count;

        public A(int count) {
            this.count = count;
        }

        @Override
        public String toString() {
            return "A{" + "count=" + count + '}';
        }

        @Override
        public int compareTo(Object o) {
            A a = (A) o;
            return count - a.count;
        }
    }
    public static void main(String[] args) {
        TreeMap treeMap = new TreeMap();
        treeMap.put(new A(9),"aaa");
        treeMap.put(new A(7),"sss");
        treeMap.put(new A(3),"ggg");
        treeMap.put(new A(8),"jjj");
        System.out.println(treeMap);
        //最小值
        Map.Entry firstEntry = treeMap.firstEntry();
        System.out.println(firstEntry);
        System.out.println(firstEntry.getKey() + "----->" + firstEntry.getValue());
        //同样使用
        System.out.println(treeMap.firstEntry());
        //最后一个值
        System.out.println("lastKey is "+treeMap.lastKey());
        //第一个值
        System.out.println(treeMap.firstKey());
        //后面的一个键值与结果
        System.out.println(treeMap.higherEntry(new A(3)));

    }
}

package com.map;

import java.util.EnumMap;

public class EnumMapTest {
    private enum SEASON{
        SPRING,SUMMER,FALL,WINTER
    }
    public static void main(String[] args) {
        EnumMap enumMap = new EnumMap(SEASON.class);
        enumMap.put(SEASON.SPRING,"ssss");
        enumMap.put(SEASON.SUMMER,"ssss");
        enumMap.put(SEASON.FALL,"ssss");
        enumMap.put(SEASON.WINTER,"ssss");
        System.out.println(enumMap);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值