Things To Remember In Maps (Java)

This article helps you to recall the methods and classes of Maps and suggests you some leet code problems that you can start. And this article assumes that you have a basic understanding of the collection framework

K = datatype of Key

V = datatype of Vlaue

Methods To Remember :

  1. put(key , value )

  2. keySet() -> returns Set data structure of all keys

  3. get(key) -> returns value corresponding to the key

  4. containsKey(key) -> true if the key is present else false

  5. isEmpty()

  6. remove(object key) -> removes the key-value pair and returns the value of the argument key

  7. values()

  8. clone() -> returns a shallow copy of the map

Classes To Remember

  1. HashMap

  2. LinkedHashMap: It maintains the insertion order

    Data Structures Used: HashTable and LinkedList

  3. TreeMap: It arranges the data in the sorted order considering the keys

    Data Structure Used: Red-Black Tree (self-balancing binary search tree)

Methods that we should remember in TreeMap

As TreeMap implements the NavigableMap Interface below are some of the methods which we can invoke with the object of treemap and I feel these are methods that are only mostly used.

  1. firstEntry() -> returns MapEntry<K,V> (the first entry with kay and value which is least among all )

  2. lastEntry() -> returns MapEntry<K , V> (obbesly it will be largest )

  3. pollFirstEntry() -> returns MapEntry<K , V> (removes and returns the first entry )

  4. pollLastEntry() -> returns MapEntry<K , V> (removes and returns the last entry )

  5. subMap(K lowerBound , boolean include , K upperBound , boolean include )

    -> returns NavigableMap<K , V >

Exceptions

  1. ClassCastExecption

  2. NullPointerExecption

  3. IllegalArgumentExecption

Problems

Below are a few problems :

  1. subarray-with-given-sum

  2. longest-substring-without-repeating-characters

  3. Count Distinct Elements in every Window of size k

  4. find-missing-elements-of-a-range

  5. check-if-an-array-can-be-divided-into-pairs-whose-sum-is-divisible-by-k

  6. For Interview questions visit www.interviewbit.com

Thank you for investing your time in reading this article. I sincerely hope it has provided valuable insights to enhance your journey. If you found it helpful, I encourage you to consider subscribing for more informative content. Feel free to share your insights or any additional information that could contribute to the discussion.

Happy Coding.

Did you find this article valuable?

Support Keerthivardhan by becoming a sponsor. Any amount is appreciated!