java HashMap和HashTable區別,Collection常見方法

來源:互聯網
上載者:User

標籤:sort   put   sys   rgs   eve   collect   bin   key   ring   

    /**     * @param args     * 面試題     * HashMap和Hashtable的區別     * 共同點:     * 底層都是雜湊演算法,都是雙列集合     * 區別:     * 1,HashMap是線程不安全的,效率高,JDK1.2版本     *   Hashtable是安全執行緒的,效率低,JDK1.0版本的     * 2,HashMap可以儲存null鍵和null值     *   Hashtable不可以儲存null鍵和null值     */    public static void main(String[] args) {        HashMap<String, Integer> hm = new HashMap<>();        hm.put(null, 23);        hm.put("李四", null);        System.out.println(hm);                /*Hashtable<String, Integer> ht = new Hashtable<>();        //ht.put(null, 23);        ht.put("張三", null);        System.out.println(ht);*/                        System.out.println("1111111111111");

=============================================================================================================================
/**
* Collecitons中的常見方法 * public static <T> void sort(List<T> list) public static <T> int binarySearch(List<?> list,T key) public static <T> T max(Collection<?> coll) public static void reverse(List<?> list) public static void shuffle(List<?> list) */ public static void main(String[] args) { //demo1(); //demo2(); ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("c"); list.add("d"); list.add("g"); list.add("f"); //System.out.println(Collections.max(list)); //根據預設排序結果擷取集合中的最大值 //Collections.reverse(list); //反轉集合 Collections.shuffle(list); //隨機置換,可以用來洗牌 System.out.println(list); } public static void demo2() { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("c"); list.add("d"); list.add("f"); list.add("g"); System.out.println(Collections.binarySearch(list, "c")); System.out.println(Collections.binarySearch(list, "b")); } public static void demo1() { ArrayList<String> list = new ArrayList<>(); list.add("c"); list.add("a"); list.add("a"); list.add("b"); list.add("d"); System.out.println(list); Collections.sort(list); //將集合排序 System.out.println(list); }

 

 

java HashMap和HashTable區別,Collection常見方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.