当前位置:   article > 正文

集合框架之map_java map 定义 character,integer

java map 定义 character,integer

 

文章目录

  • Map
  • 常用实现类的HashMap
  • 泛型的作用
  • 集合框架工具类



提示:以下是本篇文章正文内容,下面案例可供参考

一、Map

1.map的特点

①增加

 ②删除 map.remove() 

 ③.修改 

 map集合的底层是set集合,没有下标,map集合的键是唯一,

④查找

 key是可以为空

 2.遍历方式

①keySet  (先获得map集合所有的key)

 ②entrySet

 map集合的映射关系,set里放的是关系

二、常用实现类的HashMap

代码如下(示例):

  1. public class One2 {
  2.     public static void main(String[] args) {
  3.         String a="aksdjhfeirusiduyfydsskdoeuyftdtsvcssdssasrpouxsawklm";
  4.         char[] chars = a.toCharArray();
  5.         Map<Character, Integer> map=new HashMap<Character, Integer>();
  6.         for (char c : chars) {
  7.         Integer    count=map.get(c);
  8.         if(count==null||count==0) {
  9.             map.put(c, 1);
  10.         }else {
  11.             map.put(c,count+1);
  12.         }
  13.         }
  14.         Set<Entry<Character, Integer>> entrySet = map.entrySet();
  15.         for (Entry<Character, Integer> entry : entrySet) {
  16.             System.out.println(entry.getKey()+"出现了"+entry.getValue()+"次");
  17.         }
  18.     
  19.     }
  20. }
运行结果:

 

思路:1.字符串转换成字符数组
                  2.遍历数组,将字符放入map集合中key
                  3.判断如果字符没有,那么就给字符对应的值设置为1,如果有就给对应的值+1

三、泛型的作用

错误:(Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer)String不能转换成Integer

如果我把Object改为Integer (在编译期就会报错),所以说,泛型最大的好处在于将运行时的异常转换成编译期的错误

正确代码如下(示例):

  1. public class One3 {
  2.     public static void main(String[] args) {
  3.         Map<String,Integer> map=new HashMap<>();
  4.         map.put("就是就是", 1);
  5.         map.put("薛之谦", 2);
  6.         map.put("带小妹", 3);
  7.         map.put("赵丽颖", 4);
  8.         map.put("杨洋", 5);
  9.         //刷选出value为偶数对应的key
  10.         Set<Entry<String,Integer>> entrySet = map.entrySet();
  11.         for (Entry<String, Integer> entry : entrySet) {
  12.             Object value = entry.getValue();
  13.             Integer val=(Integer)value;
  14.             if(val%2==0) {
  15.                 System.out.println("偶数对应的key为: "+entry.getKey());
  16.             }
  17.         }
  18.     }
  19. }

四、集合框架工具类

1.Collections排序

代码如下(示例):

  1. public class One4 {
  2.     public static void main(String[] args) {
  3.         List<user> list=new ArrayList<>();
  4.         list.add(new user("景甜",1234));
  5.         list.add(new user("张一龙",19000));
  6.         list.add(new user("迪丽热巴",10004));
  7.         list.add(new user("书架",678));
  8.         list.add(new user("甜",1874));
  9.         
  10.         Collections.sort(list, new Comparator<user>() {
  11.             @Override
  12.             public int compare(user o1, user o2) {
  13.                 // TODO Auto-generated method stub
  14.                 return o1.getMoney()-o2.getMoney();
  15.             }
  16.         });
  17.         
  18.         
  19.         for (Object object : list) {
  20.             System.out.println(object);
  21.         }
  22.         
  23.   
  24.     }
  25. }
  26. class user {
  27.     private String name;
  28.     private int money;
  29.     public String getName() {
  30.         return name;
  31.     }
  32.     public void setName(String name) {
  33.         this.name = name;
  34.     }
  35.     
  36.     public int getMoney() {
  37.         return money;
  38.     }
  39.     public void setMoney(int money) {
  40.         this.money = money;
  41.     }
  42.     public user() {
  43.         // TODO Auto-generated constructor stub
  44.     }
  45.     
  46.     
  47.     public user(String name, int money) {
  48.         super();
  49.         this.name = name;
  50.         this.money = money;
  51.     }
  52.     @Override
  53.     public String toString() {
  54.         return "user [name=" + name + ", money=" + money + "]";
  55.     }
  56.     
  57.     
  58.     
  59. }


2.Arrays

①.Arrays.toString()

 这样结果会输出一串地址

 而用Arrays.toString 不一样

②.asList (将数组变成集合来操作,不能违背数组本身的特点

 错误示例:

③.sort (从小到大排序)

该处使用的url网络请求的数据。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号