HashMap排序问题

1package com.icos.utility.listsort; 2 3import java.util.ArrayList; 4import java.util.Comparator; 5import java.util.HashMap; 6import java.util.Map; 7import java.util.Map.Entry; 8import java.util.TreeMap; 9 10/** 11 * @给出一个集合,排一个叙出来 12 * 13 * */ 14public class CustomSort { 15 /** 16 * @Function 降序排 17 * 18 * */ 19 public static TreeMap<String, Float> sortFloatDESC(HashMap<String, Float> map) { 20 ValueComparatorDesc valComp = new ValueComparatorDesc(map); 21 TreeMap<String, Float> sortedMap = new TreeMap<String, Float>(valComp); 22 sortedMap.putAll(map); 23 return sortedMap; 24 } 25 26 /** 27 * @Function 升序排 28 * 29 * */ 30 public static TreeMap<String, Float> sortFloatASC(HashMap<String, Float> map) { 31 ValueComparatorAsc valComp = new ValueComparatorAsc(map); 32 TreeMap<String, Float> sortedMap = new TreeMap<String, Float>(valComp); 33 34 sortedMap.putAll(map); 35 36 return sortedMap; 37 } 38 39 public static void main(String[] args){ 40 HashMap<String, Float> map = new HashMap<String,Float>(); 41 map.put("A", (float) 0.9999); 42 map.put("B", (float) 0.111); 43 map.put("C", (float) 1.9090); 44 map.put("D", (float) 0.2); 45 46 CustomSort sf = new CustomSort(); 47 System.out.println(sf.sortFloatDESC(map)); 48 } 49 50 51 /** 获取一个连续数组,最大的那个数组 52 * @return */ 53 public static Integer[] getMaxCNumber(Integer[] array){ 54 ArrayList<Integer> maxArrays = new ArrayList<Integer>(); 55 ArrayList<Integer> nowArrays = new ArrayList<Integer>(); 56 // {0, 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; 57 int max = array[0] - 1; 58 for (int i = 0; i < array.length; i++) { 59// System.out.println("array[i]: "+array[i] + " ; max+1: "+(max+1)); 60 if (array[i] != max + 1) { 61 max = array[i] - 1; 62// System.out.println("--------------------------------,max="+(array[i] - 1) + " ;array[i]="+array[i]); 63 nowArrays.clear(); 64 } 65 66 nowArrays.add(array[i]); 67 max = array[i]; 68 69 if (nowArrays.size() >= maxArrays.size()) { 70 // maxArrays=nowArrays;这种情况是吧nowdays的地址赋值给了maxarrays的地址,这样以后只要nowarrays的地址改变,maxarrays的值也会改变 71 maxArrays = (ArrayList<Integer>) nowArrays.clone(); 72// System.out.println("nowArrays: "+nowArrays.toString()); 73// System.out.println("maxArrays: "+maxArrays.toString()); 74 } 75 } 76 77 return (Integer[]) maxArrays.toArray(new Integer[] {}); 78 } 79} 80 81/** 82 * @Function 降序排,从大到小 83 * 84 * */ 85class ValueComparatorDesc implements Comparator<String> { 86 87 Map<String, Float> mapFloat; 88 89 // 这里需要将要比较的map集合传进来 90 public ValueComparatorDesc(Map<String, Float> mapFloat) { 91 this.mapFloat = mapFloat; 92 } 93 94 public int compare(String a, String b) { 95 if (mapFloat.get(a) >= mapFloat.get(b)) { 96 return -1; 97 } else { 98 return 1; 99 } 100 } 101} 102 103/** 104 * @Function 升序排,从小到大 105 * 106 * */ 107class ValueComparatorAsc implements Comparator<String> { 108 109 Map<String, Float> mapFloat; 110 111 // 这里需要将要比较的map集合传进来 112 public ValueComparatorAsc(Map<String, Float> mapFloat) { 113 this.mapFloat = mapFloat; 114 } 115 116 public int compare(String a, String b) { 117 if (mapFloat.get(a) <= mapFloat.get(b)) { 118 return -1; 119 } else { 120 return 1; 121 } 122 } 123}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java spark list 转为 RDD 转为 dataset 写入表中

packagecom.example.demo;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.HashMap;importjava.util.List;importjava.util.Map;

java优化策略:hashMap内存初始化加载优化

 java优化策略:hashMap内存初始化加载优化packagecom.gsafety.opinion.pc.util;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjavax.a

FastJSON

 示例:importjava.util.ArrayList;importjava.util.List;importjava.util.HashMap;importjava.util.Map;importcom.alibaba.fastjson.JSON;importcom.a

JAVA数组去重常用方法

packagecom.zxj.test;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.HashMap;importjava.util.List;importjava.util.Map;

LinkedHashMap和HashMap的比较使用

importjava.util.HashMap;importjava.util.Iterator;importjava.util.LinkedHashMap;importjava.util.Map;publicclassTestLinkedHashMap{publics

HashMap排序问题 - HelloWorld