(JAVA)Map集合
生活随笔
收集整理的這篇文章主要介紹了
(JAVA)Map集合
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package map.demo;import java.util.*;/*** @author Alina* @date 2021年09月25日 11:20 下午* 底層原理是哈希表,保證唯一性* 允許存儲(chǔ)null鍵,null值* 線(xiàn)程不安全,運(yùn)行速度快* keyset()可以獲取到鍵* Collections 類(lèi)的方法reverseOrder :調(diào)用空參數(shù),返回比較器,逆轉(zhuǎn)對(duì)象的自有順序**/
public class HashMapDemo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);HashMap<Student,String> ha = new HashMap<>();System.out.println("請(qǐng)輸入學(xué)生的姓名,年齡,籍貫!");System.out.println("例如:'張三 37 黑龍江' ,以空格作為分隔符,以over結(jié)束");while (true){String student = sc.nextLine();String [] studentInfo = student.split(" +");if (student.equalsIgnoreCase("over")){keySet(ha);break;}else{ha.put(new Student(studentInfo[0],Integer.parseInt(studentInfo[1])),studentInfo[2]);}}}public static void keySet(HashMap<Student,String> mp){Set<Student> s = mp.keySet();Iterator<Student> it = s.iterator();while (it.hasNext()){Student key = it.next();String value = mp.get(key);System.out.println(key+ " "+ value);}}
}package map.demo;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;/*** @author Alina* @date 2021年09月23日 9:03 下午* Map集合,可以存儲(chǔ)兩個(gè)集合,一個(gè)鍵值對(duì)* 1.不繼承Collection* 2.鍵保持唯一性,值可以重復(fù)* 3.Map接口中的方法* 4.引用類(lèi)型獲取不到值,返回NILL,基本類(lèi)型,獲取不到值返回負(fù)數(shù)* 5.map集合的獲取第一種:Map集合依賴(lài)于Set集合,可以使用Map集合的Set方法,并迭代****/
public class Mapdemo {public static void main(String[] args) {method_4();}/***** @author Alina* @date 2021/9/25 10:07 下午* Map集合中,有一個(gè)類(lèi)描述映射關(guān)系* 鍵值對(duì)關(guān)系對(duì)象的描述接口,是Map接口的內(nèi)部接口* interface Map{* interface Entry{* K getKey();* V getValue();* }* }* 案例使用的是HashMap實(shí)現(xiàn)* class HashMap implements Map{* class Entry implements Map.Entry{* public k getKey();* public V getValue();* }* }* 實(shí)現(xiàn)步驟:* 1.Map集合的方法entrySet()方法獲取鍵值對(duì)關(guān)系對(duì)象-->Map.Entry接口的實(shí)現(xiàn)類(lèi)* 將Map.Entry接口實(shí)現(xiàn)類(lèi)對(duì)象,存儲(chǔ)到Set集合* 2.迭代Set集合* 3.獲取Set結(jié)合,存儲(chǔ)的是Map.Entry對(duì)象 -->Map集合中的鍵值對(duì)的映射關(guān)系* 4.使用返回的ap.Entry對(duì)象getKey getValue 獲取鍵值對(duì)*/public static void method_4(){Map<String,Integer> m = new HashMap<>();m.put("a",21);m.put("b",43);m.put("c",90);Set<Map.Entry<String,Integer>> st = m.entrySet();Iterator <Map.Entry<String,Integer>> it = st.iterator();while (it.hasNext()){Map.Entry<String,Integer> mp = it.next();String key = mp.getKey();Integer value = mp.getValue();System.out.println("key:"+key+" "+"value:"+value);}}/*** * @author Alina* @date 2021/9/25 10:00 下午* 迭代Map集合,Map集合依賴(lài)于Set集合,可以使用迭代器* keySet()方法可以獲取鍵*/public static void method_3(){Map<String,Integer> m = new HashMap<>();m.put("a",21);m.put("b",43);m.put("c",90);Set<String> s = m.keySet();Iterator <String> it = s.iterator();while (it.hasNext()){String key = it.next();Integer value = m.get(key);System.out.println("key:"+key+" "+"value:"+value);}}/*** * @author Alina* @date 2021/9/25 9:23 下午* 判斷集合中是否含有某元素*/public static void method_2(){Map<String,Integer> ht = new HashMap<>();Integer a = ht.put("a",1);Integer b = ht.put("b",2);Integer c = ht.put("c",3);Integer d = ht.put("d",4);Integer i = ht.get("c");//判斷當(dāng)前鍵中是否有某鍵boolean a1 = ht.containsKey("a");//判斷值中是否包含某值boolean b1 = ht.containsValue(3);System.out.println(a1+" "+b1);}/**** @author Alina* @date 2021/9/25 9:24 下午* 使用Map集合依賴(lài)于實(shí)現(xiàn)類(lèi)HashMap實(shí)現(xiàn)類(lèi)*/public static void method(){//Map接口對(duì)實(shí)現(xiàn)類(lèi)HashMapMap<String,Integer> h = new HashMap<>();//鍵值對(duì)存儲(chǔ)到集合的方法,-->putInteger a = h.put("a",1);System.out.println(a);System.out.println(h);//獲取鍵對(duì)應(yīng)的值}/**** @author Alina* @date 2021/9/25 9:26 下午* 往Map類(lèi)存儲(chǔ)*/public static void method_1(){Map<String,Integer> ht = new HashMap<>();Integer a = ht.put("a",1);Integer b = ht.put("b",2);Integer c = ht.put("c",3);Integer d = ht.put("d",4);Integer i = ht.get("c");System.out.println("ht:"+ht);System.out.println(i);}
}
package map.demo;import java.util.TreeMap;/*** @author Alina* @date 2021年10月02日 4:27 下午* 獲取一個(gè)字符串中某個(gè)字母出現(xiàn)的次數(shù)*/
public class Maptest {public static void main(String[] args) {String str = "aababcadasda";method_1(str);}public static void method_1(String str){char[] strArr = str.toCharArray();TreeMap<Character,Integer> strArr_treeMap = new TreeMap<>();for(char c : strArr){Integer i = strArr_treeMap.get(c);if (i==null){strArr_treeMap.put(c,1);}else{strArr_treeMap.put(c,i+1);}}System.out.println(strArr_treeMap);}}
package map.demo;import javafx.beans.binding.MapExpression;import java.util.*;/*** @author Alina* @date 2021年09月27日 10:48 下午* Map嵌套* 數(shù)據(jù)形式* 川石* 功能測(cè)試班* 學(xué)號(hào):001 姓名:張三* 學(xué)號(hào):002 姓名:李四* 性能測(cè)試班* 學(xué)號(hào):003 姓名:王二* 學(xué)號(hào):004 姓名:麻子*/
public class MapTest2 {public static void main(String[] args) {HashMap<String,HashMap<String,String>> chuanshi = new HashMap<>();HashMap<String,String> classOne = new HashMap<>();HashMap<String,String> classTwo = new HashMap<>();chuanshi.put("功能測(cè)試班",classOne);chuanshi.put("性能測(cè)試班",classTwo);classOne.put("001","張三");classOne.put("002","李四");classTwo.put("003","王二");classTwo.put("004","麻子");Map_Entry(chuanshi);}public static void keySet(HashMap<String,HashMap<String,String>> className){Set<String> classname = className.keySet();Iterator< String> it = classname.iterator();while (it.hasNext()){String class_id = it.next();HashMap<String,String> class_student_number = className.get(class_id);Set<String> class_number = class_student_number.keySet();Iterator<String> it_1 = class_number.iterator();while (it_1.hasNext()){String student_id = it_1.next();String student_name = class_student_number.get(student_id);System.out.println("Class:"+class_id+" "+"id:"+ student_id+" "+"name:"+student_name);}}}public static void Map_Entry(HashMap<String,HashMap<String,String>> className){//使用具和方法,將鍵值對(duì)關(guān)系存儲(chǔ)到Set集合Set<Map.Entry<String,HashMap<String,String>>> mp1 = className.entrySet();Iterator<Map.Entry<String,HashMap<String,String>>> it = mp1.iterator();while(it.hasNext()){Map.Entry<String,HashMap<String,String>> mp2 = it.next();//獲取班級(jí)對(duì)象HashMap<String,String> mp_class_name = mp2.getValue();//獲取班級(jí)idString mp_class_id = mp2.getKey();Set<Map.Entry<String,String>> student_number = mp_class_name.entrySet();Iterator<Map.Entry<String,String>> it2 = student_number.iterator();while (it2.hasNext()){Map.Entry<String,String>mp3 = it2.next();//獲取學(xué)生姓名String mp_student_name = mp3.getValue();//獲取學(xué)生idString mp_student_id = mp3.getKey();System.out.println("class name:"+ mp_class_id+" student id:"+mp_student_id+" student name:"+mp_student_name);}}}
}
package map.demo;import java.util.*;/*** @author Alina* @date 2021年09月26日 10:54 下午*存入Student類(lèi)對(duì)象和地址,1.實(shí)現(xiàn)名字的自然順序排序* 2.實(shí)現(xiàn)按年齡的從小到大排序*/
class MyComparator implements Comparator<Student> {public int compare(Student s1,Student s2){int age = s1.getAge()- s2.getAge();return age==0?s1.getName().compareTo(s2.getName()):age;}
}
public class treeMapDemo {public static void main(String[] args) {method_1();}public static void method_1() {TreeMap<Student, String> treemap = new TreeMap<>(new MyComparator());treemap.put(new Student("zhangsag", 23), "深圳");treemap.put(new Student("lisi", 26), "北京");treemap.put(new Student("wanger", 50), "廣州");treemap.put(new Student("mazi", 100), "上海");Set<Map.Entry<Student, String>> s = treemap.entrySet();Iterator<Map.Entry<Student, String>> it = s.iterator();while (it.hasNext()) {Map.Entry<Student, String> mp = it.next();Student key = mp.getKey();String value = mp.getValue();System.out.println(key + " " + value);}}
}
總結(jié)
以上是生活随笔為你收集整理的(JAVA)Map集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: html JSP 富文本案例,JSP页面
- 下一篇: 宝藏软件:“小狼毫” 一款开源牛叉输入法