1. i know that you can't remove stuff from the map you are iterating
2. i tried stuff from people that posted with the alterations they said it worked for them. Not for me tho
3. so i even tried instead of coping and .remove from the map, to create a new map and .put all the objects that are not supposed to be deleted ( should be the same, right?). than coping it to the map: map = m2. But this is not altering the map variable in main.
4. the previous thing works if i change the removeAll... from a VOID to a HASHMAP with a return value, so when i call it in the main: map = removeAllSummerPeople(map); it works totally fine and the map is altered.
But of course the conditions do not allow this.
--> So, why is it not working in the first place, that i read the copy of the map and remove from the original?
-->And why does the [3.] not alter the map variable in main? Is the varible in main suppose to be 'final' or 'public' or smt??
I ran into the problem before (void not changing the main variable) but i don´t know how I managed last time... I do NOT only want to know what works, but also WHY this does not.
thanks for the answers
Here is the code that works for me but is not valid:
</package com.codegym.task.task08.task0816;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class Solution {
public static HashMap<String, Date> createMap() throws ParseException {
DateFormat df = new SimpleDateFormat("MMMMM d yyyy", Locale.ENGLISH);
HashMap<String, Date> map = new HashMap<String, Date>();
map.put("Stallone", df.parse("JUNE 1 1980"));
map.put("a", df.parse("JUNE 1 1980"));
map.put("b",df.parse("JUNE 1 1980"));
map.put("c",df.parse("SEPTEMBER 1 1980"));
map.put("d",df.parse("JUNE 1 1980"));
map.put("e",df.parse("MARCH 1 1980"));
map.put("f",df.parse("JUNE 1 1980"));
map.put("g",df.parse("JUNE 1 1980"));
map.put("h",df.parse("JUNE 1 1980"));
map.put("i",df.parse("DECEMBER 1 1980"));
return map;
}
public static HashMap<String, Date> removeAllSummerPeople(HashMap<String, Date> map) {
//write your code here
HashMap<String, Date> m2 = new HashMap<>();
for(Map.Entry<String, Date> entry : map.entrySet())
{
int d = entry.getValue().getMonth();
String s = entry.getKey();
Date da = entry.getValue();
if(d <5 || d > 7)
//System.out.println("d");
m2.put(s, da);
}
return m2;
}
public static void main(String[] args) throws ParseException {
HashMap<String, Date> map = createMap();
for(Map.Entry<String, Date> entry : map.entrySet())
System.out.println(entry.getKey() +" : "+ (entry.getValue().getMonth()+1));
map = removeAllSummerPeople(map);
for(Map.Entry<String, Date> entry : map.entrySet())
System.out.println(entry.getKey() +" : "+ (entry.getValue().getMonth()+1));
}
}
>
package com.codegym.task.task08.task0816;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/*
Kind Emma and the summer holidays
Create a Map<String, Date> and add ten entries that represent (last name, birth date) pairs.
Remove from the map all people born in the summer.
Hint: At CodeGym, summer lasts from June 1 to August 31.
Requirements:
1. The program should not display text on the screen.
2. The program should not read values from the keyboard.
3. The createMap() method must create and return a HashMap that has (String, Date) elements and contains 10 entries.
4. The removeAllSummerPeople() method should remove from the map all people born in the summer.
*/
public class Solution {
public static HashMap<String, Date> createMap() throws ParseException {
DateFormat df = new SimpleDateFormat("MMMMM d yyyy", Locale.ENGLISH);
HashMap<String, Date> map = new HashMap<String, Date>();
map.put("Stallone", df.parse("JUNE 1 1980"));
map.put("a", df.parse("JUNE 1 1980"));
map.put("b",df.parse("JUNE 1 1980"));
map.put("c",df.parse("JUNE 1 1980"));
map.put("d",df.parse("JUNE 1 1980"));
map.put("e",df.parse("JUNE 1 1980"));
map.put("f",df.parse("JUNE 1 1980"));
map.put("g",df.parse("JUNE 1 1980"));
map.put("h",df.parse("JUNE 1 1980"));
map.put("i",df.parse("DECEMBER 1 1980"));
//write your code here
return map;
}
public static void removeAllSummerPeople(HashMap<String, Date> map) {
//write your code here
HashMap<String, Date> m2 = map;
for(Map.Entry<String, Date> entry : m2.entrySet())
{
int d = entry.getValue().getMonth();
String s = entry.getKey();
Date da = entry.getValue();
if(d > 4 && d <8)
//System.out.println("d");
map.remove(s);
}
}
public static void main(String[] args) throws ParseException {
/*HashMap<String, Date> map = createMap();
for(Map.Entry<String, Date> entry : map.entrySet())
System.out.println(entry.getKey() +" : "+ (entry.getValue().getMonth()+1));
removeAllSummerPeople(map);
for(Map.Entry<String, Date> entry : map.entrySet())
System.out.println(entry.getKey() +" : "+ (entry.getValue().getMonth()+1));*/
}
}
This worked. What i also tried and what I am confused about why it didn´t work is creating a new m2, putting all the objects except summer-born-people in it and than setting the 'map' reference to it, like so:
Do you got any clues why this doesn´t work by any chance?