Reputation: 613
I have a HashMap which stores Attendance information. I just want to convert the Key to Int and check for condition.
Below is my code:
import java.util.*;
public class HashClass {
public static void main(String args[]) {
HashMap<Integer, String> attendanceHashMap = new HashMap<Integer, String>();
attendanceHashMap.put(1, "John");
attendanceHashMap.put(2, "Jacob");
attendanceHashMap.put(3, "Peter");
attendanceHashMap.put(4, "Clara");
attendanceHashMap.put(5, "Philip");
for(HashMap.Entry m:attendanceHashMap.entrySet()){
if(Integer.valueOf((int)m.getKey())<3) break;
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
I want to print like this
3 Peter
4 Clara
5 Philip
I tried these methods:
- (int)m.getKey() : not working
- Integer.valueOf((int)m.getKey()) : not working
- Integer.valueOf(m.getKey()) : not working
How this can be achieved ?
Upvotes: 2
Views: 15411
Reputation: 325
you mean you dont want to show the entries which key < 3 ,I think when you run this code you will get nothing because when key = 1 ,key < 3 = true and the loop break, they won`t continue you can try continue instead of break
Upvotes: 0
Reputation: 298143
You are saying:
if(Integer.valueOf((int)m.getKey())<3) break;
In other words, if the first key you encounter happens to be smaller than three, you terminate the loop, hence print nothing. Most probably, you want to use continue
instead, to process the next entry:
for(HashMap.Entry m:attendanceHashMap.entrySet()){
if(Integer.valueOf((int)m.getKey())<3) continue;
System.out.println(m.getKey()+" "+m.getValue());
}
But note that the type conversions are obsolete. Just add the missing type arguments to the entry:
for(HashMap.Entry<Integer,String> m:attendanceHashMap.entrySet()){
if(m.getKey()<3) continue;
System.out.println(m.getKey()+" "+m.getValue());
}
But it might be clearer to make the print statement conditional instead of using loop control:
for(HashMap.Entry<Integer,String> m:attendanceHashMap.entrySet()){
if(m.getKey()>=3) {
System.out.println(m.getKey()+" "+m.getValue());
}
}
As a side note, the order of the printed entries is not guaranteed. If you want to print the entries in insertion order, use a LinkedHashMap
:
HashMap<Integer, String> attendanceHashMap = new LinkedHashMap<>();
attendanceHashMap.put(1, "John");
attendanceHashMap.put(2, "Jacob");
attendanceHashMap.put(3, "Peter");
attendanceHashMap.put(4, "Clara");
attendanceHashMap.put(5, "Philip");
// printing code follows...
And just for completeness, the Java 8 solution:
attendanceHashMap.forEach((k,v) -> { if(k>=3) System.out.println(k+" "+v); });
Upvotes: 5
Reputation: 100169
Your first problem is that you are using raw Entry
type. Replace it with
for(HashMap.Entry<Integer, String> m: ...)
Now m.getKey()
will have an Integer
type which can be converted to int
implicitly without any problems: if(m.getKey()<3)
.
Another problem is that you are using break
instead of continue
. Write
if(m.getKey()<3) continue;
The whole fixed loop:
for(HashMap.Entry<Integer, String> m:attendanceHashMap.entrySet()){
if(m.getKey()<3) continue;
System.out.println(m.getKey()+" "+m.getValue());
}
Never use rawtypes. It's just a compatibility remnant.
Upvotes: 2