zoukankan      html  css  js  c++  java
  • java===map集合

    package 集合框架5.hashmap.treemap;
    
    import java.util.HashMap;
    
    /**
     * HashMap集合:
     * map集合最大特点为双列集合,键值对存在对应关系(映射);
     * HashMap数据结构是哈希表,因此需要覆写hashCode()、
     * equals()方法;保证key的唯一性,value是可以重复的;
     * 
     * 例:写一个输入日期,返回备注信息的程序;
     * map在有映射关系时优先选择;
     * 在查表法中常见
     * */
    public class HashMapDemo {
    
        public static void main(String[] args) {
            
            
            
            String str = getInformation("17年5月3号");
            System.out.println(str);
    
        }
    
        private static String getInformation(String day) {
            HashMap<String, String> map1 = new HashMap<String,String>();
            map1.put("17年5月1号", "参加世界和平酒会");
            map1.put("17年5月2号", "网络安全研讨会");
            map1.put("17年5月3号", "陪家人");
            map1.put("17年5月4号", "去夏威夷晒太阳");
            map1.put("17年5月5号", "返回北京");
            map1.put("17年5月6号", "开始工作");
            
            return map1.get(day);
            
        }
    
    }
    package 集合框架5.hashmap.treemap;
    
    import java.util.Iterator;
    import java.util.TreeMap;
    
    /**
     * TreeMap:
     * 数据结构:二叉树;所以要实现implements Comparator接口,
     * 实现比较功能,从而进行二叉树排序;java中很多类都实现了这个接口或者Comparable接口
     * Comparable:给对象类添加比较功能
     * Comparator:给集合添加比较器
     * 
     * 例:"asdfA#AjkC Cha wguuivd-skd+ * jhdf"找出这个字符串中每个字母出现的次数(包含大小写,不包含空格特殊字符)
     * 思路:
     * 1、要对字符串中每个字符进行操作,先将字符串转成字符数组;
     * 2、返回数值是键值对;a(5) 所以联想到使用map集合,而且a,b,c,d是有顺序的所以用TreeMap集合更好
     * 3、创建一个TreeMap集合,遍历字符数组,同时将结果存入TreeMap集合;
     * 4、根据要求只输出字母的出现次数,所以chs[i]<='z'&&chs[i]>='a' || chs[i]<='Z'&&chs[i]>='A'
     * 5、存入时需要判断字符是否存在集合中并添加计数器如果存在value+1;因为map中key要唯一,所以用来存字母;
     * 
     * */
    public class TreeMapDemo {
    
        public static void main(String[] args) {
            String str1 = "asdfA#AAjkC Cdha wguuivd-skd+ * jhdf";
            char[]chs = str1.toCharArray();
            TreeMap<Character, Integer> map = new TreeMap<Character,Integer>();
            for (int i = 0; i < chs.length; i++) {
                if(!(chs[i]<='z'&&chs[i]>='a' || chs[i]<='Z'&&chs[i]>='A')){
                    continue;
                }
                int value=1;
                if(map.containsKey(chs[i])==false){
                    map.put(chs[i], value);
                    
                }else{
                    value=map.get(chs[i]);
                    map.put(chs[i], value+1);
                }
            }
            System.out.println(mapToString(map));
    
        }
    
        private static StringBuilder mapToString(TreeMap<Character, Integer> map) {
            StringBuilder sb1 = new StringBuilder();
            for(Iterator<Character> it1 = map.keySet().iterator();it1.hasNext();){
                Character key = it1.next();
                Integer value = map.get(key);
                sb1.append(key+"("+value+")");
            }
            return sb1;
        }
    
    }
  • 相关阅读:
    DX 系列之 ComboBoxEdit
    nodejs web API 相关杂项
    解决跨域No 'Access-Control-Allow-Origin' header is present on the requested resource.
    git在本地回退
    docker容器访问宿主机IP
    mongodb+nodejs
    docker-compose控制启动顺序
    angular7 + d3 显示svg
    新建ng工程
    CSS样式
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6738022.html
Copyright © 2011-2022 走看看