0

how?

how can i make it that the first names (the keys) will stay as they are and the family names (the values) will change? i tried to do something like that (but it takes the same value and sets all the keys to tha same values) : import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) { HashMap <String , String> h = new HashMap <>(); h.put ("Rody" , "Ricch"); h.put ("Travis" , "Scott"); ArrayList <String> k = new ArrayList <> (h.keySet()); //takes all the hashmap's keys if (k.size() % 2 == 0){ ArrayList <String> v = new ArrayList <> (h.values()); //takes all the hashmap's values Collections.shuffle(v); //shuffling the values HashMap <Object , Object> NewH = new HashMap <>(); for (Object i : k){ for (Object j : v){ NewH.put (i , j); } } System.out.println(NewH); } //end if statement } }

22nd Aug 2020, 11:58 AM
Yahel
Yahel - avatar
1 ответ
+ 5
Hello yahel Let's take this example: h.put("Rody", "Ricch"); If you want to change "Ricch" you need to overwrite the key and the value. Means you add the same key with a different value to the map: h.put("Rody", "Smith"); Maybe it helps to explain what you really want to do with your program.
22nd Aug 2020, 12:11 PM
Denise Roßberg
Denise Roßberg - avatar