Java 按键和值对映射进行排序
我想按键和值对地图进行排序。先按键,然后按值。 例如,这应该是结果Java 按键和值对映射进行排序,java,sorting,map,Java,Sorting,Map,我想按键和值对地图进行排序。先按键,然后按值。 例如,这应该是结果 1,2 1,3 2,1 2,2 有人对如何有效地实现这一目标提出了建议吗?我看到有人使用树形图对键进行排序,但我也需要值 或者使用任何其他方法对键和值进行排序都是受欢迎的。这是不可能的,因为映射不能包含重复的键。TreeMap始终按键值排序(假设键类型为可比较的) 但对于这些任务,我们通常使用值为列表的地图: Map<Integer, List<Integer>> map = new TreeMap&l
1,2
1,3
2,1
2,2
有人对如何有效地实现这一目标提出了建议吗?我看到有人使用树形图对键进行排序,但我也需要值
或者使用任何其他方法对键和值进行排序都是受欢迎的。这是不可能的,因为映射不能包含重复的键。
TreeMap
始终按键值排序(假设键类型为可比较的
)
但对于这些任务,我们通常使用值为列表的地图:
Map<Integer, List<Integer>> map = new TreeMap<Integer, List<Integer>>();
// add some values in random order
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(1);
map.put(2,list);
list = new ArrayList<Integer>();
list.add(2);
list.add(1);
map.put(1,list);
// result
for (Integer key:map.keySet()) { // map is already sorted
List<Integer> value = map.get(key);
Collections.sort(value); // list of values needs sorting
for (Integer innerValue:value) {
System.out.printf("%s : %s%n", key, innerValue);
}
}
Map Map=newtreemap();
//按随机顺序添加一些值
列表=新的ArrayList();
增加(2);
增加第(1)款;
地图放置(2,列表);
列表=新的ArrayList();
增加(2);
增加第(1)款;
地图放置(1,列表);
//结果
对于(整型键:map.keySet()){//map已排序
列表值=map.get(键);
Collections.sort(value);//值列表需要排序
for(整数innerValue:value){
System.out.printf(“%s:%s%n”,键,内部值);
}
}
你不能拥有这样的地图
1->11
1->21
键“1”很常见,因此21将取代11如果您愿意冒险,可以使用允许您指定比较器的构造函数: 话虽如此,你想做的事情是丑陋的和非法的,因为: *顺序与equals()不一致,equals()是SortedSet的必要条件) *由于映射中的值改变,顺序可能会改变,我不知道它的实现是否允许这种情况 我想你还需要别的东西。也许您最好创建一个同时具有key和value并正确实现equals()、hashcode()和Comparable的对象,并使用SortedSet
编辑:我已经回答了一般性的问题(根据键和值对地图进行排序),而没有查看您的示例。正如其他人所写,地图中不能有重复的键。听起来像是需要某种类型的多重地图,例如
SortedMap<Key, SortedSet<Value>> map = new TreeMap<Key, SortedSet<Value>>();
map.put(1, new TreeSet<Integer>(Arrays.asList(1, 2)));
map.put(2, new TreeSet<Integer>(Arrays.asList(2, 1)));
System.out.println(map);
其他答案指出了重复键的问题,但我猜您有要排序的对,而映射位只是一个错误。我能想到的最干净的解决方案是创建一个定制的Pair类,该类实现Comparator并比较两个Pair的键和值。然后可以使用Collections.sort对其进行排序。总体思路是,将地图转换为列表,对列表进行排序,然后将排序后的列表放回地图 映射-->列表-->排序-->映射 例子
import java.util.Collections;
导入java.util.Comparator;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.LinkedHashMap;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
公共类排序表{
公共静态void main(字符串[]args){
System.out.println(“反排序映射…”);
Map unsortMap=newhashmap();
不排序映射。放置(“1”、“1”);
不排序图。放置(“2”、“A”);
不排序地图。放置(“3”、“2”);
迭代器迭代器=unsortMap.entrySet().Iterator();
对于(Map.Entry:unsortMap.entrySet()){
System.out.println(“Key:+entry.getKey()
+“值:”+entry.getValue());
}
System.out.println(“分类地图……”);
Map sortedMap=sortByComparator(unsortMap);
对于(Map.Entry:sortedMap.entrySet()){
System.out.println(“Key:+entry.getKey()
+“值:”+entry.getValue());
}
}
私有静态映射排序比较程序(映射非排序映射){
List=新的LinkedList(unsortMap.entrySet());
//基于比较器的排序表
Collections.sort(list,newcomparator(){
公共整数比较(对象o1、对象o2){
return((可比)((Map.Entry)(o1)).getValue())
.compareTo(((Map.Entry)(o2)).getValue();
}
});
//再次将已排序的列表放入地图
Map sortedMap=new LinkedHashMap();
for(Iterator it=list.Iterator();it.hasNext();){
Map.Entry=(Map.Entry)it.next();
sortedMap.put(entry.getKey(),entry.getValue());
}
返回分拣的DMAP;
}
}
导入java.util.SortedSet;
导入java.util.TreeSet;
公共类Sortmaponkey和Value{
公共静态void main(字符串[]args){
SortedSet SortedSet=新树集();
sortedSet.add(新的KeyValuePair(1,2));
sortedSet.add(新的KeyValuePair(2,2));
sortedSet.add(新的KeyValuePair(1,3));
sortedSet.add(新的KeyValuePair(2,1));
用于(KeyValuePair KeyValuePair:sortedSet){
System.out.println(keyValuePair.key+,“+keyValuePair.value);
}
}
}
类KeyValuePair实现可比较的{
int键,值;
公钥值对(int键,int值){
超级();
this.key=key;
这个值=值;
}
公共整数比较(键值对o){
return key==o.key?value-o.value:key-o.key;
}
}
你要找的是谷歌Guava库的一部分SortedSetMultimap。它们包括的实现名为TreeMultimap
:如果您不熟悉它,Guava是一个非常棒的库,它包含了很多您有时认为应该在标准Java库中的优秀内容。事实上,我认为Java8将包含一些来自Guava的东西(至少在我看来这是本项目的大意:)。请遵循以下代码:- 此代码将首先按键对映射进行排序,然后按值对映射进行排序。 只需编写一个main方法并按如下方式调用此方法:-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SortMapByKeyAndValue
{
public static void main(String[] args)
{
aMapSortProgramByKeyAndValue();
}
private static void aMapSortProgramByKeyAndValue()
{
Map<String, Integer> myMap = new HashMap<String, Integer>();
// putting values in the Map
myMap.put("Jayant", 80);
myMap.put("Abhishek", 90);
myMap.put("Anushka", 80);
myMap.put("Amit", 75);
myMap.put("Spandan", 50);
myMap.put("Hari", 55);
myMap.put("Keshav", 60);
System.out.println("Map data without Sort :-");
for (Entry<String, Integer> myEntryMapData : myMap.entrySet())
{
System.out.println("The Map data is Key: " + myEntryMapData.getKey() + " Value: "
+ myEntryMapData.getValue());
}
List<Entry<String, Integer>> myMapDataAsList = new ArrayList<Map.Entry<String, Integer>>();
myMapDataAsList.addAll(myMap.entrySet());
System.out.println("Map data Stored in List, The whole List is : " + myMapDataAsList);
Iterator<Entry<String, Integer>> myListIterator = myMapDataAsList.iterator();
System.out.println("Map data Stored in List, Print through iterator :-");
for (; myListIterator.hasNext();)
{
Entry<String, Integer> myListData = myListIterator.next();
System.out.println("The List data is Key: " + myListData.getKey() + " Value: " + myListData.getValue());
}
Collections.sort(myMapDataAsList, new Comparator<Entry<String, Integer>>()
{
@Override
public int compare(Entry<String, Integer> dataOne, Entry<String, Integer> dataTwo)
{
return dataOne.getKey().compareTo(dataTwo.getKey());
}
});
System.out.println("After Sort by the Key the Map data is : ");
myListIterator = myMapDataAsList.iterator();
for (; myListIterator.hasNext();)
{
Entry<String, Integer> myListData = myListIterator.next();
System.out.println("The List data is Key: " + myListData.getKey() + " Value: " + myListData.getValue());
}
Collections.sort(myMapDataAsList, new Comparator<Entry<String, Integer>>()
{
@Override
public int compare(Entry<String, Integer> dataOne, Entry<String, Integer> dataTwo)
{
return dataOne.getValue().compareTo(dataTwo.getValue());
}
});
System.out.println("After Sort by the vale the Map data is : ");
myListIterator = myMapDataAsList.iterator();
for (; myListIterator.hasNext();)
{
Entry<String, Integer> myListData = myListIterator.next();
System.out.println("The List data is Key: " + myListData.getKey() + " Value: " + myListData.getValue());
}
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入java.util.Map.Entry;
公共类SortMapByKeyAndValue
{
公共静态void main(字符串[]args)
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class SortMyMap{
public static void main(String[] args) {
System.out.println("Unsort Map......");
Map<String,String> unsortMap = new HashMap<String,String>();
unsortMap.put("1", "1");
unsortMap.put("2", "A");
unsortMap.put("3", "2");
Iterator iterator=unsortMap.entrySet().iterator();
for (Map.Entry entry : unsortMap.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
System.out.println("Sorted Map......");
Map<String,String> sortedMap = sortByComparator(unsortMap);
for (Map.Entry entry : sortedMap.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
}
private static Map sortByComparator(Map unsortMap) {
List list = new LinkedList(unsortMap.entrySet());
//sort list based on comparator
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
//put sorted list into map again
Map sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
import java.util.SortedSet;
import java.util.TreeSet;
public class SortMapOnKeyAndValue {
public static void main(String[] args) {
SortedSet<KeyValuePair> sortedSet = new TreeSet<KeyValuePair>();
sortedSet.add(new KeyValuePair(1, 2));
sortedSet.add(new KeyValuePair(2, 2));
sortedSet.add(new KeyValuePair(1, 3));
sortedSet.add(new KeyValuePair(2, 1));
for (KeyValuePair keyValuePair : sortedSet) {
System.out.println(keyValuePair.key+","+keyValuePair.value);
}
}
}
class KeyValuePair implements Comparable<KeyValuePair>{
int key, value;
public KeyValuePair(int key, int value) {
super();
this.key = key;
this.value = value;
}
public int compareTo(KeyValuePair o) {
return key==o.key?value-o.value:key-o.key;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SortMapByKeyAndValue
{
public static void main(String[] args)
{
aMapSortProgramByKeyAndValue();
}
private static void aMapSortProgramByKeyAndValue()
{
Map<String, Integer> myMap = new HashMap<String, Integer>();
// putting values in the Map
myMap.put("Jayant", 80);
myMap.put("Abhishek", 90);
myMap.put("Anushka", 80);
myMap.put("Amit", 75);
myMap.put("Spandan", 50);
myMap.put("Hari", 55);
myMap.put("Keshav", 60);
System.out.println("Map data without Sort :-");
for (Entry<String, Integer> myEntryMapData : myMap.entrySet())
{
System.out.println("The Map data is Key: " + myEntryMapData.getKey() + " Value: "
+ myEntryMapData.getValue());
}
List<Entry<String, Integer>> myMapDataAsList = new ArrayList<Map.Entry<String, Integer>>();
myMapDataAsList.addAll(myMap.entrySet());
System.out.println("Map data Stored in List, The whole List is : " + myMapDataAsList);
Iterator<Entry<String, Integer>> myListIterator = myMapDataAsList.iterator();
System.out.println("Map data Stored in List, Print through iterator :-");
for (; myListIterator.hasNext();)
{
Entry<String, Integer> myListData = myListIterator.next();
System.out.println("The List data is Key: " + myListData.getKey() + " Value: " + myListData.getValue());
}
Collections.sort(myMapDataAsList, new Comparator<Entry<String, Integer>>()
{
@Override
public int compare(Entry<String, Integer> dataOne, Entry<String, Integer> dataTwo)
{
return dataOne.getKey().compareTo(dataTwo.getKey());
}
});
System.out.println("After Sort by the Key the Map data is : ");
myListIterator = myMapDataAsList.iterator();
for (; myListIterator.hasNext();)
{
Entry<String, Integer> myListData = myListIterator.next();
System.out.println("The List data is Key: " + myListData.getKey() + " Value: " + myListData.getValue());
}
Collections.sort(myMapDataAsList, new Comparator<Entry<String, Integer>>()
{
@Override
public int compare(Entry<String, Integer> dataOne, Entry<String, Integer> dataTwo)
{
return dataOne.getValue().compareTo(dataTwo.getValue());
}
});
System.out.println("After Sort by the vale the Map data is : ");
myListIterator = myMapDataAsList.iterator();
for (; myListIterator.hasNext();)
{
Entry<String, Integer> myListData = myListIterator.next();
System.out.println("The List data is Key: " + myListData.getKey() + " Value: " + myListData.getValue());
}
}
}