常州微信网站建设方案,目前最流行网站开发软件,广告投放数据分析,洛阳做网站排名package map.demo;import java.util.*;/*** author Alina* date 2021年09月25日 11:20 下午* 底层原理是哈希表#xff0c;保证唯一性* 允许存储null键#xff0c;null值* 线程不安全#xff0c;运行速度快* keyset()可以获取到键* Collections 类的方法reverseOrder :调用空…package map.demo;import java.util.*;/*** author Alina* date 2021年09月25日 11:20 下午* 底层原理是哈希表保证唯一性* 允许存储null键null值* 线程不安全运行速度快* keyset()可以获取到键* Collections 类的方法reverseOrder :调用空参数返回比较器逆转对象的自有顺序**/
public class HashMapDemo {public static void main(String[] args) {Scanner sc new Scanner(System.in);HashMapStudent,String ha new HashMap();System.out.println(请输入学生的姓名年龄籍贯);System.out.println(例如张三 37 黑龙江 ,以空格作为分隔符以over结束);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(HashMapStudent,String mp){SetStudent s mp.keySet();IteratorStudent 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集合可以存储两个集合一个键值对* 1.不继承Collection* 2.键保持唯一性值可以重复* 3.Map接口中的方法* 4.引用类型获取不到值返回NILL基本类型获取不到值返回负数* 5.map集合的获取第一种Map集合依赖于Set集合可以使用Map集合的Set方法并迭代****/
public class Mapdemo {public static void main(String[] args) {method_4();}/***** author Alina* date 2021/9/25 10:07 下午* Map集合中有一个类描述映射关系* 键值对关系对象的描述接口是Map接口的内部接口* interface Map{* interface Entry{* K getKey();* V getValue();* }* }* 案例使用的是HashMap实现* class HashMap implements Map{* class Entry implements Map.Entry{* public k getKey();* public V getValue();* }* }* 实现步骤* 1.Map集合的方法entrySet()方法获取键值对关系对象--Map.Entry接口的实现类* 将Map.Entry接口实现类对象存储到Set集合* 2.迭代Set集合* 3.获取Set结合存储的是Map.Entry对象 --Map集合中的键值对的映射关系* 4.使用返回的ap.Entry对象getKey getValue 获取键值对*/public static void method_4(){MapString,Integer m new HashMap();m.put(a,21);m.put(b,43);m.put(c,90);SetMap.EntryString,Integer st m.entrySet();Iterator Map.EntryString,Integer it st.iterator();while (it.hasNext()){Map.EntryString,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集合依赖于Set集合可以使用迭代器* keySet()方法可以获取键*/public static void method_3(){MapString,Integer m new HashMap();m.put(a,21);m.put(b,43);m.put(c,90);SetString 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(){MapString,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);//判断当前键中是否有某键boolean a1 ht.containsKey(a);//判断值中是否包含某值boolean b1 ht.containsValue(3);System.out.println(a1 b1);}/**** author Alina* date 2021/9/25 9:24 下午* 使用Map集合依赖于实现类HashMap实现类*/public static void method(){//Map接口对实现类HashMapMapString,Integer h new HashMap();//键值对存储到集合的方法--putInteger a h.put(a,1);System.out.println(a);System.out.println(h);//获取键对应的值}/**** author Alina* date 2021/9/25 9:26 下午* 往Map类存储*/public static void method_1(){MapString,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 下午* 获取一个字符串中某个字母出现的次数*/
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();TreeMapCharacter,Integer strArr_treeMap new TreeMap();for(char c : strArr){Integer i strArr_treeMap.get(c);if (inull){strArr_treeMap.put(c,1);}else{strArr_treeMap.put(c,i1);}}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嵌套* 数据形式* 川石* 功能测试班* 学号001 姓名张三* 学号002 姓名李四* 性能测试班* 学号003 姓名王二* 学号004 姓名麻子*/
public class MapTest2 {public static void main(String[] args) {HashMapString,HashMapString,String chuanshi new HashMap();HashMapString,String classOne new HashMap();HashMapString,String classTwo new HashMap();chuanshi.put(功能测试班,classOne);chuanshi.put(性能测试班,classTwo);classOne.put(001,张三);classOne.put(002,李四);classTwo.put(003,王二);classTwo.put(004,麻子);Map_Entry(chuanshi);}public static void keySet(HashMapString,HashMapString,String className){SetString classname className.keySet();Iterator String it classname.iterator();while (it.hasNext()){String class_id it.next();HashMapString,String class_student_number className.get(class_id);SetString class_number class_student_number.keySet();IteratorString 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(HashMapString,HashMapString,String className){//使用具和方法将键值对关系存储到Set集合SetMap.EntryString,HashMapString,String mp1 className.entrySet();IteratorMap.EntryString,HashMapString,String it mp1.iterator();while(it.hasNext()){Map.EntryString,HashMapString,String mp2 it.next();//获取班级对象HashMapString,String mp_class_name mp2.getValue();//获取班级idString mp_class_id mp2.getKey();SetMap.EntryString,String student_number mp_class_name.entrySet();IteratorMap.EntryString,String it2 student_number.iterator();while (it2.hasNext()){Map.EntryString,Stringmp3 it2.next();//获取学生姓名String mp_student_name mp3.getValue();//获取学生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类对象和地址1.实现名字的自然顺序排序* 2.实现按年龄的从小到大排序*/
class MyComparator implements ComparatorStudent {public int compare(Student s1,Student s2){int age s1.getAge()- s2.getAge();return age0?s1.getName().compareTo(s2.getName()):age;}
}
public class treeMapDemo {public static void main(String[] args) {method_1();}public static void method_1() {TreeMapStudent, 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), 上海);SetMap.EntryStudent, String s treemap.entrySet();IteratorMap.EntryStudent, String it s.iterator();while (it.hasNext()) {Map.EntryStudent, String mp it.next();Student key mp.getKey();String value mp.getValue();System.out.println(key value);}}
}