Java的LinkedHashSet、Map接口、可變參數、集合嵌套、
阿新 • • 發佈:2018-10-12
style map 但是 調用 obj ont 鍵值 etag 數組
1、LinkedHashSet:
(1)LinkedHashSet集合保證元素的存入和取出的順序;
package com.oracle.demo01; import java.util.HashSet; import java.util.LinkedHashSet; public class demo01 { public static void main(String[] args) { //不能存重復元素,但是LinkedHashSet是有序的。 LinkedHashSet<String> set = new LinkedHashSet<String>(); set.add("c"); set.add("b"); set.add("z"); for(String s:set){ System.out.println(s); } } }
效果如下:
(2)LinkedHashSet其實是HashSet的子類;
2、Map接口:
(1)元素是成對存在,每個元素由鍵與值兩部分組成,通過鍵可以找對所對應的值,鍵值對;
(2)Map中的集合不能包含重復的鍵,值可以重復;每個鍵只能對應一個值;
(3)遍歷:叠代器:不能直接遍歷map,遍歷set集合
第一種方法:keyset();第二種方法:entrySet();
用增強for或者是叠代器,不能用普通for循環,因為沒有下標概念;
增強for(){}
package com.oracle.demo01; import java.util.HashMap; import java.util.Map; import java.util.Set; public class demo02 { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("1", "a"); map.put("2","b"); map.put("2", "z"); map.put("3", "c"); //移除 map.remove("3"); //取數據 System.out.println(map.get("1")); System.out.println(map.get("2")); System.out.println(map.get("3")); //1、keySet遍歷 //先獲取map中的key所對應的set集合 Set<String> set = map.keySet(); //然後遍歷set找到每一個key值 for(String s:set){ String key = s; String value = map.get(key); System.out.println(key+".."+value); } //簡寫 for(String s:map.keySet()){ System.out.println(s+".."+map.get(s)); } //2、entryset()遍歷 Set<Map.Entry<String, String>> s1 = map.entrySet(); for(Map.Entry<String, String> e:s1){ System.out.println(e.getKey()+"..."+e.getValue()); } //簡寫 for(Map.Entry<String, String> e:map.entrySet()){ System.out.println(e.getKey()+"..."+e.getValue()); } } }
叠代器
package com.oracle.demo01; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class demo03 { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("鄧超", "孫儷"); map.put("李晨", "範冰冰"); map.put("文章", "馬伊琍"); map.put("小沈陽", "沈春陽"); //叠代器:不能直接遍歷map,遍歷set集合; //1、keyset() Set<String> s1 = map.keySet(); Iterator<String> it = s1.iterator(); while(it.hasNext()){ String key = it.next(); String value = map.get(key); System.out.println(key+">>"+value); } //2、entrySet() Set<Map.Entry<String, String>> s2 = map.entrySet(); Iterator<Map.Entry<String, String>> it2 = s2.iterator(); while(it2.hasNext()){ //獲取每一個"結婚證對象" Map.Entry<String, String> e =it2.next(); String key = e.getKey(); String value = e.getValue(); System.out.println(key+".."+value); } } }
(4)HashMap存儲自定義類型鍵值;保證對象唯一,必須復寫對象的hashCode和equals方法;
建立一個 人 類
package com.oracle.demo02; public class person { private String name; private Integer age; public person() { super(); } public person(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; person other = (person) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String toString() { return "姓名為" + name + ", 年齡為" + age ; } }
建立測試類
package com.oracle.demo02; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; public class demo01 { public static void main(String[] args) {
//通過方法對比看出 鍵不能重復
method01();
method02();
method03(); } public static void method01(){ Map<String, person> map = new HashMap<String, person>(); map.put("java0601", new person("a",21)); map.put("java61", new person("b",21)); map.put("java0806", new person("a",21)); map.put("java86", new person("b",21)); for(String s:map.keySet()){ System.out.println(s+"..."+map.get(s)); } } public static void method02(){ Map<person,String> map = new HashMap<person,String>(); map.put(new person("a",21),"java0601"); map.put(new person("b",21),"java61"); map.put(new person("a",21),"java0806"); map.put(new person("b",21),"java86"); for(person p:map.keySet()){ System.out.println(p+"..."+map.get(p)); } } public static void method03(){ LinkedHashMap<person,String> map = new LinkedHashMap<person,String>(); map.put(new person("a",21),"java0601"); map.put(new person("b",22),"java61"); map.put(new person("a",23),"java0806"); map.put(new person("b",24),"java86"); for(person p:map.keySet()){ System.out.println(p+"..."+map.get(p)); } } }
效果如下:
3、可變參數:
(1)修飾符 返回值類型 方法名(參數類型... 形參名){ };
package com.oracle.demo02; public class demo02 { public static void main(String[] args) { System.out.println(add(1)); System.out.println(add(1,1)); System.out.println(add(1,1,1)); } //想要做一個求和的方法,但是參數個數不一定,但是類型都是一定的Int public static int add(int...a){ //int[] b = a;相當於一個數組 int sum = 0; for(int i = 0;i < a.length;i++){ sum +=a[i]; } return sum; } }
效果如下:
(2)可變參數一定要寫在參數列表的末尾位置;
(3)調用的不確定性,只能傳一個可變參數;
4、集合嵌套:
用循環嵌套取值;
舉個例子:
package com.oracle.demo02; import java.util.HashMap; import java.util.Set; public class demo03 { public static void main(String[] args) { //HashMap<String, HashMap<String,String>> method01(); } public static void method01(){ HashMap<String, HashMap<String,String>> map = new HashMap<String, HashMap<String,String>>(); HashMap<String,String> m1 = new HashMap<String,String>(); m1.put("0601", "溫昭"); m1.put("0806", "馬榮"); HashMap<String,String> m2 = new HashMap<String,String>(); m2.put("0101", "張三"); m2.put("0202", "李四"); map.put("oracle01", m1); map.put("oracle02", m2); //第一層循環:獲取學校 Set<String> s1= map.keySet(); for(String s2:s1){ //學校名稱 String schoolname = s2; //獲取每一個班級 HashMap<String,String> s3 = map.get(s2); Set<String> s4 = s3.keySet(); for(String s5:s4){ //班級名稱 String classname = s5; //學生名稱: String studentname = s3.get(s5); System.out.println(schoolname+".."+classname+".."+studentname); } } } }
效果如下:
可以簡寫為:
//簡寫 for(String s:map.keySet()){ for(String s1:map.get(s).keySet()){ System.out.println("學校為"+s+",班級為"+s1 +"姓名為"+map.get(s).get(s1)); } }
Java的LinkedHashSet、Map接口、可變參數、集合嵌套、