JAVA自学笔记18

1、Map接口:

1)功能:



2)

  1. Map<String,String>m=new HashMap<String,String>();
  2. //添加元素,元素无序
  3. System.out.println("map.put("cc","coco"));//null
  4. //替换键值,返回该键的上一个值,若键不存在,返回null
  5. System.out.println(map);//cc=coco,左边是键,右边是值
  6. //map.clear();//清除所有元素
  7. //System.out.printn(map.remove("cc"));//coco。若键存在,则删除该键,返回该键的键值。若不存在,返回null
  8. System.out.println(map.containsKey("cc"));true.//集合存在该键,则返回true。若不在,则返回false
  9. System.out.println(map.containsKey(map.isEmpty()));//集合为空,则返回false
  10. System.out.println(map.containsKey(map.size);//返回集合中键的数目
  11. //获取功能
  12. System.out.println((map.get(cc));//coco。将获取该键的键值,若键不存在,将返回null
  13. Set<String> set=map.keyset();
  14. for(String key:set){
  15. System.out.println(key);
  16. }//将返回所有的键
  17. Collection<String>con=map.values();
  18. for(String value:value){
  19. System.out.println(value);
  20. }//将返回所有的键值
  21. //Map集合的遍历-方式1依键寻值
  22. //获取所有的键-遍历键的集合,获取得到每一个键-根据键去找值
  23. Map<String,String>m=new HashMap<String,String>();
  24. map.put("cc","coco");
  25. map.put("jc","jack");
  26. Set<String> set=map.keyset();
  27. for(String key:set){
  28. Strng value=map.get(key);
  29. System.out.println(value);
  30. }
  31. //Map集合的遍历-方式2
  32. //获取所有的键值段的集合
  33. //遍历键值对对象的集合,得到每一个键值对对象
  34. //根据键值对对象获取键和键值
  35. //键值对对象的表示:Set<Map.Entry<K,V>>entrySet()l;返回键值对对象的集合
  36. Set<Map.Entry<String,String>>set=map.entrySet();
  37. for(Map.Entry<String,String>me:set){
  38. String key=me.getKey();
  39. String value=me.getValue();
  40. }

3)两种遍历方式的图解

2、HashMap

1)键是哈希表结构,可以保证键的唯一性,是Map的接口实现

2)HashMap

  1. //创建集合对象
  2. HashMap<String,String>hm=new HashMap<String,String>();
  3. hm.put("cc","coco");
  4. hm.put("jc","jack");
  5. Set<String>set=hm.keySet();
  6. for(String key:Set){
  7. String value=hm.get(key);
  8. System.out.println(value);
  9. }

3)HashMap

  1. HashMap<Integer,String>=new HashMap<Integer,String>();
  2. Integer i=new Integer(55);
  3. String s="jack";
  4. hm.put(i,s);
  5. hm.put(14,"coco");
  6. Set<Integer>set=hm,keySet();
  7. for(Integer key:set){
  8. String value=hm.get(key);
  9. }

4)HashMap

  1. //Student类略
  2. HashMap<String,Student>hm=new HashMap<String,Student>();
  3. Student s1=new Student("coco",22);
  4. Student s1=new Student("kiki",23);
  5. //添加元素
  6. hm.put("5414",s1);
  7. hm.put("5415",s2);
  8. //遍历
  9. Set<String>set=hm.keySet();
  10. for(String key:set){
  11. Student value=hm.get(key);
  12. }

5)HashMap

  1. //创建集合对象
  2. HashMap<Student,String>hm=new HashMap<Student,String>();
  3. //创建学生对象
  4. Student s1=new Student("coco",22);
  5. Student s1=new Student("kiki",23);
  6. //添加元素
  7. hm.put(s1,"3f4d");
  8. hm.put(s2,"4f4h");
  9. //遍历
  10. Set<Student>set=hm.keySet();
  11. for(Student key:set){
  12. String value=hm.get(key);
  13. //若出现某几个对象成员变量相同但出现了多次该键时,在Student类中生成HashCode()和equal()即可
  14. }

3、LinkedHashMap

1)Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。即键是有序的

2)

  1. LinkedHashMap<String,String>hm=new LinkedHashMap<String,String>();
  2. hm.put("1223","dd");
  3. hm.put("1223","wd");
  4. hm.put("1233","qd");
  5. forString key:set){
  6. String value=hm.get(key);//输出语句中重复的将覆盖
  7. }

4、TreeMap类

1)键为红黑树结构,可以保证键的排序和唯一性

