Comes to me the collections that I do not so decide?

Hello, does not reach me topic of Java collections, specifically the HashMap can not solve the problem, so the condition is: remove people with the same name, here's the template code where you want to paste the solution :
public class Main{
 public static HashMap<string,string> createMap(){
 HashMap<string,string> map = new HashMap();
 map.put("Ivanov", "Ivan"); 
 map.put("DOE", "John");
 map.put("Kovalenko", "Peter");
 map.put("Lubny", "Ivan"); 
 return map;
}
 public static void removeTheFirstNameDuplicates(HashMap<string,string>map){

 //here we can insert the solution 
}
 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());
}
}
 }</string></string,string></string,string></string,string></string,string></string,string>

That's actually the solution the way 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());
}
}
}

 }</string></string></string,string></string,string></string,string>

Please teach,what am I doing wrong?I really want to become a programmer! thanks in advance!
September 19th 19 at 13:18
7 answers
September 19th 19 at 13:20
Solution
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());
}
}
 }</string></string></string></string></string></string></string,string>
sucks decided - maegan_OConnell66 commented on September 19th 19 at 13:23
: please explain - Jake.Balistreri commented on September 19th 19 at 13:26
my answer below - maegan_OConnell66 commented on September 19th 19 at 13:29
See my answer. If something is not clear, ask. - reuben.Bartolett commented on September 19th 19 at 13:32
: how is my answer wrong? - Jake.Balistreri commented on September 19th 19 at 13:35
: The complexity of your solution eksponentsialnaya. - reuben.Bartolett commented on September 19th 19 at 13:38
: do not talk nonsense, he "only" square. - Rick_Morar24 commented on September 19th 19 at 13:41
: Yes, I was wrong. Quadratic complexity) - reuben.Bartolett commented on September 19th 19 at 13:44
September 19th 19 at 13:22
If the task is to HashMap, then using them and need to be addressed. Pass on this HashMap, for each name of him add 1 to the value in another HashMap, cluching which will name. Then you'll be on the new HashMap and if the value had > 1 then delete from the old elements with value equal to the current key.
September 19th 19 at 13:24
Two options.
1. Frontal. The double loop, compare each with each, if equal to one to remove.
2. Move in all Set, it will automatically delete the duplicates, move back.
I don't exactly with the first option doing? but thanks, good idea - maegan_OConnell66 commented on September 19th 19 at 13:27
September 19th 19 at 13:26
And you need to remove all the people, or to leave one?
In the second case, accumulate a lot of names, and if another person the name belongs to the set, it is removed. If you don't belong - added to many names.
If you need to remove all Ivanov:
- make two sets: the names that was found, and the names that met more than once.
- looping through the table: if the next name in the first set there, add it there, and if there is - add a second set.
- remove from the table all the people whose names are in the second set.
Ivanov is a name that is a key in the map=> that Ivanov can only be 1. - maegan_OConnell66 commented on September 19th 19 at 13:29
: Meant "all people with the name Ivan". Them in the list two. - Jake.Balistreri commented on September 19th 19 at 13:32
September 19th 19 at 13:28
I did similar recently chekio , in Python.... but there once was sheet sets and from it we had to remove this set. Set (set) just do the easy thing, for some tasks, in this case should help.
September 19th 19 at 13:30
public class Main {
 public static Map<string, string> usersMap = new LinkedHashMap<string, string>() {{
 put("Ivanov", "Ivan");
 put("DOE", "John");
 put("Kovalenko", "Peter");
 put("Lubny", "Ivan");
}};

 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);
}
}</string></map.entry<string,></string></string></string></string>
The condition is not satisfied. You have only removed one pair and need everything. - maegan_OConnell66 commented on September 19th 19 at 13:33
I have removed it duplicate. Leaving only 1 who was found. Like so the task was - Jake.Balistreri commented on September 19th 19 at 13:36
I thought differently)
"here's the deal: remove people with the same name"
well, let the author solves - maegan_OConnell66 commented on September 19th 19 at 13:39
September 19th 19 at 13:32
public class ReplaceFromMapTest {
 public static void main(String[] args) {
 if(deleteNamesake(getNames())) {
 System.out.println("people with identical names were removed from \"list\".");
}
}

 public static Map<string, string> getNames() {
 Map<string, string> names = new HashMap<string, string>();
 names.put("Ivanov", "Ivan");
 names.put("DOE", "John");
 names.put("Kovalenko", "Peter");
 names.put("Lubny", "Ivan");
 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;
}
}</string></string></string></string></string></string></string></string>


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

Find more questions by tags JavaProgramming