• java统计List中的元素重复出现的次数和对map按key或键值排序


    package cn.php;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    public class CountList {
    	public static void main(String[] args) {
    		List<String> strs = Arrays.asList("a", "b", "c", "d", "e", "a", "a",
    				"a", "a", "b", "b", "b");
    		Map<String, Integer> map = new HashMap<String, Integer>();
    		Set<String> set = new HashSet<String>(strs);
    		for (String str : set) {
    			for (String lstr : strs) {
    				if (str.equals(lstr)) {
    					if (map.containsKey(str)) {
    						Integer count = map.get(str);
    						count++;
    						map.put(str, count);
    					} else {
    						map.put(str, 1);
    					}
    
    				}
    			}
    		}
    		//printMap(map);
    		Map<String, Integer> sortMap = sortMapByValue(map);
    		printMap(sortMap);
    	}
    
    	private static Map<String,Integer> sortMapByValue(Map<String, Integer> map) {
    		List<Map.Entry<String, Integer>> mapList = new ArrayList<Map.Entry<String, Integer>>(
    				map.entrySet());
    		Collections.sort(mapList, new Comparator<Map.Entry<String, Integer>>() {
    			@Override
    			public int compare(Entry<String, Integer> o1,
    					Entry<String, Integer> o2) {
    				return o1.getValue()-o2.getValue();
    			}
    		});
    		Map<String,Integer> result = new LinkedHashMap<String,Integer>();
    		for(Map.Entry<String, Integer> entry:mapList){
    			result.put(entry.getKey(), entry.getValue());
    		}
    		return result;
    	}
    
    	public static void printMap(Map<String, Integer> map) {
    
    		for (Map.Entry<String, Integer> entry : map.entrySet()) {
    			System.out.println("Key : " + entry.getKey() + " Value : "
    					+ entry.getValue());
    		}
    
    	}
    
    }

    	public Map<String, String> sortMapByKey(Map<String, String> oriMap) {
    		if (oriMap == null || oriMap.isEmpty()) {
    			return null;
    		}
    		Map<String, String> sortedMap = new TreeMap<String, String>(
    				new Comparator<String>() {
    					public int compare(String key1, String key2) {
    						int intKey1 = 0, intKey2 = 0;
    						try {
    							intKey1 = getInt(key1);
    							intKey2 = getInt(key2);
    						} catch (Exception e) {
    							intKey1 = 0;
    							intKey2 = 0;
    						}
    						return intKey1 - intKey2;
    					}
    				});
    		sortedMap.putAll(oriMap);
    		return sortedMap;
    	}
    
    	private int getInt(String str) {
    		int i = 0;
    		try {
    			Pattern p = Pattern.compile("^\d+");
    			Matcher m = p.matcher(str);
    			if (m.find()) {
    				i = Integer.valueOf(m.group());
    			}
    		} catch (NumberFormatException e) {
    			e.printStackTrace();
    		}
    		return i;
    	}
    
  • 相关阅读:
    JavaScript核心参考
    面向对象的程序设计之工厂模式
    ES6中promise的使用方法
    详解 Vue 2.4.0 带来的 4 个重大变化
    Vue.js 1.x 和 2.x 实例的生命周期
    表单控件的全面分析
    元素的一些常用属性
    为表格增加的方法
    Element类型知识大全
    6-3.斜体标签
  • 原文地址:https://www.cnblogs.com/dapeng520/p/4610081.html
Copyright © 2020-2023  润新知