常用Java代码汇总

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/System_out_print_Boy/article/details/83069934

排序问题 

private Map<String, Long> sortMapByValueLong(Map<String, Long> oriMap, final boolean isAsc) {  
        Map<String, Long> sortedMap = new LinkedHashMap<String, Long>();  
        if (oriMap != null && !oriMap.isEmpty()) {  
            List<Map.Entry<String, Long>> entryList = new ArrayList<Map.Entry<String, Long>>(oriMap.entrySet());  
            Collections.sort(entryList,  
                    new Comparator<Map.Entry<String, Long>>() {  
                        public int compare(Entry<String, Long> entry1,  
                                Entry<String, Long> entry2) {  
                        	long value1 = 0, value2 = 0;  
                            try {  
                                value1 = entry1.getValue();
                                value2 = entry2.getValue();
                            } catch (NumberFormatException e) {  
                                value1 = 0;  
                                value2 = 0;  
                            } 
                            // 判定
                            long rst = 0;
                            if (isAsc) {
                            	rst = value1 - value2;
                            } else {
                            	rst = value2 - value1;
                            }
                            return (int)rst;
                        }  
                    });  
            Iterator<Map.Entry<String, Long>> iter = entryList.iterator();  
            Map.Entry<String, Long> tmpEntry = null;
            while (iter.hasNext()) {  
                tmpEntry = iter.next();  
                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());  
            }  
        }  
        return sortedMap; 
    }

Map转list

	Map<String,String> map=new HashMap<String,String>();
		map.put("1", "1");
		map.put("2", "2");
		List<Map.Entry<String, String>> list=new ArrayList<Map.Entry<String,String>>(map.entrySet());

带有小数点的字符串转为int

int abc =Double.valueOf(num).intValue();

猜你喜欢

转载自blog.csdn.net/System_out_print_Boy/article/details/83069934