Use a HashSet. If your use case needs to have (key, value) pairs, then maintain a HashMap and a HashSet both, and whenever a key is inserted in the HashMap, insert it in the HashSet as well. Otherwise, just maintain a HashSet.
Then you can use retainAll() function to find the intersection of the two sets.
HashSet intersection = hashSet1.retainAll(hashSet2);
The time complexity will be O(n) amortised. This is almost the same as that of what you are doing, but this would make your code much cleaner and readable.
Note that you can maintain a List instead of Set and call the retainAll() method of list. However, retainAll() of List will run in O(n^2) complexity, because the contains() method of List runs in O(n) whereas contains() of HashSet runs in O(1) amortised.