How to get a key from LinkedHashMap?
For example I have LinkedHashMap
which contains key and user names and I need to input name with the help of scanner after that I want to see what key that element has, the element that I input:
static LinkedHashMap<Integer, String> names = new LinkedHashMap<>();
static Scanner sc = new Scanner(System.in);
static int i = 1;
stasic String name;
public static void main(String[] args) {
int b = 0;
while (b == 0) {
System.out.println("Input your name: ");
name = sc.nextLine;
names.put(i, name);
i += 1;
outName();
}
}
public static void outName() {
System.out.println("Input name: ");
name = sc.nextLine();
if (names.containsValue(name)) {
//I do not know what should I do here?
}
}
for (Map.Entry<String, ArrayList<String>> entry : names.entrySet()) {
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
}
use Map.Entry to iterate