排序HashMap<?,?>按键 [英] Sorting HashMap<?,?> by key

查看:79
本文介绍了排序HashMap<?,?>按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello
我需要实现一个方法,它接收一个HashMap并按键排序(mergeSort)它的值(不使用TreeMap,SortedMap或Collections.Sort或使用JAVA包的任何排序解决方案)即可。
我的问题是处理通配符类型...
这是我的实现(由于使用通配符,返回编译错误)

pre > public HashMap<?,?> mergeSort(HashMap<?,?> map){
if(map.size()<1){
return map;
}
//向下舍入
int middle = map.size()/ 2;
int location = 0;

HashMap<?,?> mapLeft = new HashMap<?,?>();
HashMap<?,?> mapRight = new HashMap<?,?>();
//分割映射
for(Iterator<> keyIter = map.keySet()。iterator(); keyIter.hasNext();){
if(location< middle) {
mapLeft.put(keyIter,map.get(keyIter));
} else {
mapRight.put(keyIter,map.get(keyIter));
}
位置++;
}
//递归调用
mapLeft = mergeSort(mapLeft);
mapRight = mergeSort(mapRight);
返回合并(mapLeft,mapRight);
}

public HashMap<?,?>合并(HashMap<?,?> mapLeft,HashMap<?,?> mapRight){
HashMap<?,?> result = new HashMap<?,?>();
迭代器<?> keyLeftIter = mapLeft.keySet()。iterator();
迭代器<?> keyRightIter = mapRight.keySet()。iterator();
String keyLeft;
字符串keyRight;
while(keyLeftIter.hasNext()){
keyLeft = keyLeftIter.next();
while(keyRightIter.hasNext()){
keyRight = keyRightIter.next(); (keyLeft.compareTo(keyRight)< 0){
result.put(keyLeft,mapLeft.get(keyLeft));

if
keyLeft = keyLeftIter.next();
} else {
result.put(keyRight,mapRight.get(keyRight));
keyRight = keyRightIter.next();
}
}
}
返回结果;
}

感谢您的帮助!

解决方案

与其他评论者一样,我建议阅读Java中的泛型主题。你在merge中做的是在结果上使用通配符HashMap

  HashMap<?,?> result = new HashMap<?,?>(); 

当你把通配符放在它上面时,你基本上会说我只会从这里读取。稍后,您尝试在
$ b

  result.put(keyLeft,mapLeft.get(keyLeft)); 

编译器会说:嘿,你刚告诉我你只会读,现在你想放一些在...失败



然后它会产生您的编译时错误。



解决方案



不要将通配符放在您要修改的集合上。


hello I need to implement a method that receives a HashMap and sorts (mergeSort) it's values by key (without using TreeMap, SortedMap or Collections.Sort or use any sort solutions from JAVA Packages). my problem is dealing with the wildcard Types... this is my implementation (that returns compilation errors because of wildcards use)

public HashMap<?, ?> mergeSort(HashMap<?, ?> map) {
        if (map.size() < 1) {
            return map;
        }
        // rounds downwards
        int middle = map.size() / 2;
        int location = 0;

        HashMap<?,?> mapLeft = new HashMap<?, ?>();
        HashMap<?,?> mapRight = new HashMap<?, ?>();
        // splitting map
        for (Iterator<?> keyIter = map.keySet().iterator(); keyIter.hasNext();) {
            if (location < middle) {
                mapLeft.put(keyIter, map.get(keyIter));
            } else {
                mapRight.put(keyIter, map.get(keyIter));
            }
            location++;
        }
        // recursive call
        mapLeft = mergeSort(mapLeft);
        mapRight = mergeSort(mapRight);
        return merge(mapLeft, mapRight);
    }

    public HashMap<?, ?> merge(HashMap<?, ?> mapLeft, HashMap<?, ?> mapRight) {
        HashMap<?, ?> result = new HashMap<?, ?>();
        Iterator<?> keyLeftIter = mapLeft.keySet().iterator();
        Iterator<?> keyRightIter = mapRight.keySet().iterator();
        String keyLeft;
        String keyRight;
        while (keyLeftIter.hasNext()) {
            keyLeft = keyLeftIter.next();
            while (keyRightIter.hasNext()) {
                keyRight = keyRightIter.next();

                if (keyLeft.compareTo(keyRight) < 0) {
                    result.put(keyLeft, mapLeft.get(keyLeft));
                    keyLeft = keyLeftIter.next();
                } else {
                    result.put(keyRight, mapRight.get(keyRight));
                    keyRight = keyRightIter.next();
                }
            }
        }
        return result;
    }

I appreciate your help!

解决方案

Like other commenters I would suggest reading up on the subject of generics in Java. What you did in merge is using wildcards on the result HashMap

HashMap<?, ?> result = new HashMap<?, ?>();

When you put wildcards on it, you are basically saying "I will only be reading from this". Later on you trying to push something in

result.put(keyLeft, mapLeft.get(keyLeft));

The compiler will say "Hey, you just told me you would only read and now you want to put something in... FAIL

Then it generates your compile time errors.

Solution

Don't put wildcards on collections you will modify.

这篇关于排序HashMap&lt;?,?&gt;按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