Java去除一个Map中value值最小的K个记录

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值