(JAVA)Map集合

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);HashMap<Student,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(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集合,可以存储两个集合,一个键值对* 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(){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集合依赖于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");//判断当前键中是否有某键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接口对实现类HashMapMap<String,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(){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 下午* 获取一个字符串中某个字母出现的次数*/
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嵌套* 数据形式*      川石*          功能测试班*              学号:001  姓名:张三*              学号:002  姓名:李四*          性能测试班*              学号:003  姓名:王二*              学号: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("功能测试班",classOne);chuanshi.put("性能测试班",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){//使用具和方法,将键值对关系存储到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();//获取班级对象HashMap<String,String> mp_class_name = mp2.getValue();//获取班级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();//获取学生姓名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 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);}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421754.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

各种推荐资料汇总。。。

http://blog.csdn.net/lzt1983/article/details/7914536 转载于:https://www.cnblogs.com/tandychao/archive/2013/05/30/3108022.html

(JAVA)File类

package filedemo;import java.io.File; import java.io.IOException;/*** author Alina* date 2021年10月07日 10:33 下午* File类 提供方法操作 文件 目录 文件夹* 1.两个静态成员变量 static String pathSeparator 路径分隔符* 2.static String separator 名称分隔符* 3.创…

(JAVA)File类2

package filedemo;import java.io.File; import java.io.FileFilter; import java.text.DateFormat; import java.util.Date;/*** author Alina* date 2021年10月10日 11:15 下午* 1.获取文件最后修改时间 Long lastModified()* 2.以数组获取路径下所有文件String[] list();Fi…

MVVM之旅

MVVM的概念已经在脑子里渗透了一段时间&#xff0c;也试着使用了一段时间&#xff0c;就我个人理解&#xff0c;MVVM所倡导的应该是解耦UI跟数据打交道的那一部分&#xff0c;而纯UI的还是写在CodeBehind里。MVVM是以绑定&#xff08;绑定数据、绑定命令&#xff09;来驱动的&a…

变量原理深入讲解

javascript是一种解释执行的语言 语言分解释执行和编译执行 人用直观的编程语言来写程序-------------计算机语言010011100 举例理解&#xff1a; 英文 中国人张三(不会英文) 1、把英文报刊翻译成中文报刊&#xff0c;然后再看(翻译完后&#xff0c;多了一份中…

(JAVA)IO1

IO流四个抽象及类 1.字节输出流&#xff0c;写入任意文件OutputStreamwrite 字节数组 字节数组的一部分 单个字节close 释放资源flush 刷新资源&#xff0c;强制刷新资源 2.字节输入流&#xff0c;读取任意文件InputStreamread 字节数组 字节数组的一部分 单个字节close 关闭资…

putil:一个用于获得处理器和系统相关信息的模块

psutil能干的事是&#xff1a;提供了个接口&#xff0c;可以用来获取信息&#xff0c;包括&#xff1a; 当前运行的进程系统&#xff08;资源使用&#xff09;信息CPU内存磁盘网络用户psutil实现了很多功能&#xff0c;包括了如下工具所具有的&#xff1a; pstopdfkillfreelsof…

(JAVA)IO缓冲区

package IODemo;import java.io.*; import java.nio.charset.StandardCharsets;/*** author Alina* date 2021年10月18日 9:57 下午* 一、 1.IO流关于缓冲区&#xff0c;* 2.输出流缓冲区&#xff1a;BufferedOutputStream(OutputStream out)* 3.输入流缓冲区&#…

php正则表达式如何找到匹配模式中的最后一组

转载于:https://www.cnblogs.com/MyFlora/archive/2013/06/07/3124073.html

(JAVA)IO流之读写单个字节和复制文本文件

package IODemo;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;/*** author Alina* date 2021年10月15日 8:19 下午* read()方法特点* 1.每次只读取一个文件* 2.只运行一次&#xff0c…

sql where in 排序问题

直接上代码了 对于int类型的需要转化一下 select * from cvProducts where ID in(972,687,678,962) order by charindex(cast(ID as varchar),972,687,678,962) 对于varchar的直接使用 select * from cvProducts where MouldNo in(C62859,C63417,C32283) order by charindex(…

(JAVA)FileWriter

package IODemo;import java.io.BufferedWriter; import java.io.FileWriter;/*** author Alina* date 2021年10月31日 10:48 下午* FileWrite 写入文本文件的便捷类&#xff0c;方便快捷* 默认查询编码表&#xff0c;不能指定编码表* BufferedWriter 字符输出流的缓冲对象**/ …

JavaWeb中验证码的实现

在Web程序中&#xff0c;验证码是经常使用的技术之一。Web程序永远面临未知用户和未知程序的探测。为了防止恶意脚本的执行&#xff0c;验证码技术无疑是首选方案之一。本文将讨论如何在JSP和Servlet中使用验证码技术。 验证码的产生思路很简单&#xff0c;在Servlet中随机产生…

IO流复制图片

package IODemo;/*** author Alina* date 2021年11月14日 4:32 下午* 复制文件到指定目录**/ import java.io.*; public class IOcopyfile {public static void main(String[] args) {CopyDir(new File(“源文件”),new File(“目标文件”));}public static void CopyDir(File …