Gathering keys and positions of value in HashMap in the start and checking if this key is already exist while adding new key-value