Issue
i make a linkedhashmap like this in java using netbean
LinkedHashMap map = new LinkedHashMap();
map.put(0, "one");
map.put(1, "two");
map.put(2, "three");
in the above code the key(0,1,2), i want that it auto generate. I try different online solution but doesn't work.
Solution
public class Test {
static int i = 0;
public static void main(String[] args) {
Map<Integer, String> map = new LinkedHashMap<>();
map.put(getNext(), "One");
map.put(getNext(), "Two");
map.put(getNext(), "Three");
map.forEach((key, value) -> System.out.println("Key = " + key + ", value = " + value));
}
static int getNext() {
i = i+1;
return i;
}
}
Answered By - Narendra Kekane