Java去除一个Map中value值最小的K个记录
来源:互联网 发布:腾讯红包大数据,归属地 编辑:程序博客网 时间:2024/06/07 01:27
import java.util.*;public class TestHeapSorted { public static void main(String[] args) { Map<String, String> testMap = new HashMap<String, String>(); testMap.put("tag2", "2"); testMap.put("tag1", "1"); testMap.put("tag3", "3"); testMap.put("tag5", "5"); testMap.put("tag4", "4"); testMap.put("tag7", "7"); testMap.put("tag9", "9"); testMap.put("tag8", "8"); testMap.put("tag6", "6"); Object[] testObjectValue = testMap.values().toArray(); TestHeapSorted testHeapSorted = new TestHeapSorted(); System.out.println(testHeapSorted.GetLeastNumbers_Solution(testObjectValue, (3 + 1))); List<Double> testList = testHeapSorted.GetLeastNumbers_Solution(testObjectValue, (3 + 1)); Iterator<Map.Entry<String, String>> it = testMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); for (int i = 0; i < testList.size(); i++) { if (Double.parseDouble(entry.getValue()) == testList.get(i)) { testList.remove(i); it.remove(); break; } } } for (String key : testMap.keySet()) { System.out.println(key + "=>" + testMap.get(key)); } System.out.println(testList.size()); } public ArrayList<Double> GetLeastNumbers_Solution(Object[] input, int k) { ArrayList<Double> res = new ArrayList<Double>(); if (input == null || input.length == 0 || input.length < k) { return res; } if (k == 0) return res; double[] maxHeap = new double[k]; //初始化堆 for (int i = 0; i < maxHeap.length; i++) { maxHeap[i] = Double.parseDouble(input[i].toString()); } //将初始化的堆调整为最大堆。最大堆:根节点的值总是大于子树中任意节点的值 // 注意是--i,从下往上 for (int i = (maxHeap.length - 1) / 2; i >= 0; i--) { adjustHeap(maxHeap, i); } //遍历初始数组不断调整最大堆 for (int i = k; i < input.length; i++) { if (maxHeap[0] > Double.parseDouble(input[i].toString())) { maxHeap[0] = Double.parseDouble(input[i].toString()); adjustHeap(maxHeap, 0); // 是0位置,即根节点的值为最大值 } } for (int i = 0; i < maxHeap.length; i++) { res.add(maxHeap[i]); } return res; } static void adjustHeap(double maxHeap[], int i) { int index = i; int lchild = 2 * i + 1; //i的左孩子节点序号 int rchild = 2 * i + 2; //i的右孩子节点序号 if (index <= (maxHeap.length - 1) / 2) { //寻找子节点中最大的节点 if (lchild < maxHeap.length && maxHeap[index] < maxHeap[lchild]) { //左孩子大于index index = lchild; } if (rchild < maxHeap.length && maxHeap[index] < maxHeap[rchild]) { //右孩子大于index index = rchild; //此时index为左右孩子中较大的那个 } if (i != index) { //将节点与最大的子节点交换 double tmp = maxHeap[index]; maxHeap[index] = maxHeap[i]; maxHeap[i] = tmp; //交换后,子树可能不满足最大推,递归调整。 adjustHeap(maxHeap, index); //因为交换下来一个小的数字,要调整的是被交换的子树 } } }}
答案:
[4.0, 3.0, 2.0, 1.0]
tag5=>5
tag8=>8
tag9=>9
tag6=>6
tag7=>7
0
阅读全文
0 0
- Java去除一个Map中value值最小的K个记录
- 在Java中自定义的一个key对多个Value的map
- 找出一个数组中最小的K个元素
- 多个KEY值对应一个VALUE的MAP
- map的一个key多个value
- 在一个数组中查找最大的K个元素或者最小的K个元素
- 【方法1】删除Map中Value重复的记录,并且只保留Key最小的那条记录
- 【方法2】删除Map中Value重复的记录,并且只保留Key最小的那条记录
- 【方法3:Perl版本】删除Map中Value重复的记录,并且只保留Key最小的那条记录
- 求一维数组中最小的k个值
- java Map 一个key其实可以保存多个value
- java 获取 Map中所有的 key 和 value 值
- java 获取map中所有的key和value值
- java对map中value的排序
- 从map中取出最大或最小value值对应的key值
- Map之一个Key存多个Value的MultiValueMap(一个键多个值)
- 线性时间内从一个数组中找出第K个最小的元素
- 在一个无序数组中找到第K个最小或者最大的元素
- 记一次踩坑|空table应该编码为数组还是对象
- day03 LED灯源程序编写与编译、UART原理
- day05 UART实战、源码阅读工具(Sourceinsight/Ctags)、Makefile编写
- day04 UART协议
- 高效能人士的七个习惯总结
- Java去除一个Map中value值最小的K个记录
- 高PowerSurfacing RE v2.4-4.1 for SolidWorks 2012-2017 Win64 6CD
- C/C++ pthread_cond_timedwait()函数使用心得
- day06 链接脚本、shell程序优化、IIC总线
- 阿里云数加产品家族图首次亮相
- LintCode 431 Connected Component in Undirected Graph
- 如何选择一个适合自己的专业
- [SVN] 分支同步、合入主干操作分享
- oninput事件