同一代码为 Java 7 和 8 生成不同的输出
Same Code is producing different outputs for Java 7 & 8
在输入为 91912323 时,输出为 33221199 for Java 8 while in Java 7输出是 11223399.
问题陈述也提供给大家参考。
如有任何帮助,我们将不胜感激。
A string consists of digits from 1-9 will be passed as input. The
program must print the digits sorted based on the number of
occurrence. If one or more digits occur the same number of times, the
smallest digit must be printed first.
Input Format: The first line will contain the N digits from 1-9
Boundary Conditions: 3 <= N <= 30
Output Format: The digits sorted based on the number of occurrence.
Example Input/Output 1:
Input: 4443338993
Output: 3333444998
Explanation: 3 occurs the most number of times (four times). Hence it
is printed first. 4 occurs thrice and hence printed after the 3s. 9
occurs twice and hence printed after the 4s. 8 occurs only once and
hence printed after 9.
Example Input/Output 2:
Input: 95559998228
Output: 99995552288
Explanation: Here 2 and 8 occurs twice. Hence 2 being the smaller
digit is printed before 8.
package E001;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author Anagh
*/
public class CharOccurrences {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
char[] arr = input.toCharArray();
HashMap<String, Integer> map = new HashMap<>();
for(int i = 0; i < arr.length; i++)
{
if(!map.containsKey(String.valueOf(arr[i])))
{
map.put(String.valueOf(arr[i]), 1);
}
else
{
map.put(String.valueOf(arr[i]), map.get(String.valueOf(arr[i]))+1);
}
}
TreeMap<String, Integer> output = sortByValue(map);
printMap(output);
}
public static TreeMap<String, Integer> sortByValue (HashMap<String, Integer> map)
{
ValueComparator vc = new ValueComparator(map);
TreeMap<String,Integer> sortedMap = new TreeMap<>(vc);
sortedMap.putAll(map);
return sortedMap;
}
private static void printMap(TreeMap<String, Integer> map) {
String key;
int value;
for (Map.Entry<String, Integer> entry : map.entrySet())
{
key = entry.getKey();
value = entry.getValue();
for(int j = 0; j < value; j++)
{
System.out.print(key);
}
}
}
}
class ValueComparator implements Comparator<String> {
Map<String, Integer> map;
public ValueComparator(Map<String, Integer> base) {
this.map = base;
}
@Override
public int compare(String a, String b) {
if (map.get(a) > map.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
你的比较器违反了它的合同,你应该添加一个案例,当出现的确切数量时,它应该如何排序,例如,首先是较小的数字。
Output: 99995552288
Explanation: Here 2 and 8 occurs twice. Hence 2 being the smaller digit is printed before 8.
您的比较器未实现该逻辑。为了让它实现该逻辑,它应该是:
public int compare(String a, String b) {
if (map.get(a) > map.get(b)) {
return -1;
} else if (map.get(a) < map.get(b)) {
return 1;
} else {
return a.compareTo(b);
}
}
在输入为 91912323 时,输出为 33221199 for Java 8 while in Java 7输出是 11223399.
问题陈述也提供给大家参考。
如有任何帮助,我们将不胜感激。
A string consists of digits from 1-9 will be passed as input. The program must print the digits sorted based on the number of occurrence. If one or more digits occur the same number of times, the smallest digit must be printed first.
Input Format: The first line will contain the N digits from 1-9
Boundary Conditions: 3 <= N <= 30
Output Format: The digits sorted based on the number of occurrence.
Example Input/Output 1:
Input: 4443338993
Output: 3333444998
Explanation: 3 occurs the most number of times (four times). Hence it is printed first. 4 occurs thrice and hence printed after the 3s. 9 occurs twice and hence printed after the 4s. 8 occurs only once and hence printed after 9.
Example Input/Output 2:
Input: 95559998228
Output: 99995552288
Explanation: Here 2 and 8 occurs twice. Hence 2 being the smaller digit is printed before 8.
package E001;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
*
* @author Anagh
*/
public class CharOccurrences {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
char[] arr = input.toCharArray();
HashMap<String, Integer> map = new HashMap<>();
for(int i = 0; i < arr.length; i++)
{
if(!map.containsKey(String.valueOf(arr[i])))
{
map.put(String.valueOf(arr[i]), 1);
}
else
{
map.put(String.valueOf(arr[i]), map.get(String.valueOf(arr[i]))+1);
}
}
TreeMap<String, Integer> output = sortByValue(map);
printMap(output);
}
public static TreeMap<String, Integer> sortByValue (HashMap<String, Integer> map)
{
ValueComparator vc = new ValueComparator(map);
TreeMap<String,Integer> sortedMap = new TreeMap<>(vc);
sortedMap.putAll(map);
return sortedMap;
}
private static void printMap(TreeMap<String, Integer> map) {
String key;
int value;
for (Map.Entry<String, Integer> entry : map.entrySet())
{
key = entry.getKey();
value = entry.getValue();
for(int j = 0; j < value; j++)
{
System.out.print(key);
}
}
}
}
class ValueComparator implements Comparator<String> {
Map<String, Integer> map;
public ValueComparator(Map<String, Integer> base) {
this.map = base;
}
@Override
public int compare(String a, String b) {
if (map.get(a) > map.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}
你的比较器违反了它的合同,你应该添加一个案例,当出现的确切数量时,它应该如何排序,例如,首先是较小的数字。
Output: 99995552288
Explanation: Here 2 and 8 occurs twice. Hence 2 being the smaller digit is printed before 8.
您的比较器未实现该逻辑。为了让它实现该逻辑,它应该是:
public int compare(String a, String b) {
if (map.get(a) > map.get(b)) {
return -1;
} else if (map.get(a) < map.get(b)) {
return 1;
} else {
return a.compareTo(b);
}
}