尝试从返回 null 的 hashmap 值数组中创建 hashmap 键数组

Trying to make an array of hashmap keys from an array of hashmap values returning null

我创建了一个方法,它将哈希图作为输入,将值转换为数组,使用冒泡排序对数组进行排序,然后我希望它使用排序后的值到 select 第一个 N来自初始哈希映射的索引键。我知道这不是执行此操作的最佳方法,但没关系(我已将此方法作为替代方法编写)。

方法 returns 为空,因为 maxItems 为空且“System.out.println(entry.getKey());”不向控制台打印任何内容。

感谢您的帮助!

public String[] getMaxListAlt(int n, HashMap itemCount) {

    itemCount.values().toArray();

    Integer[] maxArray = itemCount.values().toArray(new Integer[0]);

    int length = maxArray.length;
    for (int i = 0; i < length-1; i++)
        for (int j = 0; j < length-i-1; j++)
            if (maxArray[j] > maxArray[j+1])
            {
                int temp = maxArray[j];
                maxArray[j] = maxArray[j+1];
                maxArray[j+1] = temp;
                
            }

    String[] maxItems = new String[n];
    int maxIndex = n-1;
    System.out.println(maxIndex);


    for (int i=0; i >= n-1; i++) {
        for (Map.Entry<String, Integer> entry : itemCount.entrySet()) {
            if (entry.getValue().equals(maxArray[i])) {
                System.out.println(entry.getKey());
                maxItems[i] = entry.getKey();
            }
        }
    }

        for (int counts : maxArray) {

        System.out.println(counts);

    }

    return maxItems;
}

你的 for-loop 没有 运行 因为你打错了 - int i=0; i>= n-1; 但你的 i 永远不会大于你的 [=14] =].相反:

for (int i=0; i <= n-1; i++) {
    for (Map.Entry<String, Integer> entry : itemCount.entrySet()) {

如果我理解您正在尝试做什么,那么您可能会对另一种方法感兴趣。但是,您似乎做错了几件事。

  1. 您的冒泡排序按升序排序,我认为您不需要。
  2. 当循环遍历 entrySet 时,您会得到重复的值,因为您总是会找到第一个具有特定数字的值。但这可能是你已经拥有的东西的重复。

下面的流式处理是这样的。

  • 首先它流式传输字符串数组
  • 然后使用 groupingBy
  • 创建基于映射的频率计数
  • 然后它流式传输 entryset 并根据值(计数)按降序排序
  • 然后它将条目数限制为n并将键映射到流
  • 和return数组中的那些键。

创建一些测试数据

Random r = new Random();
String[] v = r.ints(100, 'a', 'z' + 1)
        .mapToObj(c -> (char) c + "").toArray(String[]::new);

int n = 10;

流式处理

Map<String, Long> itemCount = Arrays.stream(v)
        .collect(Collectors.groupingBy(a -> a, Collectors.counting()));
        
String[] result = itemCount.entrySet()
        .stream()
        .sorted(Entry.<String,Long>comparingByValue().reversed())
        .limit(n).map(Entry::getKey).toArray(String[]::new);

for (int i = 0; i < result.length; i++) {
   System.out.println(result[i] + " --> " + itemCount.get(result[i]));
}

打印类似

的内容
h --> 7
q --> 7
t --> 7
f --> 6
a --> 5
e --> 5
m --> 5
x --> 5
z --> 5
d --> 4

请注意,如果需要,第一个流可以通过流式传输 entrySet() 直接流入第二个流。在那种情况下,您 return 数组而永远看不到实际的地图。

如果你想保留原来的地图

如果您想使用您原来的地图和方法,这里有一个带有注释的修改。

public static String[] getMaxListAlt(int n,
        Map<String, Long> itemCount) {
    
    
    // copy entries to a list for sorting.
    List<Entry<String,Long>> entries = new ArrayList<>(itemCount.entrySet());
    
    // sort the entries based on value in  reverse order.
    entries.sort(Entry.<String,Long>comparingByValue().reversed());
    
    // now create the array and just grab the top n items from the sorted
    // entries list.
    String[] maxItems = new String[n];
    for (int i = 0; i < n; i++) {
        maxItems[i] = entries.get(i).getKey();
    }
    
    return maxItems;
}