Collision resolution in Java HashMap

When you insert the pair (10, 17) and then (10, 20), there is technically no collision involved. You are just replacing the old value with the new value for a given key 10 (since in both cases, 10 is equal to 10 and also the hash code for 10 is always 10). Collision happens when … Read more

Accessing the last entry in a Map

To answer your question in one sentence: Per default, Maps don’t have a last entry, it’s not part of their contract. And a side note: it’s good practice to code against interfaces, not the implementation classes (see Effective Java by Joshua Bloch, Chapter 8, Item 52: Refer to objects by their interfaces). So your declaration … Read more

Loading a map using Properties class

Don’t use Properties, which is legacy Divide entries into multiple files Read each file in sequence, load and process using Preferences Example code: package com.mypack.test; import java.io.*; import java.util.*; import java.util.prefs.Preferences; public class PreferencesExample { public static void main(String args[]) throws FileNotFoundException { Preferences ps = Preferences.userNodeForPackage(PreferencesExample.class); // Load file object File fileObj = new … Read more