zoukankan      html  css  js  c++  java
  • 《疯狂Java讲义》(二十六)---- Map

    • HashMap

    HashMap里的key不能重复,所以HashMap里最多只有一个key-value的key为null,但可以无数多个key-value对的value为null。

    为了成功地在HashMap中存储获取对象,用作key的对象必须实现hashCode和equals方法

    • 使用Properties类读写属性文件
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class PropertiesTest {
    
        public static void main(String[] args) throws FileNotFoundException, IOException {
            // TODO Auto-generated method stub
    
            Properties props = new Properties();
            props.setProperty("username", "ivy");
            props.setProperty("password", "12345");
            props.put("fruit", "apple");
            props.store(new FileOutputStream("a.ini"), "comment line");
            Properties props2 = new Properties();
            props2.setProperty("gender", "famale");
            props2.load(new FileInputStream("a.ini"));
            System.out.println(props2);
        }
    
    }

    输出结果:

    {password=12345, fruit=apple, gender=famale, username=ivy}

    • Map性能分析

    HashMap vs Hashtable

      HashMap快,因为Hashtable需要额外的线程同步控制。

      HashMap线程不安全,Hashtable线程安全。

    TreeMap vs HashMap

      TreeMap慢,因为需要使用红黑树管理key-value对。

      TreeMap的key-value处于有序状态,无需专门进行排序操作。

      一般应用多使用HashMap,只有程序需要排序的Map时,考虑TreeMap

    LinkedHashMap vs HashMap

      LinkedHashMap慢,因为需要维护链表。

  • 相关阅读:
    iOS中循环引用的解除
    Block的循环引用详解
    Mac OS X下面 Node.js环境的搭建
    swift中闭包和OC的block的对比
    STL priority_queue
    优先使用map(或者unordered_map)的find函数而非algorithm里的find函数
    Insert Interval
    Integer Break
    Unique Binary Search Trees
    腾讯2016实习生笔试
  • 原文地址:https://www.cnblogs.com/IvySue/p/6380639.html
Copyright © 2011-2022 走看看