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; } } } 

    2 answers 2

    First, take the number of old and data that came to update:

     int beforeSize = previous.size(); int afterSize = current.size(); 

    Add all users at once to one list:

     List<User> allUsers = new ArrayList<>(); allUsers.addAll(previous); allUsers.addAll(current); 

    And scatter in the map. When duplicating a key, its value changes; this can be used to get a card with already changed data:

     Map<Integer, String> usersMap = new HashMap<>(); for (User user : allUsers) { usersMap.put(user.getId(), user.getName()); } 

    Next we get the size of this card:

     int newListSize = usersMap.size(); 

    Now we have three numbers: how many pairs came as β€œnew”, how many were and how many became. Based on these data, you can get the number of new, the number of changed and the number of deleted.

     int newAdd = Math.max(0, newListSize - beforeSize); int newChange = Math.max(0, afterSize - newAdd); int newDelete = Math.max(0, newListSize - newAdd - newChange); 
    • The author has given the full code why it’s not clear why you give in chunks, therefore if the author doesn’t understand, the next questions will follow, where, why. Therefore, you need to give the full code by changing the author's code. - And
    • @And, so that the author does not just copy it himself and read and understand. If you have not noticed, I will explain the code in parts, otherwise it would be idiocy - Flippy
    • @And, if you want to - edit, I will accept - Flippy
    • Thanks for the comment. Explained as necessary. But your code is not fully working. Here it is: - Alexandar Vysotskiy
    • one
      If my answer helped you, mark it right by checking the box on the left - Flippy

    The issue was resolved as follows:

     package ru.job4j.store; 

    import java.util.HashMap; import java.util.List;

    public class Store {

     /** * ΠœΠ΅Ρ‚ΠΎΠ΄ считаСт ΠΊΠΎΠ»-Π²ΠΎ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΈ. * * @param previous - прСТняя коллСкция. * @param current - новоя коллСкция. * @return ΠΊΠ°Ρ€Ρ‚Ρƒ с счСтчиками. */ public HashMap<String, Integer> isChange(List<User> previous, List<User> current) { int deleteCount = 0; int changeCount = 0; int beforeSize = previous.size(); HashMap<Integer, String> allUsers = new HashMap<>(); for (User index : current) { allUsers.put(index.getId(), index.getName()); } for (User index : previous) { if (!allUsers.containsKey(index.getId())) { deleteCount++; } else if (!allUsers.get(index.getId()).equals(index.getName())) { changeCount++; } allUsers.put(index.getId(), index.getName()); } int newListSize = allUsers.size(); HashMap<String, Integer> result = new HashMap<>(); result.put("Amount new add: ", newListSize - beforeSize); result.put("Amount new change: ", changeCount); result.put("Amount new delete: ", deleteCount); 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; } } 

    }

    Here you can see the test for it:

    https://github.com/AlexandarVysotskiy/AVysotskiy/commit/1479aac7301a200f5611c81e002d342f44359e36