Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用重复值对HashMap进行排序_Java_Sorting_Hashmap - Fatal编程技术网

Java 使用重复值对HashMap进行排序

Java 使用重复值对HashMap进行排序,java,sorting,hashmap,Java,Sorting,Hashmap,您好,我正在使用以下代码对HashMap进行排序,它对映射进行了正确排序,但不计算重复值 Map<String, Integer> mymap = new HashMap<String, Integer>(); mymap.put("item1", 5); mymap.put("item2", 1); mymap.put("item3", 7); mymap.put("item4", 1); Map<String, Integer> tempMap = ne

您好,我正在使用以下代码对HashMap进行排序,它对映射进行了正确排序,但不计算重复值

Map<String, Integer> mymap = new HashMap<String, Integer>();
mymap.put("item1", 5);
mymap.put("item2", 1);
mymap.put("item3", 7);
mymap.put("item4", 1);

Map<String, Integer> tempMap = new HashMap<String, Integer>();
for (String wsState : mymap.keySet()) {
    tempMap.put(wsState, mymap.get(wsState));
}

List<String> mapKeys = new ArrayList<String>(tempMap.keySet());
List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
Object[] sortedArray = sortedSet.toArray();
int size = sortedArray.length;
for (int i = 0; i < size; i++) {
    sortedMap.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),
            (Integer) sortedArray[i]);
}
for (Map.Entry<String, Integer> entry : mymap.entrySet())
    System.out.println("Item is:" + entry.getKey() + " with value:"
            + entry.getValue());

System.out.println("***");

for (Map.Entry<String, Integer> entry : sortedMap.entrySet())
    System.out.println("Item is:" + entry.getKey() + " with value:"
            + entry.getValue());
它是一个HashMap,需要按值排序。 预期输出为:

Item is:item3 with value:7
Item is:item1 with value:5
Item is:item2 with value:1
Item is:item4 with value:1


您正在使用
TreeSet-sortedSet

按定义设置将不允许重复

下面是一个示例,它按照您期望的值进行排序,而不会丢失任何条目

import java.util.*;

public class Test {

public static Map<String, Integer> sortByValueDesc(Map<String, Integer> map) {
    List<Map.Entry<String, Integer>> list = new LinkedList(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });

    Map<String, Integer> result = new LinkedHashMap<>();
    for (Map.Entry<String, Integer> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

    public static void main(String[] args) {

        HashMap<String, Integer> map = new HashMap<String, Integer>();

        map.put("item1", 1);
        map.put("item2", 2);
        map.put("item3", 1);
        map.put("item4", 7);
        map.put("item5", 3);
        map.put("item6", 4);

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("Item is:" + entry.getKey() + " with value:"
                    + entry.getValue());
        }

        System.out.println("*******");

        Map<String,Integer> sortedMap = sortByValueDesc(map);

        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
            System.out.println("Item is:" + entry.getKey() + " with value:"
                    + entry.getValue());
        }

    }

}
为什么你会失去一个元素,这是你的问题:

//HERE YOU ARE GETTING ALL THE VALUES
List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

//YOU ARE INSERTING THE VALUES TO A TreeSet WHICH WILL REMOVE DUPLICATES
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
//这里是所有的值
List mapValues=new ArrayList(tempMap.values());
HashMap sortedMap=新建LinkedHashMap();
//您正在将值插入到树集,该树集将删除重复项
TreeSet-sortedSet=新的TreeSet(映射值);

我相信您需要树形图。它不允许重复,并将comparable作为键。如果compareTo()是可比较的,则根据它进行排序,如果不是,则在构造函数中询问comparator。地图不允许重复

    Map<String,Integer> mymap = new TreeMap<String,Integer>();
    mymap.put("item1", 5);
    mymap.put("item2", 1);
    mymap.put("item3", 7);
    mymap.put("item4", 1);

for(Map.Entry<String, Integer> entry: mymap.entrySet())
            System.out.println("Item is:" + entry.getKey() + " with value:" + 
                    entry.getValue());
