Update 2sum.java

Don't actually need to put all the elements into hashmap at the beginning.
Because we are looking for a pair. We can just add elements into hashmap as we go.
And we don't need to compare the indices, since the result of map.get will always give the first index.