2)TreeMap集合键是String值是String的案例

  1. TreeMap<String,String>tm=new TreeMap<String,String>;
  2. tm.put("coco","cc");
  3. tm.put("jack","jc");
  4. set<String>set=tm.eySet();
  5. for(String keyset){
  6. String value=tm.get(key);
  7. //使用的是自然排序
  8. }

3)TreeMap集合键是Student值是String的案例

  1. TreeMap<String,String>tm=new TreeMap<String,String>(new Comparator<Student>(){
  2. public int compare(Student s1,Student s2){
  3. int num=s1.getAge()-s2.getAge();
  4. //以下是次要条件
  5. int num2=num==0?s1.getName().compareTo(s2.getName()):num;
  6. return num2;
  7. }
  8. });//比较器排序,按年龄从低到高排序
  9. Student s1=new Student ("kiki",23);
  10. Student s2=new Student ("jiji",24);
  11. tm.put(s1,"5821");
  12. tm.put(s1,"5851");
  13. set<String>set=tm.keySet();
  14. for(String keyset){
  15. String value=tm.get(key);
  16. //要重写compareTo方法实现Comparable接口才能实现自然排序

@例题1:统计字符串中每个字符出现的次数

图解

  1. Scannner sc=new Scanner(System.in);
  2. //键盘录入一个字符串
  3. String line=sc.nextLine();
  4. //定义TreeMap集合
  5. TreeMap<Character,Integer>tm=new TreeMap<Character,Integer>();
  6. //把字符串转换为字符数组
  7. char[] chs=line,toCharArray();
  8. //遍历数组
  9. for(char ch:chs){
  10. Integer i=tm.get(ch,1);
  11. }else{
  12. i++;
  13. tm.put(ch,i);
  14. }}
  15. StringBuilder sb=new StringBuilder();
  16. Set<Character>set=tm.keySet();
  17. for(Character key:set){
  18. Integer value=tm.get(key);
  19. sb.append(key).append)("(").append(value).append(")");
  20. String result=sb.toString;
  21. System.out,println(result);
  22. }

@例题3:HashMap嵌套HashMap

  1. //创建集合对象
  2. HashMap<String,HashMap><String,Integer>>czbkMap=new HashMap<String,HashMap><String,Integer>>();
  3. //创建基础班集合对象
  4. HashMap<String,Integer>jcMap=new HashMap<String,Integer>();
  5. jcMap.put("cc",12);
  6. jcMap.put("bb",14);
  7. //添加到大集合中
  8. czbkMap.put("jc",jcMap);
  9. //创建就业班集合对象
  10. HashMap<String,Integer>jyMap=new HashMap<String,Integer>();
  11. jcMap.put("aa",13);
  12. jcMap.put("dd",15);
  13. //添加到大集合中
  14. czbkMap.put("jy",jyMap);
  15. //遍历集合
  16. Set<String>czbkMapSet=czbkMap.keySet();
  17. for(String czbkMapKey:czbkMapSet){
  18. HashMap<String,Integer>czbkMapValue=czbkMap.get(czbkMapSet);
  19. Set<String > czbkMapValueSet=czbkMapValueSet.keySet();
  20. for(String czbkMapValueKey:czbkMapValueSet){
  21. Integer czbkMapValueValue=czbkMapValue.get(czbkMapValueKey);
  22. System.out,println(czbkMapValueKey+","czbkValueValue);
  23. }
  24. }

@例题4:HashMap集合嵌套ArrayList

  1. //创建集合对象
  2. HashMap<String,ArrayList<String>>hm=new HashMap<String,ArrayList<String>>();
  3. //创建元素集合1
  4. ArrayList<String>array1=new ArrayList<String>();
  5. array1.add("黄盖");
  6. array1.add("孙权");
  7. bjczbkMap.put("jc",array1)
  8. bjczbkMap.put("jc",array2)
  9. hm.put("三国","array1");//添加键
  10. //创建元素集合2
  11. ArrayList<String>array2=new ArrayList<String>();
  12. array3.add("孙悟空");
  13. array3.add("观音");
  14. hm.put("西游记","array2");//添加键
  15. //创建元素集合1
  16. ArrayList<String>array3=new ArrayList<String>();
  17. array4.add("贾宝玉");
  18. array4.add("薛宝钗");
  19. bjczbkMap.put("jc",array3)
  20. bjczbkMap.put("jc",array4)
  21. hm.put("红楼","array3");//添加键
  22. //遍历集合
  23. Set<String>set=hm.keySet();
  24. for(String key:set){
  25. System.out.println(key);
  26. ArrayList<String> array=hmget(key);
  27. for(String s:value){
  28. System.out.println("\t"+s);
  29. }
  30. }

