Source:
<code>protected void <START> executeQueryCommand() <END> { List<Disk> allDiskAndSnapshots = diskDao.getAllFromDisksIncludingSnapshots(getUserID(), getParameters().isFiltered()); HashMap<UUID, DiskImage> activeDisks = new HashMap<>(); HashMap<UUID, List<DiskImage>> disksLeftForAggregate = new HashMap<>(); List<Disk> result = new ArrayList<>(); allDiskAndSnapshots.stream() .filter(disk -> { if (disk.isOvfStore()) { result.add(disk); return false; } return true; }) .filter(disk -> { if (disk instanceof LunDisk) { result.add(disk); return false; } return true; }) .map(diskImage -> (DiskImage) diskImage) .forEach(diskImage -> { UUID imageId = diskImage.getId().getUuid(); if (diskImage.getActive()) { activeDisks.put(imageId, diskImage); if (disksLeftForAggregate.containsKey(imageId)) { activeDisks.get(imageId).getSnapshots().addAll(disksLeftForAggregate.get(imageId)); disksLeftForAggregate.remove(imageId); } } else { if (activeDisks.containsKey(imageId)) { activeDisks.get(imageId).getSnapshots().add(diskImage); } else if (disksLeftForAggregate.containsKey(imageId)) { disksLeftForAggregate.get(imageId).add(diskImage); } else { List<DiskImage> newDiskImageList = new ArrayList<>(); newDiskImageList.add(diskImage); disksLeftForAggregate.put(imageId, newDiskImageList); } } }); result.addAll(activeDisks.values().stream().collect(Collectors.toList())); getQueryReturnValue().setReturnValue(result); }</code><technical_language>Generally logic ok, a bit complicated. If I you, I would: 1. Collectors.partitioningBy create lists out of allDiskAndSnapshots - DiskImage objects another rest. 2. Collectors.partitioningBy create lists out of DiskImage objects list - active active disks. 3. active add snapshots disk. 4. Add active disks DiskImage disks section 1 return this list. separate more method it, IMHO, easier understand maintain</technical_language>

Target:
protected void executeQueryCommand() { List<Disk> allDisksAndSnapshots = diskDao.getAllFromDisksIncludingSnapshots(getUserID(), getParameters().isFiltered()); HashMap<UUID, DiskImage> activeDisks = new HashMap<>(); HashMap<UUID, List<DiskImage>> disksLeftForAggregate = new HashMap<>(); List<Disk> result = new ArrayList<>(); allDisksAndSnapshots.stream() .filter(disk -> { if (disk.isOvfStore()) { result.add(disk); return false; } return true; }) .filter(disk -> { if (disk instanceof LunDisk) { result.add(disk); return false; } return true; }) .map(diskImage -> (DiskImage) diskImage) .forEach(diskImage -> { UUID imageId = diskImage.getId().getUuid(); if (diskImage.getActive()) { activeDisks.put(imageId, diskImage); if (disksLeftForAggregate.containsKey(imageId)) { activeDisks.get(imageId).getSnapshots().addAll(disksLeftForAggregate.get(imageId)); disksLeftForAggregate.remove(imageId); } } else { if (activeDisks.containsKey(imageId)) { activeDisks.get(imageId).getSnapshots().add(diskImage); } else if (disksLeftForAggregate.containsKey(imageId)) { disksLeftForAggregate.get(imageId).add(diskImage); } else { List<DiskImage> newDiskImageList = new ArrayList<>(); newDiskImageList.add(diskImage); disksLeftForAggregate.put(imageId, newDiskImageList); } } }); result.addAll(new ArrayList<>(activeDisks.values())); getQueryReturnValue().setReturnValue(result); }