如何迭代地图列表并找到字段的最大值并删除其他字段

如何迭代地图列表并找到字段的最大值

我有一个列表,其中有一张地图

listGrid={{id=5,name="person",no=9},{id=6,name="person",no=19},{id=6,name="jam",no=10}}

我想要结果div,因为它应该删除id的副本并且具有最多没有字段的副本

  List listGrid=new ArrayList(); Map resultMap=new HashMap(); resultMap.put("id", "5"); resultMap.put("name", "one"); resultMap.put("no", 1); listGrid.add(resultMap); resultMap.put("id", "5"); resultMap.put("name", "one"); resultMap.put("no", 11); listGrid.add(resultMap); resultMap.put("id", "1"); resultMap.put("name", "one"); resultMap.put("no", 5); List list=new ArrayList(); Map mapList=new HashMap(); ListIterator litr = listGrid.listIterator(); while(litr.hasNext()) { Map element = (Map)litr.next(); String id= (String) element.get("id"); Integer damagesNo = (Integer) element.get("no"); if(mapList.containsKey(id)){ Integer mapCode = (Integer) mapList.get(id); int damagesNoInt = damagesNo; int mapCodeInt = mapCode; if(damagesNoInt <= mapCodeInt){ int i=litr.nextIndex()-1; list.add(i); } else{ mapList.put(id, damagesNo); }} else{ mapList.put(id, damagesNo); } } System.out.println(listGrid); 

我试过这个,但它没有正常工作。

根据我的说法,您应该为您的id,no,名称值创建DTO类对象,并对它们进行处理,这将是一种正确的方法

试试这个代码

 public static void main(String[] args) throws IOException { List listGrid = new ArrayList(); Map resultMap = new HashMap(); resultMap.put("id", "5"); resultMap.put("name", "person"); resultMap.put("no", 9); listGrid.add(resultMap); resultMap = new HashMap(); resultMap.put("id", "6"); resultMap.put("name", "person"); resultMap.put("no", 19); listGrid.add(resultMap); resultMap = new HashMap(); resultMap.put("id", "6"); resultMap.put("name", "jam"); resultMap.put("no", 21); listGrid.add(resultMap); resultMap = new HashMap(); resultMap.put("id", "6"); resultMap.put("name", "jam"); resultMap.put("no", 21); listGrid.add(resultMap); List destListGrid = new ArrayList(); HashMap destMap = new HashMap(); ListIterator litr = listGrid.listIterator(); while (litr.hasNext()) { HashMap element = (HashMap) litr .next(); String id = (String) element.get("id"); Integer damagesNo = (Integer) element.get("no"); if (destMap.containsKey(id)) { Integer mapCode = (Integer) destMap.get(id); if (mapCode <= damagesNo) { destMap.remove(id); destMap.put(id, damagesNo); } } else { destMap.put(id, damagesNo); } } for (Map map : listGrid) { if (destMap.containsKey(map.get("id")) && destMap.get(map.get("id")) == map.get("no")) { destMap.remove(map.get("id")); destListGrid.add(map); } } System.out.println(destListGrid); } 

只有当它适合你时,我才建议另一种方式。 此解决方案基于创建具有hashCodeequals方法的专用类,允许标准数据结构对您起作用。 您需要使用hashCode返回实体的id值,并使用equals方法来比较id的相等性,并且不比较grater的值,而只允许具有更大值的实体替换前者。 查看代码plz:

 public class MapList { static class Entity { int id; String name; int no; public int hashCode() { return id; } public boolean equals(Object o) { if (o instanceof Entity) { Entity entity = (Entity) o; return id == entity.id && entity.no >= no; } return false; } public Entity(int id, String name, int no) { this.id = id; this.name = name; this.no = no; } public String toString() { return String.format("[id=%d,name='%s',no=%d]", id, name, no); } } public static void main(String[] args) { List initialList = new ArrayList(); initialList.add(new Entity(5, "person", 9)); initialList.add(new Entity(6, "person", 19)); initialList.add(new Entity(6, "jam", 10)); System.out.println(initialList); Set magicCleanSet = new HashSet(); magicCleanSet.addAll(initialList); System.out.println(magicCleanSet); } 

}

输出是:

 [[id=5,name='person',no=9], [id=6,name='person',no=19], [id=6,name='jam',no=10]] [[id=5,name='person',no=9], [id=6,name='person',no=19]] 

希望这对您的任务有所帮助。