@例题6:ArrayListq嵌套HashMap

  1. //创建集合对象
  2. ArrayList<HashMap><String.String>>array=new ArrayList<HashMap<String,String>>();
  3. //创建元素1
  4. HashMap<String,String>hm1=new HashMap<String,String>();
  5. hm1.put("周瑜","小乔");
  6. hm1.put("吕布","貂蝉");
  7. //把元素添加到array里
  8. array.add(hm1);
  9. //创建元素2
  10. HashMap<String,String>hm1=new HashMap<String,String>();
  11. hm1.put("牛魔王","铁扇");
  12. hm1.put("金角","银角");
  13. //把元素添加到array里
  14. array.add(hm2);
  15. //创建元素1
  16. HashMap<String,String>hm1=new HashMap<String,String>();
  17. hm1.put("牛肉","牛肉面");
  18. hm1.put("陈靖仇","于小雪");
  19. //把元素添加到array里
  20. array.add(hm1);
  21. //遍历
  22. for(HashMap<String,String>hm:array){
  23. Set<String>set=hm.keySet();
  24. for(String key:set){
  25. String value=hm.get(key);
  26. System.out.println(key+" "+value)
  27. }
  28. }

@例题7:HashMap嵌套HashMap嵌套HashMap

  1. //创建大集合
  2. HashMap<String,HashMap<String,ArrayList<Student>>>czbkMap=new HashMap<String,HashMap<String,ArrayList<Student>>>();
  3. //北京校区
  4. HashMap<String,ArrayList<Student>> bjczbkMap=new HashMap<String,ArrayList<Student>>();
  5. ArrayList<Student>array1=new ArrayList<Student>();
  6. Student s1=new Student("林青霞","27");
  7. Student s2=new Student("风清扬","30");
  8. array1.add(s1);
  9. array1.add(s2);
  10. ArrayList<Student>array2=new ArrayList<Student>();
  11. Student s3=new Student("董存瑞","17");
  12. Student s4=new Student("李云龙","60");
  13. array2.add(s3);
  14. array2.add(s4);
  15. //其余类比不再给出
  16. //遍历集合
  17. Set<String>czbkMapSet=czbkMap.keySet();
  18. for(String czbkMapKey:czbkMapSet){
  19. for(String czbkMapKey:czbkMapSet){
  20. System,out.println(czbkMapKey);
  21. HashMap<String,ArrayList<Student>>czbkMapValue=cabkMap.get(czbkMapKey);
  22. set<String>czbkMapValueSet=czbkMapValue.keySet();
  23. for(String czbkMapValueKey:czbkMapValueSet){
  24. ArrayList<Student>czbkMapValueValue=czbkMapValue.get(czbkMapValueKey);
  25. for(Student s:czbkMapValueValue){
  26. System.out.println(s.getName);
  27. }
  28. }
  29. }
  30. }
  31. @例题8HashMapHashtable的区别
  32. Hashtable:此类实现一个哈希表,与HashMap用法几乎一样,但它不允许null键和null 。它是同步的,HashMap是不同步的。
  33. @例题9Ltst,Set,Map接口是否都继承自Map接口?
  34. List,Set不继承自Map接口,它们继承自Collection接口,Map本身就是一个顶层接口
  35. 2Collections类:
  36. 1)针对集合操作的工具类
  37. 2 )CollectionCollections的区别:前者是单列集合的顶层接口。后者是对集合操作的工具类
  38. 3)成员方法
  39. public static <T> void sort(List<T>List)
  40. 排序。默认进行自然排序
  41. public static <T> int binarySearch(List<?>list,T key)
  42. 进行二分查找
  43. public static <T> max(collection<?> coll)
  44. 求最大值
  45. public static void reverse(List<?>list)
  46. 反转
  47. public static void shuffle (List<?>list)
  48. 随机置换
  1. List<Integer>list=new ArrayList<Integer>()'
  2. list.add(20);
  3. list.add(30);
  4. list.add(10);
  5. list.add(60);
  6. list.add(5);
  7. list.add(200);
  8. Collections.sort(list);
  9. int index=Collections(list,10);
  10. System.out.println(Collections.max(list));
  11. Collections.reverse(list);
  12. Collections.shuffle(list);

