package com.icos.utility.listsort;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* @给出一个集合,排一个叙出来
*
* */
public class CustomSort {
/**
* @Function 降序排
*
* */
public static TreeMap<String, Float> sortFloatDESC(HashMap<String, Float> map) {
ValueComparatorDesc valComp = new ValueComparatorDesc(map);
TreeMap<String, Float> sortedMap = new TreeMap<String, Float>(valComp);
sortedMap.putAll(map);
return sortedMap;
}
/**
* @Function 升序排
*
* */
public static TreeMap<String, Float> sortFloatASC(HashMap<String, Float> map) {
ValueComparatorAsc valComp = new ValueComparatorAsc(map);
TreeMap<String, Float> sortedMap = new TreeMap<String, Float>(valComp);
sortedMap.putAll(map);
return sortedMap;
}
public static void main(String[] args){
HashMap<String, Float> map = new HashMap<String,Float>();
map.put("A", (float) 0.9999);
map.put("B", (float) 0.111);
map.put("C", (float) 1.9090);
map.put("D", (float) 0.2);
CustomSort sf = new CustomSort();
System.out.println(sf.sortFloatDESC(map));
}
/** 获取一个连续数组,最大的那个数组
* @return */
public static Integer[] getMaxCNumber(Integer[] array){
ArrayList<Integer> maxArrays = new ArrayList<Integer>();
ArrayList<Integer> nowArrays = new ArrayList<Integer>();
// {0, 1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
int max = array[0] - 1;
for (int i = 0; i < array.length; i++) {
// System.out.println("array[i]: "+array[i] + " ; max+1: "+(max+1));
if (array[i] != max + 1) {
max = array[i] - 1;
// System.out.println("--------------------------------,max="+(array[i] - 1) + " ;array[i]="+array[i]);
nowArrays.clear();
}
nowArrays.add(array[i]);
max = array[i];
if (nowArrays.size() >= maxArrays.size()) {
// maxArrays=nowArrays;这种情况是吧nowdays的地址赋值给了maxarrays的地址,这样以后只要nowarrays的地址改变,maxarrays的值也会改变
maxArrays = (ArrayList<Integer>) nowArrays.clone();
// System.out.println("nowArrays: "+nowArrays.toString());
// System.out.println("maxArrays: "+maxArrays.toString());
}
}
return (Integer[]) maxArrays.toArray(new Integer[] {});
}
}
/**
* @Function 降序排,从大到小
*
* */
class ValueComparatorDesc implements Comparator<String> {
Map<String, Float> mapFloat;
// 这里需要将要比较的map集合传进来
public ValueComparatorDesc(Map<String, Float> mapFloat) {
this.mapFloat = mapFloat;
}
public int compare(String a, String b) {
if (mapFloat.get(a) >= mapFloat.get(b)) {
return -1;
} else {
return 1;
}
}
}
/**
* @Function 升序排,从小到大
*
* */
class ValueComparatorAsc implements Comparator<String> {
Map<String, Float> mapFloat;
// 这里需要将要比较的map集合传进来
public ValueComparatorAsc(Map<String, Float> mapFloat) {
this.mapFloat = mapFloat;
}
public int compare(String a, String b) {
if (mapFloat.get(a) <= mapFloat.get(b)) {
return -1;
} else {
return 1;
}
}
}
HashMap排序问题
点赞
收藏