Map mymap=newtreemap();
mymap.put(“第1项”,第5项);
mymap.put(“项目2”,1);
mymap.put(“第3项”,第7项);
mymap.put(“项目4”,1);
对于(Map.Entry:mymap.entrySet())
System.out.println(“项为:”+entry.getKey()+”,值为:“+
entry.getValue());
公共静态void main(字符串[]args){
Set s=新树集();
s、 增加(新项目(“项目1”,5));
s、 增加(新项目(“项目2”,1));
s、 增加(新项目(“项目3”,7));
s、 增加(新项目(“项目4”,1));
(项目编号:s){
System.out.println(“项为:”+it.getName()+”,值为:
+it.getValue());
}
}
类项实现了可比较的{
私有整数值;
私有字符串名称;
项(字符串名称,int val){
this.name=名称;
this.value=val;
}
//吸气剂和集
公共布尔等于(对象obj){
if(obj==null)
返回false;
如果(obj==此)
返回true;
如果(obj.getClass()!=getClass())
返回false;
项目i=(项目)obj;
如果(i.getValue().equals)(getValue())
&&i.getName().equals(getName())
返回true;
返回false;
}
公共整数比较(o项){
返回getValue().compareTo(o.getValue());
}
公共布尔等于(对象o){
返回false;
}
}

不确定要做什么,按键排序?价值数一数重复的?什么?我有一个HashMap,需要按值对它进行排序。好的,然后创建treeMap并放入其中,如:Value->key,它会自动排序,然后创建HashMap并读取treeMap中的所有值并放入HashMap,不需要任何逻辑输入的预期输出是什么?那就是clear@elbek谢谢你的问题,我已经更新了这个问题,但它是值的副本而不是键的副本。@emeemtana编辑我的答案也可以解决为什么你会失去一个值你的例子仍然不能像预期的那样工作。它仍然以非降序返回值(应用sortByValue方法后,我得到了10,10,10,3,4,3,10,10,6,1,1)。@chao请描述一下你是如何插入和排序的。。我3年前刚刚运行了这段代码,它根据您的评论提供的输入工作。(10,10,10,10,6,4,3,3,1,1)
Item is:item4 with value:7
Item is:item2 with value:2
Item is:item3 with value:1
Item is:item1 with value:1
Item is:item6 with value:4
Item is:item5 with value:3
*******
Item is:item4 with value:7
Item is:item6 with value:4
Item is:item5 with value:3
Item is:item2 with value:2
Item is:item3 with value:1
Item is:item1 with value:1
//HERE YOU ARE GETTING ALL THE VALUES
List<Integer> mapValues = new ArrayList<Integer>(tempMap.values());
HashMap<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();

//YOU ARE INSERTING THE VALUES TO A TreeSet WHICH WILL REMOVE DUPLICATES
TreeSet<Integer> sortedSet = new TreeSet<Integer>(mapValues);
    Map<String,Integer> mymap = new TreeMap<String,Integer>();
    mymap.put("item1", 5);
    mymap.put("item2", 1);
    mymap.put("item3", 7);
    mymap.put("item4", 1);

for(Map.Entry<String, Integer> entry: mymap.entrySet())
            System.out.println("Item is:" + entry.getKey() + " with value:" + 
                    entry.getValue());
public static void main(String[] args) {
    Set<Item> s = new TreeSet<Item>();
    s.add(new Item("item1", 5));
    s.add(new Item("item2", 1));
    s.add(new Item("item3", 7));
    s.add(new Item("item4", 1));

    for (Item it : s) {
        System.out.println("Item is:" + it.getName() + " with value:"
                + it.getValue());
    }

}

class Item implements Comparable<Item> {
    private Integer value;
    private String name;

    Item(String name, int val) {
        this.name = name;
        this.value = val;
    }

    // getters and sets

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (obj.getClass() != getClass())
            return false;

        Item i = (Item) obj;
        if (i.getValue().equals(getValue())
                && i.getName().equals(getName()))
            return true;
        return false;

    }

    public int compareTo(Item o) {
        return getValue().compareTo(o.getValue());
    }

    public boolean equals(Object o) {
        return false;
    }
}