@例题10:ArrayList存储自定义对象并排序

  1. //学生类略
  2. Public class Student implements Comparable<Student>{
  3. public int compareTo(Student s){
  4. int num=this.age-s.age;
  5. int num2=num==0?this.name.comparaTo(s.name):num;
  6. return num2;
  7. }
  8. List<Student>list=new ArrayList<Student>();
  9. Student s1=new Student("huh",34);
  10. Student s2=new Student("hh",44);
  11. list.add(s1);
  12. list.add(s2);
  13. //自然排序
  14. //collections.sort(list);
  15. //比较器排序,以比较器为主
  16. Collections.sort(list,new Comparator)<Student>(){
  17. public int compare(Student s1,Student s2){
  18. int num=s2.getAge()-s1.getAge();
  19. return num;
  20. }
  21. })
  22. for(Student s:List){
  23. System.out.println(s.getName())
  24. }

@例题11:模拟斗地主洗牌和发牌

  1. ArrayList<String> array=new ArrayList<String>();
  2. String[] colors={"方块","梅花","红桃","黑桃"}
  3. String[] numbers={"A","2","3","4","5","6","7","8","9","10","J","Q""K"};
  4. for(String color:colors){
  5. for(String number:numbers){
  6. array.add(color.concat(number));
  7. }
  8. }
  9. array.add(大王);
  10. array.add(小王);
  11. //洗牌
  12. Collections.shuffle(array);
  13. //发牌
  14. ArrayList<String>p1=new ArrayList<String>();
  15. ArrayList<String>p2=new ArrayList<String>();
  16. ArrayList<String>p3=new ArrayList<String>();
  17. ArrayList dipai=new ArrayList<String>();
  18. for(int x=0;x<array.size();x++){
  19. if(x>array.size()-3){
  20. diPai.add(array.get(x));
  21. else if(x%3==0){p1.add(array.get(x))
  22. }
  23. else if(x%3==1){p2.add(array.get(x))
  24. }
  25. else if(x%3==2){p3.add(array.get(x))
  26. }
  27. }
  28. }
  29. //看牌
  30. lookPoker("p1",p1);
  31. lookPoker("p2",p2);
  32. lookPoker("p3",p3);
  33. public static void lookPoker(String name,ArrayList<String>array){
  34. System.out.println(name+"的牌是");
  35. for(String s:array){
  36. System.out.println(s+" ")
  37. }
  38. System.out.println(" ")
  39. }

图解:

  1. //完整代码
  2. //创建一个HashMap集合
  3. //创建一个ArrayList集合
  4. //创建花色和点数数组
  5. //从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号即可
  6. //洗牌(洗的是编号)
  7. //发牌(发的是编号,为了使得在看牌时编号是排序的,就创建TreeSet集合接收)
  8. //看牌(遍历TreeSet集合,获取编号,到HashMap里找对应的牌)
  9. //创建一个HashMap集合
  10. // 创建一个HashMap集合
  11. HashMap<Integer, String> hm = new HashMap<Integer, String>();
  12. // 创建一个ArrayList集合
  13. ArrayList<Integer> array = new ArrayList<Integer>();
  14. // 创建花色数组和点数数组
  15. // 定义一个花色数组
  16. String[] colors = { "♠", "♥", "♣", "♦" };
  17. // 定义一个点数数组
  18. String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
  19. "K", "A", "2", };
  20. // 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
  21. int index = 0;
  22. for (String number : numbers) {
  23. for (String color : colors) {
  24. String poker = color.concat(number);
  25. hm.put(index, poker);
  26. array.add(index);
  27. index++;
  28. }
  29. }
  30. hm.put(index, "小王");
  31. array.add(index);
  32. index++;
  33. hm.put(index, "大王");
  34. array.add(index);
  35. // 洗牌(洗的是编号)
  36. Collections.shuffle(array);
  37. // 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
  38. TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
  39. TreeSet<Integer> linQingXia = new TreeSet<Integer>();
  40. TreeSet<Integer> liuYi = new TreeSet<Integer>();
  41. TreeSet<Integer> diPai = new TreeSet<Integer>();
  42. for (int x = 0; x < array.size(); x++) {
  43. if (x >= array.size() - 3) {
  44. diPai.add(array.get(x));
  45. } else if (x % 3 == 0) {
  46. fengQingYang.add(array.get(x));
  47. } else if (x % 3 == 1) {
  48. linQingXia.add(array.get(x));
  49. } else if (x % 3 == 2) {
  50. liuYi.add(array.get(x));
  51. }
  52. }
  53. // 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
  54. lookPoker("风清扬", fengQingYang, hm);
  55. lookPoker("林青霞", linQingXia, hm);
  56. lookPoker("刘意", liuYi, hm);
  57. lookPoker("底牌", diPai, hm);
  58. }
  59. // 写看牌的功能
  60. public static void lookPoker(String name, TreeSet<Integer> ts,
  61. HashMap<Integer, String> hm) {
  62. System.out.print(name + "的牌是:");
  63. for (Integer key : ts) {
  64. String value = hm.get(key);
  65. System.out.print(value + " ");
  66. }
  67. System.out.println();
  68. }

