• 05集合HashMap and Properties and TreeSet and Collections


    1、HashMap

    /*
           * HashMap集合key部分允许null吗?
           * 允许
           * 但是注意:HashMap集合的key = null的值仅仅允许存在一个
           *
           *
           * HashTable的key可以为null吗?
           * HashTable的key和value都是不能为null的
           * HashTable方法都带有synchronized:线程安全的
           * 线程安全有其他的方案,这个HashTable对线程的处理,导致效率比较低,使用较少了
           *
           * Hashtable和HashMap一样,底层都是哈希表数据结构
           * Hashtable初始化容量是11,然后默认的加载因子是0.75
           * Hashtable的扩容量:原容量 * 2 + 1
           *
           * Properties相关的方法即可
           * Properties是一个Map集合,继承Hashtable,Properties的key和value都是String类型
           *
           * */
    
            Map map = new HashMap();
    
            //HashMap集合允许key为null
            map.put(null,null);
            System.out.println(map.size());
    
            //key重复的话value覆盖
            map.put(null,1);
            System.out.println(map.size());
    
            //通过key = null获取value
            System.out.println(map.get(null));
    
            //HashTable
            Map table = new Hashtable();
    
            //table.put(null,"123");
            table.put(100,null);   //值为空,这是不可以的
            table.put(null,10000); //键为空,这是不可以的

    2、Properties类

     /*
            * 掌握Properties属性类相关的方法即可
            * Properties是一个map集合,继承Hashtable,其中key和value都是String类型
            * Properties被称为属性类对象
            * Properties是线程安全的
            * 
            * 
            * */
            
           //创建一个Properties对象
            Properties pro = new Properties();
    
            //需要掌握Properties的两个方法,一个存,一个取
            pro.setProperty("url1","jdbc://localhost:3306");
            pro.setProperty("driver","com.mysql.jdbc.Driver");
            pro.setProperty("username","root");
            pro.setProperty("password","123");
    
            //通过key获取value
            String url1 = pro.getProperty("url1");
            String driver = pro.getProperty("driver");
            String username =  pro.getProperty("username");
            String password = pro.getProperty("password");
    
            System.out.println(url1);
            System.out.println(driver);
            System.out.println(username);
            System.out.println(password);

    3、TreeSet

     /*
           * TreeSet集合底层实际是一个TreeMap
           * TreeSet集合底层是一个二叉树
           * 放到TreeSet集合中元素,等同于放到TreeMap集合key部分
           * TreeSet集合中的元素,无序不可重复,但是可以按照元素的大小自动排序
           * 称为:可排序集合
           *
           *
           * */
            //创建集合
            TreeSet<String> tree = new TreeSet<>();
    
            //添加数据
            tree.add("abc");
            tree.add("okp");
            tree.add("koko");
    
            //输出数据
            for(String s:tree){
                //升序排列
                System.out.println(s);
            }
    
            //TreeSet如何对自定义类型排序
            //继承了Comparable,实现了 public int compareTo(Object o)方法,改变了比较方式
            TreeSet<Student> treeSet = new TreeSet<>();
    
            //添加数据
            treeSet.add(new Student(67));
            treeSet.add(new Student(34));
            treeSet.add(new Student(12));
    
            //输出数据
            for(Student s:treeSet){
                //升序
                System.out.println(s);
            }

     /*
            * TreeSet集合中元素可排序的第二种方式:使用比较器的方式
            * 最终的结论:
            *   放到TreeSet或者TreeMap集合key部分的元素
            *
            * Comparable 和 Comparator怎么选择?
            * 当比较规则发生改变的时候,或者当比较规则只有1个的时候,建议实现Comparable接口
            * 如果比较规则有多个,并且需要多个比较规则之间频繁切换,建议使用Comparator接口,符合OCP原则
            * */
            TreeSet<Student> tree = new TreeSet<>(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.age - o2.age;
                }
            });

     4、Collections

      /*
           * java.util.Collection   集合接口
           * java.util.Collections   集合工具类,方便集合的操作
           * 对List中集合排序,要保证List集合中的元素实现了Comparable接口
           * */
            //ArrayList集合不是线程安全的
            List<String> list = new ArrayList<>();
    
            //变成线程安全的
            Collections.synchronizedList(list);
    
            //排序
            list.add("abcf");
            list.add("abx");
            list.add("abe");
    
            //输出
            Collections.sort(list);
            for(String s:list){
                System.out.println(s);
            }
    
            //自定义
            List<Student> lst = new ArrayList<>();
            lst.add(new Student(100));
            lst.add(new Student(45));
            lst.add(new Student(134));
    
            //对集合中元素排序
            Collections.sort(lst);
            for(Student stu: lst){
                System.out.println(stu);
            }
            //Collections.sort(List集合,比较器)也是可以的
        }
  • 相关阅读:
    贪心算法
    分治法
    递归法
    查找二 树与图的搜索
    (转载)查找三 哈希表的查找
    (转载)查找一 线性表的查找
    4.写出完整版的strcpy函数
    3.strcpy使用注意(3)
    2.strcpy使用注意(2)
    1.strcpy使用注意
  • 原文地址:https://www.cnblogs.com/xiaoming521/p/15841293.html
Copyright © 2020-2023  润新知