将hashmap的键值存储在字符串[]中 [英] store key values of hashmap in string[]

查看:137
本文介绍了将hashmap的键值存储在字符串[]中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到了解决方案..使用linkedHashMap并能够将排序后的键值存储在数组中。 此处是代码的链接。


可能存在重复:


我创建了一个hashmap,然后使用这个代码
代码工作,我能够在屏幕上显示按值排序的键值对。

现在我想存储键(从排序的元素)在 String [] 中。 HashMap.keySet()。toArray()没有帮助,因为退役的HashMap没有排序。



Can (b,23)(c,7)(b,b) )
通过使用上面的链接,我能够得到(a,1)(c,7)(b,23)作为输出。我想将{a,c,b}存储为一个字符串数组。
我希望这个问题现在已经很清楚了。

解决方案

重复使用此代码,您只需使用

 列表键= sortByValue(m); 
String [] keyArray = keys.toArray(new String [keys.size()]);

使用泛型会使这个更好。我还会根据Map.Entry(from entrySet())进行排序,这会更快更简单。


Got the solution.. Used linkedHashMap and was able to store the sorted key values in an array. Here's the link for the code.

Possible Duplicate:
store key values of sorted hashmap in string[]

I created a hashmap and then 'sorted by value' the elements in the map using this code. The code worked and i was able to display on screen the key value pairs sorted by value.

Now I want to store the keys(from the sorted elements) in a String[]. HashMap.keySet().toArray() doesn't help as the entired HashMap are not sorted.

Can anyone please suggest a method to do this?

further clarifying the situation - Suppose I have key value pairs like (a,1)(b,23)(c,7) by using the above link, i was able to get (a,1)(c,7)(b,23) as output. I want to store {a,c,b} as a string array. I hope the problem is clear now.

解决方案

Reusing this code you would just use

List keys = sortByValue(m);
String[] keyArray = keys.toArray(new String[keys.size()]);

Using generics would make this nicer. I would also sort by Map.Entry (from entrySet()) which would be faster and simpler.

这篇关于将hashmap的键值存储在字符串[]中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