Both List and HashMap are part of collections in Java. Use List for the operations related to keys and HashMap for key and value pairs.
List has following methods:
Declaration:
List<Integer> list = new ArrayList<>();
Add– use it to add keys
Example:
list.add(1);
Remove – remove the keys
Example:
list.remove(0); // removes element at index 0
list.remove(Integer.valueOf(1)); // removes element with value 1
get – used to fetch the value based on index
list.get(0) — fetch 1 in this case
size – used to get the size of list
Example usage:
int size = list.size();
HashMap has following methods:
Declaration:
HashMap<Integer,Integer> map= new HashMap<>();
Put– use it to add keys and values
Example:
map.put(1,2);
get – used to fetch the value based on key
map.get(1) — fetch 2 in this case
size – used to get the size of map
Example usage:
int size = map.size();