在java中从映射列表中删除值
从包含映射的java中的列表中删除值 我有以下表格中的列表:在java中从映射列表中删除值,java,list,map,Java,List,Map,从包含映射的java中的列表中删除值 我有以下表格中的列表: List: [{id=1,c_id=3,value=5},{id=1,c_id=2,value=5},{id=1,c_id=3,value=5},{id=2,c_id=1,value=5}, {id=2,c_id=null,value=5}}]; 结果,我试图得到 List: [{id=1,c_id=3,value=5},,{id=1,c_id=2,value=5},{id=2,c_id=1,value
List: [{id=1,c_id=3,value=5},{id=1,c_id=2,value=5},{id=1,c_id=3,value=5},{id=2,c_id=1,value=5},
{id=2,c_id=null,value=5}}];
结果,我试图得到
List: [{id=1,c_id=3,value=5},,{id=1,c_id=2,value=5},{id=2,c_id=1,value=5}}]
对于相同的id
如果c\u id
相同,则应将其删除,如果c\u id
为null
则应将其删除。您可以这样做
for(Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext(); ) {
Map<String, Object> map = iter.next();
Object c_id = map.get("c_id");
if (c_id == null || c_id.equals(matching_c_id))
iter.remove();
}
for(迭代器iter=list.Iterator();iter.hasNext();){
Map=iter.next();
对象c_id=map.get(“c_id”);
如果(c|u id==null | c|u id.equals(匹配的c|u id))
iter.remove();
}
您可以这样做
for(Iterator<Map<String, Object>> iter = list.iterator(); iter.hasNext(); ) {
Map<String, Object> map = iter.next();
Object c_id = map.get("c_id");
if (c_id == null || c_id.equals(matching_c_id))
iter.remove();
}
for(迭代器iter=list.Iterator();iter.hasNext();){
Map=iter.next();
对象c_id=map.get(“c_id”);
如果(c|u id==null | c|u id.equals(匹配的c|u id))
iter.remove();
}
我认为解决方案可以是以下解决方案,而且我认为它是可读的
首先,为对象类型构造一个集合元素代理:
public class MyTypeProxy {
private MyType obj;
public MyTypeProxy(MyType obj) {
this.obj = obj;
}
public MyType getObj() { return this.obj; }
public int hashcode() {
return obj.getC_id() ^ obj.getId();
}
public boolean equals(Object ref) {
if (ref.getClass() != MyTypeProxy.class) {
return false;
}
return
((MyTypeProxy)ref).obj.getC_id() == this.obj.getC_id() &&
((MyTypeProxy)ref).obj.getId() == this.obj.getC_id();
}
}
然后,在代码中,您可以构造一个哈希集
HashSet<MyTypeProxy> set = new HashSet<MyTypeProxy>();
for (MyType mt : list) {
if (mt.getC_id() != null){
set.add(new MyTypeProxy(myList));
}
}
HashSet=newhashset();
对于(MyType mt:list){
如果(mt.getC_id()!=null){
添加(新的MyTypeProxy(myList));
}
}
下面做两件事。首先,它排除具有空c_id字段的对象,其次,它过滤出具有相同id和c_id组合的对象。您可能想考虑是否保留第一个OK(这是上面代码所做的)。
该集合现在正好包含您需要的内容。我认为解决方案可能是以下解决方案,而且我认为它是可读的 首先,为对象类型构造一个集合元素代理:
public class MyTypeProxy {
private MyType obj;
public MyTypeProxy(MyType obj) {
this.obj = obj;
}
public MyType getObj() { return this.obj; }
public int hashcode() {
return obj.getC_id() ^ obj.getId();
}
public boolean equals(Object ref) {
if (ref.getClass() != MyTypeProxy.class) {
return false;
}
return
((MyTypeProxy)ref).obj.getC_id() == this.obj.getC_id() &&
((MyTypeProxy)ref).obj.getId() == this.obj.getC_id();
}
}
然后,在代码中,您可以构造一个哈希集
HashSet<MyTypeProxy> set = new HashSet<MyTypeProxy>();
for (MyType mt : list) {
if (mt.getC_id() != null){
set.add(new MyTypeProxy(myList));
}
}
HashSet=newhashset();
对于(MyType mt:list){
如果(mt.getC_id()!=null){
添加(新的MyTypeProxy(myList));
}
}
下面做两件事。首先,它排除具有空c_id字段的对象,其次,它过滤出具有相同id和c_id组合的对象。您可能想考虑是否保留第一个OK(这是上面代码所做的)。
现在,该集合正好包含您需要的内容。:
Iterables.removeIf(列表,新谓词(){
@覆盖公共布尔应用(元素e){
返回e.c_id==null | | e.c_id.equals(匹配的__id);
}});
:
Iterables.removeIf(列表,新谓词(){
@覆盖公共布尔应用(元素e){
返回e.c_id==null | | e.c_id.equals(匹配的__id);
}});
它是调试器的输出。我假设您不想要,
为什么它删除第二个c\u id=3
而不是第一个?它是调试器的输出。我假设您不想要,
为什么它删除第二个c\u id=3
而不是第一个?您还可以添加Java 8版本您还可以添加Java8版本(如上所述)。