java - 相同的代码为 Java 7 和 8 产生不同的输出

标签 java sorting hashmap comparator

将输入指定为 91912323 时,对于 Java 8,输出为 33221199,而在 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);
    }
}

关于java - 相同的代码为 Java 7 和 8 产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479349/

相关文章:

java - 检测台球 table 上的球(条纹和实心)

C++:按字母顺序将节点添加到双向链表

c++ - 对于非常小的表(通常<10个项目)的高性能表结构,一旦创建表就不会改变?

java - HashMap使用对象实现一键多值

java - 在数值数组和 SimpleITK Image 对象之间传输

java - ListArray 保存相同的记录

java - 排序复杂度

c# - 在 C# 中反向 Fisher-Yates Shuffle

c++ - 为什么LLVM选择开放寻址哈希表来实现llvm::StringMap?

java - 如何在 Swing 中创建单独的屏幕?