求大神会java的hashmap的问题:如何删掉Map中重复的值?
如:map.put("1","abc");map.put("2","abc");map.put("3","kjs");map.put("4","abc");map("5"...
如:map.put("1","abc");map.put("2","abc");map.put("3","kjs");map.put("4","abc");map("5","kjs");
运行后2,4,5对应的map都会从list中删掉。
由于不太懂java,求详细代码,会加分 展开
运行后2,4,5对应的map都会从list中删掉。
由于不太懂java,求详细代码,会加分 展开
展开全部
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.collections4.map.LinkedMap;
public class Test030 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> m = new LinkedMap<String, String>();
m.put("1", "abc");
m.put("2", "abc");
m.put("3", "kjs");
m.put("4", "abc");
m.put("5", "kjs");
System.out.println("before: " + m);
removeDuplicate(m);
System.out.println("after: " + m);
}
private static void removeDuplicate(Map<String, String> m) {
Set<String> values = new HashSet<String>();
for (Iterator<Entry<String, String>> it = m.entrySet().iterator(); it
.hasNext();) {
Entry<String, String> e = it.next();
if(values.contains(e.getValue())){
it.remove();
}else{
values.add(e.getValue());
}
}
}
}
写的时候没有注意, 如果jdk中没有linkedMap, 那么请引入commons的collections包
主要是HashMap不保证顺序
追问
非常感谢你的回答,不过我水平不够,你的这个代码我没用得起来。
展开全部
package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.eclipse.jdt.internal.compiler.ast.DoStatement;
public class A {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1","abc");
map.put("2","abc");
map.put("3","kjs");
map.put("4","abc");
map.put("5","kjs");
Map<String, String> map4 = new HashMap<String, String>();
map4 = doThing(map);
for (String key : map4.keySet()){
System.out.println(key+":"+map4.get(key));
}
}
public static Map<String, String> doThing(Map<String, String> map){
Map<String, String> map2 = new HashMap<String, String>();
Map<String, String> map3 = new HashMap<String, String>();
//TreeMap:对map按key值排序
TreeMap<String, String> treemap = new TreeMap<String, String>(map);
Iterator<String> it = treemap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = treemap.get(key);
if(map2.containsKey(value)){
continue;
}else{
map2.put(value, value);
map3.put(key, value);
}
}
return map3;
}
}
输出:
3:kjs
1:abc
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
HashMap
继承于AbstractMap,实现了Map、Cloneable、java.io.Serializable接口。
HashMap
的实现不是同步的,这意味着它不是线程安全的。它的key、value都可以为null。此外,HashMap中的映射不是有序的。它存储的值是无序不可重复的!
HashMap
继承于AbstractMap,实现了Map、Cloneable、java.io.Serializable接口。
HashMap
的实现不是同步的,这意味着它不是线程安全的。它的key、value都可以为null。此外,HashMap中的映射不是有序的。它存储的值是无序不可重复的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你个例子看,就知道了
package shuai.study.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* @author shengshu
*
*/
public class UniqueMap {
// Remove repetition from Map, this is core part in this Class
public static Map<String, String> removeRepetitionFromMap(Map<String, String> map) {
Set<Entry<String, String>> set = map.entrySet();
List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);
Collections.sort(list, new Comparator<Entry<String, String>>() {
@Override
public int compare(Entry<String, String> entry1, Entry<String, String> entry2) {
return Integer.valueOf(entry1.getValue().hashCode()) - Integer.valueOf(entry2.getValue().hashCode());
}
});
// list.size() is dynamic change
for (int index = 0; index < list.size(); index++) {
String key = list.get(index).getKey();
String value = list.get(index).getValue();
int next_index = index + 1;
if (next_index < list.size()) {
String next_key = list.get(next_index).getKey();
String next_value = list.get(next_index).getValue();
// Remove repetition record whose key is more bigger
if (value == next_value) {
if (key.hashCode() < next_key.hashCode()) {
map.remove(next_key);
list.remove(next_index);
} else {
map.remove(key);
list.remove(index);
}
// Due to having repetition in List, so index will be reduced
index--;
}
}
}
return map;
}
// Transfer Map to Sorted Map
public static Map<String, String> transferToSortedMap(Map<String, String> map) {
// Define comparator for TreeMap
Map<String, String> new_sort_map = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String key1, String key2) {
return key1.hashCode() - key2.hashCode();
}
});
new_sort_map.putAll(map);
return new_sort_map;
}
public static void printMap(Map<String, String> map) {
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " --> " + value);
}
}
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("A", "1");
map.put("B", "2");
map.put("C", "2");
map.put("D", "3");
map.put("E", "3");
Map<String, String> new_map = UniqueMap.removeRepetitionFromMap(map);
// new_sort_map is what we want
Map<String, String> new_sort_map = UniqueMap.transferToSortedMap(new_map);
// Print new_sort_map
UniqueMap.printMap(new_sort_map);
}
}
package shuai.study.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* @author shengshu
*
*/
public class UniqueMap {
// Remove repetition from Map, this is core part in this Class
public static Map<String, String> removeRepetitionFromMap(Map<String, String> map) {
Set<Entry<String, String>> set = map.entrySet();
List<Entry<String, String>> list = new ArrayList<Entry<String, String>>(set);
Collections.sort(list, new Comparator<Entry<String, String>>() {
@Override
public int compare(Entry<String, String> entry1, Entry<String, String> entry2) {
return Integer.valueOf(entry1.getValue().hashCode()) - Integer.valueOf(entry2.getValue().hashCode());
}
});
// list.size() is dynamic change
for (int index = 0; index < list.size(); index++) {
String key = list.get(index).getKey();
String value = list.get(index).getValue();
int next_index = index + 1;
if (next_index < list.size()) {
String next_key = list.get(next_index).getKey();
String next_value = list.get(next_index).getValue();
// Remove repetition record whose key is more bigger
if (value == next_value) {
if (key.hashCode() < next_key.hashCode()) {
map.remove(next_key);
list.remove(next_index);
} else {
map.remove(key);
list.remove(index);
}
// Due to having repetition in List, so index will be reduced
index--;
}
}
}
return map;
}
// Transfer Map to Sorted Map
public static Map<String, String> transferToSortedMap(Map<String, String> map) {
// Define comparator for TreeMap
Map<String, String> new_sort_map = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String key1, String key2) {
return key1.hashCode() - key2.hashCode();
}
});
new_sort_map.putAll(map);
return new_sort_map;
}
public static void printMap(Map<String, String> map) {
Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " --> " + value);
}
}
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("A", "1");
map.put("B", "2");
map.put("C", "2");
map.put("D", "3");
map.put("E", "3");
Map<String, String> new_map = UniqueMap.removeRepetitionFromMap(map);
// new_sort_map is what we want
Map<String, String> new_sort_map = UniqueMap.transferToSortedMap(new_map);
// Print new_sort_map
UniqueMap.printMap(new_sort_map);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把键值对调
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |