No idea why this is not working while the solution I found is exactly the same (probably not but I don't see it). Please help me see what the difference is.
package com.codegym.task.task08.task0815;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/*
Census
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code
// create and intialize a hashmap
HashMap<String, String> map = new HashMap<>();
map.put("LastName", "Name");//create a HashMap with 10 key-value pairs (lastName , name)
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
map.put("LastName", "Name");
/*HashMap<String, String> map = new HashMap<>();
map.put("A", "A");
map.put("B", "B");
map.put("C", "A");
map.put("D", "D");
map.put("E", "A");
map.put("F", "D");
map.put("G", "A");
map.put("H", "G");
map.put("I", "G");
map.put("J", "A");
return map;*/
return map;
}
public static int getSameFirstNameCount(HashMap<String, String> map, String name) {
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();//create an iterator
int count = 0;//create and intialize the int count
while(iterator.hasNext())//loops over all the strings checks if there is a next
{
Map.Entry<String, String> pair = iterator.next();// get the next
if(pair.getValue().equals(name)){// compares the values in the pair key-value (lastName, name)
count++;// if there are keys(lastName) that are the same +1
}
}
return count;// gives the total number of people with the same lastName
}
public static int getSameLastNameCount(HashMap<String, String> map, String lastName) {
//write your code here
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();//create and intialize an iterator for Map
int count = 0;//create and intialize the int count
while(iterator.hasNext())//create an iterator that loops over all the strings checks if there is a next
{
Map.Entry<String, String> pair = iterator.next();// get the next element
//String value = pair.getValue(); //to get the key which is first name not neccesary
if(pair.getKey().equals(lastName))// compares the keys(lastName) with the next
{
count++;// if there are keys that are the same +1 count
}
}
return count;// gives the total number of people with the same name
}
public static void main(String[] args) {
}
}