zoukankan      html  css  js  c++  java
  • Map 实现类之:TreeMap(SortedMap的实现类) 和 Properties(Hashtable的实现类)

    TreeMap存储 Key-Value 对时,需要根据 key-value 对进行排序。
    TreeMap 可以保证所有的 Key-Value 对处于 有序状态。
    TreeSet底层使用 红黑树结构存储数据
    TreeMap 的 Key 的排序:
     自然排序:TreeMap 的所有的 Key 必须实现 Comparable 接口,而且所有
    的 Key 应该是同一个类的对象,否则将会抛出 ClasssCastException
     定制排序:创建 TreeMap 时,传入一个 Comparator 对象,该对象负责对
    TreeMap 中的所有 key 进行排序。此时不需要 Map 的 Key 实现
    Comparable 接口
     TreeMap判断 两个key 相等的标准:两个key通过compareTo()方法或
    者compare()方法返回0。

    //向TreeMap中添加key-value,要求key必须是由同一个类创建的对象
        //因为要按照key进行排序:自然排序 、定制排序
        //自然排序
        @Test
        public void test1(){
            TreeMap map = new TreeMap();
            User u1 = new User("Tom",23);
            User u2 = new User("Jerry",32);
            User u3 = new User("Jack",20);
            User u4 = new User("Rose",18);
    
            map.put(u1,98);
            map.put(u2,89);
            map.put(u3,76);
            map.put(u4,100);
    
            Set entrySet = map.entrySet();
            Iterator iterator1 = entrySet.iterator();
            while (iterator1.hasNext()){
                Object obj = iterator1.next();
                Map.Entry entry = (Map.Entry) obj;
                System.out.println(entry.getKey() + "---->" + entry.getValue());
    
            }
        }

    //定制排序
        @Test
        public void test2(){
            TreeMap map = new TreeMap(new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    if(o1 instanceof User && o2 instanceof User){
                        User u1 = (User)o1;
                        User u2 = (User)o2;
                        return Integer.compare(u1.getAge(),u2.getAge());
                    }
                    throw new RuntimeException("输入的类型不匹配!");
                }
            });
            User u1 = new User("Tom",23);
            User u2 = new User("Jerry",32);
            User u3 = new User("Jack",20);
            User u4 = new User("Rose",18);
    
            map.put(u1,98);
            map.put(u2,89);
            map.put(u3,76);
            map.put(u4,100);
    
            Set entrySet = map.entrySet();
            Iterator iterator1 = entrySet.iterator();
            while (iterator1.hasNext()){
                Object obj = iterator1.next();
                Map.Entry entry = (Map.Entry) obj;
                System.out.println(entry.getKey() + "---->" + entry.getValue());
    
            }
        }

    Map 实现类: 之五:Properties
    Properties 类是 Hashtable 的子类,该对象用于处理属性文件
    由于属性文件里的 key、value 都是字符串类型,所以 Properties 里的 key
    和 value 都是字符串类型
    存取数据时,建议使用setProperty(String key,String value)方法和
    getProperty(String key)方法
    Properties pros = new Properties();
    pros.load(new FileInputStream("jdbc.properties"));
    String user = pros.getProperty("user");
    System.out.println(user);

    //Properties:常用来处理配置文件。key和value都是String类型
    package com.atguigu.java;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * @author CH
     * @create 2021 下午 4:07
     */
    public class PropertiesTest {
    
        //Properties:常用来处理配置文件。key和value都是String类型
        public static void main(String[] args)  {
            FileInputStream fis = null;
            try {
                Properties pros = new Properties();
    
          fis = new FileInputStream("jdbc.properties");//加载文件输入流
                pros.load(fis);//加载流对应的文件
    
                String name = pros.getProperty("name");
                String password = pros.getProperty("password");
    
                System.out.println("name = " + name + ", password = " + password);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(fis != null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
            }
    
        }
    }

     

     
    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    struts2之JSP与Action的关系
    struts2之基本配置
    Android笔记之BroadCast判断网络状况
    Android笔记之监听左右滑动事件
    Android笔记之转到主线程运行
    springboot项目启动报错 Failed to configure a DataSource: 'url' attribute is not specified and no embedde
    centos6 初次安装成功,未显示eth0网卡的信息
    layerui上传文件
    判断当前浏览器类型
    jquery重置下拉框
  • 原文地址:https://www.cnblogs.com/CCTVCHCH/p/14812847.html
Copyright © 2011-2022 走看看