首先遍历List,然后将遍历的元素放入到HashMap类型中的map中,然后再讲map中的内容放入到list中去。
代码如下:
//KeyValue类
public class KeyValue <K,V> implements Serializable{//<K,V>类型参数
private K key;
private V value;
public KeyValue(){}
public KeyValue(K key,V value){
this.key = key;
this.value = value;
}
@Override
public String toString(){
return "[KEY]:"+key+"[VALUE]:"+value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V v) {
this.value = v;
}
}
//ArrayList处理方法
public static List<KeyValue<String,Integer>> handlerReList(List<KeyValue<String,Integer>> list){
Map<String,Integer> map = new HashMap<String,Integer>();
List <KeyValue<String,Integer>>returnList = new ArrayList<KeyValue<String,Integer>>();
for(KeyValue<String,Integer> key:list){
map.put(key.getKey(),key.getValue());
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for(Map.Entry<String, Integer> me:entrySet){
returnList.add(new KeyValue(me.getKey(),me.getValue()));
}
return returnList;
}