There is a method that must be reduced to two for loops. One for loading data, and the second for searching in this map the elements of the second list. Any thoughts on how to do this?
package ru.job4j.store; import java.util.HashMap; import java.util.List; import java.util.Map; public class Store { /** * ΠΠ΅ΡΠΎΠ΄ ΡΡΠΈΡΠ°Π΅Ρ ΠΊΠΎΠ»-Π²ΠΎ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ. * <p> * ΠΠ΅ΡΠΎΠ΄ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ: * ΠΡΠ»ΠΈ ΠΊΠΎΠ»-Π²ΠΎ ΠΊΠ»ΡΡΠ΅ΠΉ ΠΈΠ· ΠΏΡΠΎΡΠ»ΠΎΠΉ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ Π±ΠΎΠ»ΡΡΠ΅ ΡΠ΅ΠΌ Ρ Π½ΠΎΠ²ΠΎΠΉ ΡΠ°ΡΡΠ΅Ρ ΡΡΠ΅ΡΡΠΈΠΊ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ². * ΠΡΠ»ΠΈ ΠΊΠΎΠ»-Π²ΠΎ ΠΊΠ»ΡΡΠ΅ΠΉ ΠΈΠ· ΠΏΡΠΎΡΠ»ΠΎΠΉ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΠΈ ΠΌΠ΅Π½ΡΡΠ΅ ΡΠ΅ΠΌ Ρ Π½ΠΎΠ²ΠΎΠΉ ΡΠ°ΡΡΠ΅Ρ ΡΡΠ΅ΡΡΠΈΠΊ ΡΠ΄Π°Π»Π΅Π½ΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ². * ΠΡΠ»ΠΈ ΠΊΠ»ΡΡΠΈ Π½Π΅ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΡΠ΅ ΡΠΎ ΡΠ°ΡΡΠ΅Ρ ΡΡΠ΅ΡΡΠΈΠΊ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ. * * @param previous - ΠΏΡΠ΅ΠΆΠ½ΡΡ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΡ. * @param current - Π½ΠΎΠ²ΠΎΡ ΠΊΠΎΠ»Π»Π΅ΠΊΡΠΈΡ. * @return ΠΊΠ°ΡΡΡ Ρ ΡΡΠ΅ΡΡΠΈΠΊΠ°ΠΌΠΈ. */ public HashMap<String, Integer> isChange(List<User> previous, List<User> current) { int newAdd = 0; int newChange = 0; int newDelete = 0; Map<Integer, String> mapOfPrevious = new HashMap<>(); Map<Integer, String> mapOfCurrent = new HashMap<>(); HashMap<String, Integer> result = new HashMap<>(); for (User indexPrevious : previous) { mapOfPrevious.put(indexPrevious.getId(), indexPrevious.getName()); } for (User indexCurrent : current) { mapOfCurrent.put(indexCurrent.getId(), indexCurrent.getName()); } for (Integer key : mapOfPrevious.keySet()) { if (!mapOfCurrent.containsKey(key)) { newDelete++; } else if (!mapOfCurrent.get(key).equals(mapOfPrevious.get(key))) { newChange++; } } for (Integer key : mapOfCurrent.keySet()) { if (!mapOfPrevious.containsKey(key)) { newAdd++; } } result.put("Amount new add: ", newAdd); result.put("Amount new change: ", newChange); result.put("Amount new delete: ", newDelete); return result; } static class User { private int id; private String name; public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public String getName() { return this.name; } } }