-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyOfSameValue.java
More file actions
32 lines (32 loc) · 955 Bytes
/
KeyOfSameValue.java
File metadata and controls
32 lines (32 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
public class KeyOfSameValue {
public static void main(String[] args){
Scanner s=new Scanner(System.in);
Map<String, String> map=new HashMap<String, String>();
System.out.println("Enter number of entries");
int n= s.nextInt();
System.out.println("Enter "+n+" entries");
for(int i=0;i<n;i++){
String key=s.next();
String val=s.next();
map.put(key, val);
}
System.out.println(map);
System.out.println("Enter a value");
String find=s.next();
List<String> l=new ArrayList<String>();
Iterator<Map.Entry<String, String>> itr=map.entrySet().iterator();
while(itr.hasNext()){
Map.Entry<String, String> entry=itr.next();
if(entry.getValue().equals(find)){
l.add(entry.getKey());
}
}
String[] arr = new String[l.size()];
arr = l.toArray(arr);
System.out.println("Keys that have "+find+" as value..");
for(String i:arr){
System.out.println(i);
}
}
}