我有一个关于地图的问题,来自Integer,Integer类型。我通过在<Integer,Integer>
上解决基于<String,String>
地图的问题而犯了一个错误,最终结果是正确的,但是数字的顺序并没有下降。如果我选择Integer
而不是String
来解决这个问题。
如何从用户那里获取输入并在Map<Integer, Integer>
时将其分配给地图的键/值?我使用substring()
和indexOf()
来制作我的旧地图,但对于整数我无法找到方法
import java.util.*;
import java.io.*;
import type.lib.*;
public class MapTesting {
public static void main(String[] args) {
PrintStream output = new PrintStream(System.out);
Scanner input = new Scanner(System.in);
Map<String,String> normal = new HashMap<String,String>();
output.println("Enter your map, one key-value pair per line,");
output.println("comma-separated. Use empty line as sentinel.");
for ( String entry = input.nextLine(); entry.length() !=0;
entry = input.nextLine()) {
int comma = entry.indexOf(",");
String key = entry.substring(0,comma);
String value = entry.substring(comma+1);
normal.put(key,value);
}
output.println("The input map is:");
output.println(normal);
Map<String,String> reverse = new HashMap<String,String>();
boolean distinct = true;
for (Map.Entry<String,String> entry : normal.entrySet()) {
String keyY = entry.getKey();
String valueE = entry.getValue();
String existingReverseValue = reverse.get(valueE);
if (existingReverseValue != null) {
int currentValue = Integer.parseInt(existingReverseValue);
int potentialNewValue = Integer.parseInt(keyY);
if ( potentialNewValue < currentValue) {
reverse.put(valueE,keyY);
}
} else {
reverse.put(valueE,keyY);
}
}
output.println("The inverted map [using the smaller key as a tie breaker]:");
output.println(reverse);
}
}
答案 0 :(得分:1)
您要采取两个步骤:
Map<String, String>
到Map<Integer, Integer>
,您需要在之前将输入转换为整数
把它放在你的地图上。entrySet()
返回按键降序排序的条目集的方法。这将
还涉及Comparator接口的自定义实现。你可以这样做。
import java.util.*;
import java.io.*;
public class MapTesting {
public static void main(String[] args) {
PrintStream output = new PrintStream(System.out);
Scanner input = new Scanner(System.in);
Map<Integer, Integer> normal = new LinkedHashMap<Integer, Integer>();
output.println("Enter your map, one key-value pair per line,");
output.println("comma-separated. Use empty line as sentinel.");
for ( String entry = input.nextLine(); entry.length() !=0;
entry = input.nextLine()) {
int comma = entry.indexOf(",");
Integer key = Integer.parseInt(entry.substring(0,comma));
Integer value = Integer.parseInt(entry.substring(comma+1));
normal.put(key,value);
}
output.println("The input map is:");
output.println(normal);
// Here we make our reverse map an instance of our custom DescendingKeysMap.
Map<Integer, Integer> reverse = new DescendingKeysMap();
for (Map.Entry<Integer, Integer> entry : normal.entrySet()) {
Integer keyY = entry.getKey();
Integer valueE = entry.getValue();
Integer existingReverseValue = reverse.get(valueE);
if (existingReverseValue != null) {
int currentValue = existingReverseValue;
int potentialNewValue = keyY;
if ( potentialNewValue < currentValue) {
reverse.put(valueE,keyY);
}
} else {
reverse.put(valueE,keyY);
}
}
output.println("The inverted map [using the smaller key as a tie breaker]:");
output.println(reverse);
}
/**
* Extends HashMap<Integer, Integer> and Overrides entrySet() to return the entries sorted by keys descending.
*/
public static class DescendingKeysMap extends HashMap<Integer, Integer>{
@Override
public Set<Map.Entry<Integer, Integer>> entrySet() {
List<Map.Entry<Integer, Integer>> entries = new ArrayList<Map.Entry<Integer, Integer>>(super.entrySet());
// Sort
Collections.sort(entries, new ByKeysDescending());
return new LinkedHashSet<Map.Entry<Integer,Integer>>(entries);
}
}
/**
* Implements Comparator<Map.Entry<Integer, Integer>> to compare by the keys descending.
*/
public static class ByKeysDescending implements Comparator<Map.Entry<Integer, Integer>> {
@Override
public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) {
// return the opposite of comparing the keys, to get a descending order
return e1.getKey().compareTo(e2.getKey()) * -1;
}
}
}