java hashmap string<object,String>

java的Hashmap中排序问题_百度知道
java的Hashmap中排序问题
相对通话次数排序,怎么进行啊,通话次序也定义成了StringHashmap不是只能存储String类型数据吗
提问者采纳
但是你存数值的时候,取出来的时候你用String的方式取出来,然后你转化成数值类型的就行了。用Integer.parseInt(res)在hashmap中存什么类型都可以
提问者评价
来自团队:
其他类似问题
为您推荐:
其他3条回答
不一定能第一个取出来Hashmap的key和value都可以存储Object类型的,并且Hashmap是不保证存储顺序的,不仅仅是String,也就是说第一个存进去的值
HashMap里面的对象要实现一个接口 implements Comparable&T& 这样就可以用ComparTo方法进行自定义排序了
谁给你说的Hashmap只能存String类型的?
hashmap的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁java list&map& 排序_百度知道
java list&map& 排序
map22= list,Object&OMap&& maptemp=new HashMap&().get(&
Map& j++) {
Map& list=new ArrayList&
Map&lt, Object& map=new HashMap&lt, Object&Map&();O
int map2value=(Integer) list.get(j+1), Object&number&number&(); list1=new ArrayList&Map&lt.add(map);;;;O map2=new HashMap&
if (map1value&map2value) {
maptemp=map11;Object,Object&
Map&&gt.add(map3), Object&gt,Object&gt.size()-i-1;number&quot.get(j);(); map22;& i++) {
for (int j = 0;
list, 2););O();Object,Object&gt.get(j+1);O
map11=map22.put(&Ofor (int i = 0.size().put(&
map11= list, Object&&Object, Object&O
Map&); i &
map3;Object, Object&number&quot, Object&;Onumber&quot.get(list.add(map2);OObject.get(&
list1, Object&(); j &Map&lt, 1), 4); map3=new HashMap&lt.add(map1).get(j); map11, Object&
Map&lt.put(&number&O map1=new HashMap&lt.add(list.size()-i)).put(&quot, Object&gt, Object&O
int map1value=(Integer)
map2, 3);()public static void main(String[] args) {
哪里错了呢
提问者采纳
&nbsp,&&nbsp,&HashMap&&&&&&nbsp.add(map);&Map&&&&&&public&&ArrayList&&&&static&&&&nbsp.HashM&&nbsp.&Object&o2;&;&Object&&&&&&=&nbsp.add(map2);java.&&&=&number&&&&O&&nbsp.util.put(&new&nbsp.put(&&&&&&&&&&nbsp.Mimport&&(),&return&&nbsp.sort(&&nbsp.CO&nbsp.).put(&&nbsp,&&&nbsp,&&nbsp,&&&3);&&&compare(Map&number&&&&&&&&&map2;&map2&import&&args)&nbsp,&&S&&number&&&&&&&&&&&&;&&map1value&number&O&Object&Onumber&quot.ArrayL=&HashMap&public&nbsp,&&O&map3;&&&map&Map&&&Object&gt.add(map1);&nbsp,&&&import&o2)&nbsp.get(&&main(String[]&nbsp,&&&();public&&&&nbsp.&&map1&&();Map&list&&&&&C&O(Integer)&&&new&List&int&&{&nbsp.add(map3);&&&class&&&O&&&O&&O{&&&nbsp.HashMap&=&map1value&new&&&(Integer)&int&Object&;&O&&&import&nbsp,&$&O&&4);&Object&&&&}&&&&});Object&&&&&nbsp.put(&&import&&nbsp,&&{&&O&&&&nbsp.&nbsp,&Map&&2);int&);&&nbsp,&o1;&&&&&nbsp,&map1;{&&&&map2value&&&=&&()&&&new&&&nbsp,&&&&&nbsp,&Object&&&&&number&Object&&map2value.L&nbsp,&&&void&new&nbsp用我这个排序不是更简单么import&Object&&&();&Object&&&&&Map&Comparator&&1);=&Map&Object&O&nbsp.println(list);;&o1;&&Map&HashMap&&=&&&&&Object&&();&nbsp.get(&&&new&&nbsp,&&&Map&&map3&&-&&&nbsp
您回答的对,现在我想问下用冒泡排序的话,怎么才能写出来呢?谢谢你
不好意思,才看见,你这个不适合用冒泡排序做。实现起来非常麻烦,冒泡的,建议你理解的概念,就可以了。上面代码的30行~40行,用下面的代替&&&&&&&&System.out.println(list);&&&&&&&&for&(int&i&=&0;&i&&&list.size();&i++)&{&&&&&&&&&&&&Map&Object,&Object&&tmp0&=&list.get(i);&&&&&&&&&&&&int&number0&=&(Integer)&tmp0.get(&number&);&&&&&&&&&&&&Map&Object,&Object&&tmp&=&&&&&&&&&&&&&for&(int&j&=&i;&j&&&list.size();&j++)&{&&&&&&&&&&&&&&&&Map&Object,&Object&&tmp1&=&list.get(j);&&&&&&&&&&&&&&&&int&number1&=&(Integer)&tmp1.get(&number&);&&&&&&&&&&&&&&&&if&(number0&&&number1)&{&&&&&&&&&&&&&&&&&&&&tmp&=&tmp0;&&&&&&&&&&&&&&&&&&&&list.set(i,&list.get(j));&&&&&&&&&&&&&&&&&&&&list.set(j,&tmp);&&&&&&&&&&&&&&&&}&&&&&&&&&&&&}&&&&&&&&}&&&&&&&&System.out.println(list);&这个是用冒泡排序实现的,你对比下吧,哪个简单?你出现的问题是:对象间用等号赋值,那么就让这两个对象指向了同一个内存地址,也就是等同的,即,可以理解为对东一个文件创建两个快捷方式,但实际指向的还是同一个文件。利用list里面的set方法来赋值
提问者评价
太厉害了,不知道怎么给你加点分。非常感谢
来自团队:
其他类似问题
为您推荐:
java的相关知识
其他1条回答
带Tree的类多好啊,排序方便啊
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁&p&一堆键值均重复的键值对,去掉值重复的键值对,并记录值被重复的信息, 把值和信息封装成对象。与键再构造键值对。有时会报错。&img src=&/feaf64bd7baaf538e7d5b_b.jpg& data-rawwidth=&1852& data-rawheight=&972& class=&origin_image zh-lightbox-thumb& width=&1852& data-original=&/feaf64bd7baaf538e7d5b_r.jpg&&&br&&/p&&p&public static IdentityHashMap&String, JSONObject& removeRepetitionFromMap(IdentityHashMap&String, JSONObject& map) {&/p&&p&
System.setProperty(&java.util.Arrays.useLegacyMergeSort&, &true&);&/p&&p&
int i = 0 ;&/p&&p&
Set&Entry&String, JSONObject&& set = map.entrySet();&/p&&p&
List&Entry&String, JSONObject&& list = new ArrayList&Entry&String, JSONObject&&(set);&/p&&p&
Collections.sort(list, new Comparator&Entry&String, JSONObject&&() {&/p&&p&
public int compare(Entry&String, JSONObject& entry1, Entry&String, JSONObject& entry2) {&/p&&p&
return Integer.valueOf(entry1.getValue().hashCode()) - Integer.valueOf(entry2.getValue().hashCode());&/p&&p&
for (int index = 0; index & list.size(); index++) {&/p&&p&
String key = list.get(index).getKey();&/p&&p&
JSONObject value = list.get(index).getValue();&/p&&p&
int next_index = index + 1;&/p&&p&
if (next_index & list.size()) {&/p&&p&
String next_key = list.get(next_index).getKey();&/p&&p&
JSONObject next_value = list.get(next_index).getValue();&/p&&p&
if (value.toString().equals(next_value.toString())) {&/p&&p&
Dup = &/p&&p&
if (key.hashCode() & next_key.hashCode()) {&/p&&p&
map.remove(next_key);&/p&&p&
list.remove(next_index);
} else {&/p&&p&
map.remove(key);&/p&&p&
list.remove(index);
index--;&/p&&p&
duplicateRequestArray.add(value);&/p&&p&
} else{&/p&&p&
Dup =&/p&&p&
ArrayList&JSONObject& jsonList = new ArrayList&JSONObject&();&/p&&p&
Iterator&Entry&String, JSONObject&& iterator = map.entrySet().iterator();&/p&&p&
while (iterator.hasNext()) {&/p&&p&
Entry&String, JSONObject& entry = iterator.next();
JSONObject value = entry.getValue();&/p&&p&
jsonList.add(value);&/p&&p&
if(map.size()!=(removeDuplicate(jsonList).size())){
removeRepetitionFromMap(map);&/p&&p&
一堆键值均重复的键值对,去掉值重复的键值对,并记录值被重复的信息, 把值和信息封装成对象。与键再构造键值对。有时会报错。public static IdentityHashMap&String, JSONObject& removeRepetitionFromMap(IdentityHashMap&String, JSONObject& map) {
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
int i = 0 ;
Set&Entry&String, JSONObject&& set = map.entrySet();
List&Entry&String, JSONObject&& list = new ArrayList&Entry&String, JSONObject&&(set);
Collections.sort(list, new Comparator&Entry&String, JSONObject&&() {
public int compare(Entry&String, JSONObject& entry1, Entry&String, JSONObject& entry2) {
你这个犯了初学者常见的错误,边遍历java容器list,map,set边删除的错误。正确的做法是遍历的时候记录下要删除的数据,之后再统一删除。参考下这个代码吧:
已有帐号?
无法登录?
社交帐号登录Visit the Apple Store
locations.
1-800-MY-APPLE
Copyright & 2011 Apple Inc. All rights reserved.Java: how to convert HashMap&String, Object& to array - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I need to convert a HashMap&String, Object& could anyone show me how it's done?
86.6k34250402
14.4k153450
hashMap.keySet().toArray(); // returns an array of keys
hashMap.values().toArray(); // returns an array of values
Should be noted that the ordering of both arrays may not be the same,
See oxbow_lakes answer for a better approach for iteration when the pair key/values are needed.
18.7k3278109
If you want the keys and values, you can always do this via the entrySet:
hashMap.entrySet().toArray(); // returns a Map.Entry&K,V&[]
From each entry you can (of course) get both the key and value via the getKey and getValue methods
86.6k34250402
If you have HashMap&String, SomeObject& hashMap then
hashMap.keySet().toArray();
will return an Object[]. If instead you want an array of the type SomeObject, you could use:
hashMap.keySet().toArray(new SomeObject[0]);
To guarantee the correct order for each array of Keys and Values, use this (the other answers use individual Sets which offer no guarantee as to order.
Map&String, Object& map = new HashMap&String, Object&();
String[] keys = new String[map.size()];
Object[] values = new Object[map.size()];
int index = 0;
for (Map.Entry&String, Object& mapEntry : map.entrySet()) {
keys[index] = mapEntry.getKey();
values[index] = mapEntry.getValue();
2,89911538
An alternative to CrackerJacks suggestion, if you want the HashMap to maintain order you could consider using a LinkedHashMap instead. As far as im aware it's functionality is identical to a HashMap but it is FIFO so it maintains the order in which items were added.
2,40112030
Map&String, String& map = new HashMap&String, String&();
map.put("key1", "value1");
map.put("key2", "value2");
Object[][] twoDarray = new String[map.size()][2];
Object[] keys = map.keySet().toArray();
Object[] values = map.values().toArray();
for (int row = 0; row & twoDarray. row++) {
twoDarray[row][0] = keys[row];
twoDarray[row][1] = values[row];
for (int i = 0; i & twoDarray. i++) {
for (int j = 0; j & twoDarray[i]. j++) {
System.out.println(twoDarray[i][j]);
77.6k16148248
To Get in One Dimension Array.
String[] arr1 = new String[hashmap.size()];
String[] arr2 = new String[hashmap.size()];
Set entries = hashmap.entrySet();
Iterator entriesIterator = entries.iterator();
int i = 0;
while(entriesIterator.hasNext()){
Map.Entry mapping = (Map.Entry) entriesIterator.next();
arr1[i] = mapping.getKey().toString();
arr2[i] = mapping.getValue().toString();
To Get in two Dimension Array.
String[][] arr = new String[hashmap.size()][2];
Set entries = hashmap.entrySet();
Iterator entriesIterator = entries.iterator();
int i = 0;
while(entriesIterator.hasNext()){
Map.Entry mapping = (Map.Entry) entriesIterator.next();
arr[i][0] = mapping.getKey().toString();
arr[i][1] = mapping.getValue().toString();
You may try this too.
public static String[][] getArrayFromHash(Hashtable&String,String& data){
String[][] str =
Object[] keys = data.keySet().toArray();
Object[] values = data.values().toArray();
str = new String[keys.length][values.length];
for(int i=0;i&keys.i++) {
str[0][i] = (String)keys[i];
str[1][i] = (String)values[i];
Here I am using String as return type. You may change it to required return type by you.
I used almost the same as @kmccoy, but instead of a keySet() I did this
hashMap.values().toArray(new MyObject[0]);
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 string转hashmap 的文章

 

随机推荐