假设我有个List 里面放的是javabean 数据如下:物理 95
数学 100
物理 100以为物理 100和前面的物理95重复了 我想把他给去掉,请问大家有什么方法去吗?
数学 100
物理 100以为物理 100和前面的物理95重复了 我想把他给去掉,请问大家有什么方法去吗?
解决方案 »
- 请问这个程序应该保存那个类
- java数组问题
- 强烈推荐:[原创]不是我的错——为JBuilder的光标错位问题平反!
- jb8中关于UI的非常简单的问题
- 求远程打印程序!!!急!
- superclass supera =new superclass(),superb;这是什么意思superclass是父类superrb是子类
- unicode 编码的byte[]数组转换成中文STRING
- 怎样打包我的applet class
- split()方法使用的疑问,关于分隔符。
- 我是JAVA新手,我安装了JDK1.7系统变量也配置好了,但是为什么我在运行窗口里输入javac却显示'javac'不是内部或外部命令
- java画图(Graphics)的问题
- 去掉字符串中的某个字符
javaBean里面定义,equals方法,
return this.分数=other.分数
可以放在map里,key保证是唯一的
两个javabean对象equals返回true,就认为这两个对象重复了。
当然,还要重写hashcode()方法
map是直接覆盖第一个,也就是保存的是第二个
看楼主的意思是要删除第二个,那么应该用set
遍历arraylist做判断哪些该删掉
往里存值时,map.put("物理",95);map.put("数学",100);这时map.put("物理",100);就插不进去了,三楼的方法也很好,都可以的!
代码如下:
//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;
}
如果想保留95
if(!map.containsKey("物理")){
map.put("key",value);
}
如果想保留100
直接put