练习20——HashMap、LinkedHashMap、TreeMap、Properties类的应用

package test4;

import org.junit.Test;
import test2.Person;

import java.util.*;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author abu
 * @create 2019/7/23
 * @since 1.0.0
 */
public class TestMap {
    @Test
    public void test1(){
        Map map = new HashMap();
        map.put("aa",123);
        map.put("aa",12222);
        map.put("bb",456);
        map.put("cc",789);
        map.put(123,"ab");
        map.put(1234,"aa");
        map.put(null,null);
        map.put(new Person("dd",24),10);
        map.put(new Person("dd",24),10);

        System.out.println(map.get("ab"));
        System.out.println(map.get(1234));

        System.out.println(map.size());
        map.remove(null);
        System.out.println(map);
        System.out.println(map.size());

        Map t = new HashMap();
        t.put("aa",123);
        t.put("cc",12);
        t.put("ee",456);
        //map.putAll(t);
        t.putAll(map);
        System.out.println(map);
        System.out.println(t);
        System.out.println(t.get("aa"));
        t.clear();
        System.out.println(t);
        System.out.println(map.get("aa"));

        Boolean b1 = t.containsKey("ab");
        Boolean b2 = map.containsValue(12);
        Boolean b3 = t.isEmpty();
        Boolean b4 = t.equals(map);
        System.out.println(b1 + "\t" + b2 + "\t" + b3 + "\t" + b4);

    }

    @Test
    public void test2(){
        Map map = new HashMap();
        map.put("aa",123);
        map.put("aa",12222);
        map.put("bb",456);
        map.put("cc",789);
        map.put(123,"aa");
        map.put(null,null);
        map.put(new Person("dd",24),10);
        //遍历key值
        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //遍历value值
        Collection value = map.values();
        for(Object obj : value){
            System.out.println(obj);
        }
        //遍历key-value对
        Set set1 = map.keySet();
        for (Object obj : set1){
            System.out.println(obj + "--->" + map.get(obj));
        }
        //遍历key-value对
        Set set2 = map.entrySet();
        for(Object obj : set2){
            Map.Entry entry = (Map.Entry)obj;
            //System.out.println(entry.getKey() + "--->" + entry.getValue());
            System.out.println(entry);
        }
    }
    @Test
    public void test3(){
        //LinkedHashMap使用链表维护添加进map中的顺序,因此遍历时是按照添加顺序
        Map map = new LinkedHashMap();
        map.put("bb",456);
        map.put("aa",123);
        map.put("aa",12222);

        map.put("cc",789);
        map.put(123,"aa");
        map.put(null,null);
        map.put(new Person("dd",24),10);

        Set set = map.entrySet();
        for(Object obj : set){
            Map.Entry entry = (Map.Entry)obj;
            //System.out.println(entry.getKey() + "--->" + entry.getValue());
            System.out.println(entry);
        }

    }

    @Test
    public void test4(){
        //TreeMap,按照添加进map中的key的指定属性排序(类比TreeSet),因此要求key必须是同一类的对象
        Map map = new TreeMap();
        map.put("bb",456);
        map.put("aa",123);
        map.put("aa",12222);

        map.put("cc",789);
        map.put(123,"aa");
        map.put(null,null);
        map.put(new Person("dd",24),10);

        Set set = map.entrySet();
        for(Object obj : set){
            Map.Entry entry = (Map.Entry)obj;
            //System.out.println(entry.getKey() + "--->" + entry.getValue());
            System.out.println(entry);
        }

    }
    @Test
    public void test5(){
        //TreeMap的定制排序
        Comparator com = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Employee1 && o2 instanceof Employee1){
                    Employee1 e1 = (Employee1)o1;
                    Employee1 e2 = (Employee1)o2;

                    MyDate1 birth1 = e1.getBirthday();
                    MyDate1 birth2 = e2.getBirthday();
                    if (birth1.getYear() != birth2.getYear()){
                        return birth1.getYear() - birth2.getYear();
                    }else{
                        if(birth1.getMonth() != birth2.getMonth()){
                            return birth1.getMonth() - birth2.getMonth();
                        }else{
                            if(birth1.getDay() != birth2.getDay()){
                                return birth1.getDay() - birth2.getDay();
                            }else {
                                return e1.getName().compareTo(e2.getName());
                            }
                        }
                    }
                }
                return 0;
            }
        };
        Map map = new TreeMap(com);
        map.put(new Employee1("余淮",35,new MyDate1(1984,06,05)),10);
        map.put(new Employee1("阿芳",35,new MyDate1(1984,06,05)),20);
        map.put(new Employee1("耿耿",35,new MyDate1(1984,06,05)),30);
        map.put(new Employee1("余周周",36,new MyDate1(1983,01,26)),40);
        map.put(new Employee1("林扬",37,new MyDate1(1982,03,28)),50);
        map.put(new Employee1("温淼",34,new MyDate1(1985,07,04)),60);

        Set set = map.entrySet();
        for (Object obj : set){
            Map.Entry entry = (Map.Entry) obj;
            System.out.println(entry);
        }
    }
    @Test
    public void test6() throws FileNotFoundException,IOException{
        Properties properties = new Properties();
        properties.load(new FileInputStream(new File("jdbc.properties")));
        String user = properties.getProperty("user");
        System.out.println(user);
        String password = properties.getProperty("password");
        System.out.println(password);


    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值