![Java for loop hashmap](https://loka.nahovitsyn.com/78.jpg)
![java for loop hashmap java for loop hashmap](https://2.bp.blogspot.com/-eXmR5qzDSbQ/WS-pA5eaTsI/AAAAAAAAB5o/uhDnNfnyigsMg4hTnsdnh5DkKN247V1GACLcB/s1600/infinite-loop-in-hashmap-rehashing-code-explained-step-2.png)
Only you said you are not allowed that and that. It doesn't say that you are not allowed something, nor it does say what you are allowed. * In the next iteration an Id (key) and studentDetails (value) would be of different student */īut again, I'm not entirely sure what you can use in your assignment. StudentsDatabase.put("", studentDetails) So what I was thinking of (and that is purely to work on the structure you laid down with Map>): Honestly I am willing to start over from scratch cause I feel like it shouldn't be this complicated and there is a simpler approach of doing this haha String fullName1 = sc.next() + sc.nextLine() Īm I doing it right? What did you have in mind?
Liutauras Vilda wrote:Why? Could you give an example please.īecause I need to create a new one each time it loops? 1.1 HashMap.entrySet () HashMap.entrySet () Map SetHow do I create new values for the same hashmap without overwriting the previous inputted hashmaps? I am not supposed any methods, classes or anything object related. (database.get(inputID)) Īfter the first loop, if I choose to create a new student record it will to start to overwrite the previous one and I get two hashmaps with the same values inside my Hashmap>: I suggest you move the new ArrayList inside the loop instead of reusing the list each time. ("Enter the Student ID of the student you want to find")
![java for loop hashmap java for loop hashmap](https://3.bp.blogspot.com/-ysHHskhoCxA/V6ihGm4XU0I/AAAAAAAAGzg/GLK-Mn7pjgYJ9D-ouD7L2UMkueLSPZqWACLcB/s1600/ConcurrentHashMap%2Bin%2BJava%2BExample.png)
("Enter '2' to search for a student in the database") Student.put("Year Enrolled", yearEnrolled) ĭatabase.put(studentID,student) //I use the student ID as key and the hashmap as the value String yearEnrolled = sc.next() + sc.nextLine() String course = sc.next() + sc.nextLine()
#Java for loop hashmap code#
Am I doing anything wrong Is there any change I have to make Below is the code and its output. The HashMap can be iterated over with the forEach() method. Java HashMap 'put' method in a for loop Ask Question Asked 10 years, 3 months ago Modified 9 years, 7 months ago Viewed 9k times 2 I am facing an issue while using HashMap in a for loop. String fullName = sc.next() + sc.nextLine() The forEach() is a new method introduced in Java 8 to iterate over collections. Scanner sc = new Scanner(System.in) // Sorry for this messy code btw especially for the scanner if the student is found, output their details Lets user find a student record by their student ID
![Java for loop hashmap](https://loka.nahovitsyn.com/78.jpg)