java 循环中使用list时,出现list中全部加入了对象导致没有实现分组的解决方案
清空list即可
public List fetchNmapSingleTaskWithDataList() { Map map = new HashMap<>(); List listNst = new ArrayList(); List listNsp = new ArrayList(); List<NmapSingleTaskWithBLOBs> nmapSingleTaskWithBLOBs = nmapSingleTaskMapper.selectByExampleWithBLOBs(null); List<NmapSingleipPortresultWithBLOBs> nmapSingleipPortresultWithBLOBs = this.getAll(); for (NmapSingleTask nst : nmapSingleTaskWithBLOBs) { map.put("NmapSingleTask", nst); for (NmapSingleipPortresult nsp : nmapSingleipPortresultWithBLOBs) { if (nst.getId().equals(nsp.getSingletaskid())) { listNsp.add(nsp); } } map.put("NmapSingleipPortresults", listNsp); listNst.add(map); // 清空map map = new HashMap(); // 清空list 如果不清空,listNsp会再次加入nsp,导致全部加进来了 listNsp = new ArrayList(); } return listNst; }