D
D
Denis Sechin2015-02-26 18:23:10
Java
Denis Sechin, 2015-02-26 18:23:10

The topic of collections does not reach me, what am I doing wrong?

Hello, the topic of collections in Java does not reach me, and specifically HashMap, I cannot solve the problem, here is the condition: remove people with the same names, here is the template code where you need to insert the solution:

public class Main{
    public static HashMap<String,String> createMap(){
      HashMap<String,String> map = new HashMap();
      map.put("Иванов", "Иван"); 
      map.put("Пупкин", "Вася");
      map.put("Коваленко", "Петя");
      map.put("Лубнов", "Иван");  
      return map;
    }
    public static void removeTheFirstNameDuplicates(HashMap<String,String>map){
      
    //сюда надо вставить решение 
    }
    public static void removeItemFromMapByValue(HashMap<String,String>map,String value){
      HashMap<String,String>copy = new HashMap(map);
      for(Map.Entry<String, String> pair: copy.entrySet()){
        if(pair.getValue().equals(value)){
          map.remove(pair.getKey());
        }
      }
    }

Here is the actual solution as I see it
public static void removeTheFirstNameDuplicates(HashMap<String,String>map){
      HashMap <String,String> map2 = new HashMap(map);
      HashMap <String,String> map3 = new HashMap(map);
      
      for(Map.Entry<String, String> pair : map2.entrySet()){
        for(Map.Entry<String, String>pair1 :map3.entrySet()){
          if(pair.equals(pair1.getValue())){
            removeItemFromMapByValue(map,pair.getValue());
          }
        }
      }
      
    }

Please teach me what I'm doing wrong? I really want to become a programmer! thanks in advance!

Answer the question

In order to leave comments, you need to log in

7 answer(s)
D
Denis Sechin, 2015-02-26
@tamogavk

I decided:

public static void removeTheFirstNameDuplicates(HashMap<String,String>map){
       HashMap<String, String> copy1 = new HashMap<String, String>(map);
            HashMap<String, String> copy2 = new HashMap<String, String>(map);
            for (Map.Entry<String, String> pair1: copy1.entrySet())
            {
                int count = 0;
                for (Map.Entry<String, String> pair2: copy2.entrySet()) {
                    if (pair1.getValue().equals(pair2.getValue())) {
                        count++;
                    }
                }
                if (count > 1) {
                    removeItemFromMapByValue(map, pair1.getValue());
                }
            }
        }

E
evnuh, 2015-02-26
@evnuh

If the task is on hashmaps, then with the help of them it is necessary to solve. Go through this hashmap, for each name from it, add 1 to the value in another hashmap, in which the name will be the key. Next, go through the new hashmap and if its value is > 1, then delete elements with a value equal to the current key from the old one.

V
Victor Gogilchin, 2015-02-26
@lslayer

Two options.
1. Frontal. Double loop, compare each with each, if equal - delete one.
2. Move everything to Set, it will automatically remove duplicates, move back.

M
Mrrl, 2015-02-26
@Mrl

Do you need to remove all people, or leave one?
In the second case, you accumulate a set of names, and if the name of the next person belongs to the set, you delete it. If it does not belong - add to the set of names.
If you need to delete all Ivans:
- start two sets: names that met, and names that met more than once.
- in the loop through the table: if there is no next name in the first set, add it there, and if there is, add it to the second set.
- remove from the table all people whose names are in the second set.

L
landstalker, 2015-02-26
@landstalker

I recently did something similar on chekio , on python.... but there was immediately a list of sets and it was necessary to remove this set from it. Sets (set) just do a handy thing, for some tasks, in this case it should help.

N
Nikolai Pavlov, 2015-02-27
@gurinderu

public class Main {
  public static Map<String, String> usersMap = new LinkedHashMap<String, String>() {{
        put("Иванов", "Иван");
        put("Пупкин", "Вася");
        put("Коваленко", "Петя");
        put("Лубнов", "Иван");
    }};

    public static void main(String[] args) {
        Set<String> names = new HashSet<String>();
        for (Iterator<Map.Entry<String, String>> iterator = usersMap.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry<String, String> next = iterator.next();
            if (names.contains(next.getValue())) {
                iterator.remove();
            } else {
                names.add(next.getValue());
            }
        }
        System.out.println(usersMap);
    }
}

C
cane, 2015-07-02
@cane

public class ReplaceFromMapTest {
  public static void main(String[] args) {
    if(deleteNamesake(getNames())) {
      System.out.println("люди с одинаковыми именами были удалены из \"списка\".");
    }
  }
  
  public static Map<String, String> getNames() {
    Map<String, String> names = new HashMap<String, String>();
    names.put("Иванов", "Иван");
    names.put("Пупкин", "Вася");
    names.put("Коваленко", "Петя");
    names.put("Лубнов", "Иван");
    return names;
  }
  
  public static boolean deleteNamesake(Map<String, String> names) {
    boolean result = false;
    Collection<String> allFirstNames = names.values();
    Set<String> delNames = new HashSet<String>();
    for(Map.Entry<String, String> p : names.entrySet()) {
      if(Collections.frequency(allFirstNames, p.getValue()) > 1) {
        delNames.add(p.getKey());
      }
    }
    if(delNames.size() > 0) {
      for(String n : delNames) {
        names.remove(n);
        result = true;
      }
    }
    return result;
  }
}

Only I would not use the names of people as keys in the HashMap, there is a high probability of coincidence.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question