一、Map常用方法簡介 package com.bjpowernode.java_learning; import java.util.HashMap; import java.util.*; public class D91_1_MapCommonMethod { public stati ...
一、Map常用方法簡介
package com.bjpowernode.java_learning; import java.util.HashMap; import java.util.*; public class D91_1_MapCommonMethod { public static void main(String[] args) { Map persons = new HashMap();//HashMap的預設初始化容量為16,預設載入因數時0.75 persons.put("10000","JACK"); persons.put("10001","ZHANGXINLEI"); persons.put("10002","YAOKUN"); persons.put("10003","ZHAOZEKUN"); persons.put("10004","LIDUO"); persons.put("10005","DIAOBI"); //1.判斷集合中是否包含這樣的value //註意:如果Map中的key重覆了,value採用的是“覆蓋” System.out.println(persons.containsValue("LUCK")); //2.通過key來獲取value String k = "10001"; Object v = persons.get(k); System.out.println(v); //3.通過key刪除鍵值對 persons.remove("10002"); System.out.println(persons.get("10002")); //4.獲取所有的value Collection values = persons.values(); Iterator it = values.iterator(); while(it.hasNext()) { System.out.println(it.next()); } //5.獲取所有的key //以下程式演示遍歷Map集合 Set keys = persons.keySet(); Iterator it2 = keys.iterator(); while(it2.hasNext()) { Object k2 = it2.next(); Object v2 = persons.get(k2); System.out.println(k2+"-->"+v2); } System.out.println("--------------------------------------"); //6.entrySet將Map集合key和value都列印出來,中間使用等號進行連接 Set s2 = persons.entrySet(); Iterator i3 = s2.iterator(); while(i3.hasNext()) { System.out.println(i3.next()); } } }
二、Hashtable
1.HashMap預設初始化容量是16,預設載入因數為0.75
2.Hashtable預設初始化容量是11,預設載入因數為0.75
package com.bjpowernode.java_learning; import java.util.*; public class D91_2_Hashtable_Propet { public static void main(String[] args) { //1.創建屬性類對象 Properties p = new Properties(); //2.存 //註意key是不能重覆,如果重覆就會造成value覆蓋 p.setProperty("driver","oracle.jdbc.driver.OracleDriver"); p.setProperty("username","scott"); p.setProperty("password","tiger"); p.setProperty("url","jdbc:oracle:thin:@192.167.1.100:1521:bjpowernode"); //3.取出來,通過key獲取value String v1 = p.getProperty("driver"); String v2 = p.getProperty("username"); String v3 = p.getProperty("password"); System.out.println(v1); System.out.println(v2); System.out.println(v3); } }
三、SortedMap
1.SortedMap中的key特點:無序不可以重覆,但是存進去的元素可以按照大小進行排列。
2.如果想要自動排序,key部分的元素需要:(1)實現Comparable介面或者(2)單獨寫一個比較器
package com.bjpowernode.java_learning; import java.util.*; public class D91_3_TreeMap { public static void main(String[] args) { //Map,key存儲Product91,value存儲個數 SortedMap products = new TreeMap(); /** * 匿名內部類(單獨寫一個比較器的方法) * SortedMap products = new TreeMap(new Comparator(){ * public int compareTo(Object o) { * double price1 = this.price; * double price2 = ((Product91)o).price; * if(price1<price2) { * return -1; * }else if(price1>price2) { * return 1; * * }else { * return 0; * } * } *}) */ //準備對象 Product91 p1 = new Product91("西瓜",1.0); Product91 p2 = new Product91("黃瓜",2.0); Product91 p3 = new Product91("南瓜",3.0); Product91 p4 = new Product91("冬瓜",4.0); //添加 products.put(p1,8);//後面這個value無所謂,我們暫且認為是斤數 products.put(p2,4); products.put(p3,4); products.put(p4,4); //遍歷 Set keys = products.keySet(); Iterator it = keys.iterator(); while(it.hasNext()) { Object k = it.next(); Object v =products.get(k); System.out.println(k+"-->"+v); } } } class Product91 implements Comparable{ String name; double price; Product91(String name,double price){ this.name = name; this.price = price; } public String toString() { return "Poduct91(name=" + name + ",price=" + price +")"; } public int compareTo(Object o) { double price1 = this.price; double price2 = ((Product91 )o).price; if(price1<price2) { return -1; }else if(price1>price2) { return 1; }else { return 0; } } }
四、源碼:
D91_1_MapCommonMethod.java
D91_2_Hashtable_Propet.java
D91_3_TreeMap.java
https://github.com/ruigege66/Java/blob/master/D91_1_MapCommonMethod.java
https://github.com/ruigege66/Java/blob/master/D91_2_Hashtable_Propet.java
https://github.com/ruigege66/Java/blob/master/D91_3_TreeMap.java
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關註微信公眾號:傅里葉變換,個人公眾號,僅用於學習交流,後臺回覆”禮包“,獲取大數據學習資料