JAVA自学笔记18的更多相关文章

  1. JAVA自学笔记24

    JAVA自学笔记24 1.能使用同步代码块就使用同步代码块,除非锁对象是this,就可以考虑使用同步方法.静态方法的锁是类的字节码对象. 2.JDK5新特性 1)接口Lock void Lock()/ ...

  2. JAVA自学笔记27

    JAVA自学笔记27 1.类的加载 1)当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. ①加载:就是指将class文件读入内存,并为之创 ...

  3. JAVA自学笔记09

    JAVA自学笔记09 1.子类的方法会把父类的同名方法覆盖(重写) 2.final: 1)可修饰类.方法.变量 2)修饰类时:此时该类变为最终类,它将无法成为父类而被继承 3)修饰方法时:该方法将无法 ...

  4. JAVA自学笔记05

    JAVA自学笔记05 1.方法 1)方法就是完成特定功能的代码块,类似C语言中的函数. 2)格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,-){ 函数体; return ...

  5. JAVA自学笔记06

    JAVA自学笔记06 1.二维数组 1)格式: ①数据类型[][]数组名 = new 数据类型[m][n]; 或 数据类型[]数组名[]=new 数据类型[m][n]; m表示这个二维数组有多少个一维 ...

  6. JAVA自学笔记04

    JAVA自学笔记04 1.switch语句 1)格式:switch(表达式){ case 值1: 语句体1; break; case 值2: 语句体2; break; - default: 语句体n+ ...

  7. JAVA自学笔记07

    JAVA自学笔记07 1.构造方法 1) 例如:Student s = new Student();//构造方法 System.out.println(s);// Student@e5bbd6 2)功 ...

  8. JAVA自学笔记10

    JAVA自学笔记10 1.形式参数与返回值 1)类名作为形式参数(基本类型.引用类型) 作形参必须是类的对象 2)抽象类名作形参 需要该抽象类的子类对象,通过多态实现 3)接口名为形参 需要的是该接口 ...

  9. JAVA自学笔记13

    JAVA自学笔记13 1.StringBuffer类 1)线程安全的可变字符序列 线程安全(即同步) 2)StringBuffer与String的区别:一个可变一个不可变 3)构造方法: ①publi ...

随机推荐

  1. 牛客网练习赛t2(线段树)

    题解: 好像因为他说了 数据范围全部在ll以内 所以直接暴力就可以过了 比较正常是用线段树来维护 洛谷上有道模板题是支持加,乘,区间和 而这题还多了区间平方和的操作 按照那题的操作 我们维护的时候保证 ...

  2. selenium相关:通过location 和 size 获取元素所在像素位置和尺寸,截取图片ROI

    1.实验 #https://captcha.luosimao.com/demo/ chrome default: location 不滚动,直接返回相对整个html的坐标 {'x': 15.0, 'y ...

  3. 在grails中远程调用action

    在进行类似批处理的程序时,如果在一个action中需要保存很多记录数,这会导致grails中的数据库session超过负荷,从而导致OOM. 因为这个情况的发生是由于在一次请求中,对数据进行的修改都保 ...

  4. 部署Tomcat及nginx负载均衡

    Web应用服务器的选择   (1)IBM的WebSphere及Oracle的WebLogic 性能高,但价格也高   (2)Tomcat 性价比高 Tomcat服务器是一个免费的开放源代码的Web应用 ...

  5. Nginx的配置与部署研究,Upstream负载均衡模块

    Nginx 的 HttpUpstreamModule 提供对后端(backend)服务器的简单负载均衡.一个最简单的 upstream 写法如下: upstream backend { server ...

  6. 2018牛客网暑假ACM多校训练赛(第五场)H subseq 树状数组

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round5-H.html 题目传送门 - https://www.no ...

  7. AtCoder Regular Contest 100 (ARC100) E - Or Plus Max 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/9251448.html 题目传送门 - ARC100E 题意 给定一个正整数 $n(n\leq 18)$. 然后 ...

  8. springboot学习——第二集:整合Mybaits

    1,Mybatis动态插入(insert)数据(使用trim标签):https://blog.csdn.net/h12kjgj/article/details/55003713 2,mybatis 中 ...

  9. 使用entitiy

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  10. Java实现简单记事本

    代码实现: import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; im ...