What is the difference between a Pair Class and a HashMap
. I see both of them having key and value
.
Code Earlier:
List<Patient> patientDetails = new ArrayList<Patient>();
Patient patientInfo = new Patient();
patientInfo.setReligion(Constants.ReligionArray[custom.getReligion()]);
patientDetails.add(patientInfo);
The code has been changed to this today.
Changed Code
List<Pair<String, String>> patientInfo = new ArrayList<Pair<String, String>>();
patientInfo.add(new Pair<String, String>("Religion", Constants.ReligionArray[custom.getReligion()]));
Pair Class here?
What could be the reason.Pair Class and a HashMap
.Note: Both the code works perfectly.
The implementation of Pair class contains the following components: Two public fields: first and second, just like in C++. A private constructor: takes two arguments – a key and, its corresponding value. Method of(): A static factory method for creating an immutable, Typed Pair instance.
Pairs provide a convenient way of handling simple key to value association, and are particularly useful when we want to return two values from a method. A simple implementation of a Pair is available in the core Java libraries.
JavaFX 2.2 has the javafx. util. Pair class which can be used to store a pair. We need to store the values into Pair using the parameterized constructor provided by the javafx.
The pair class in Java is under the util package of the JavaFX package.
I think that the question you're asking is: what is the difference between
List<Pair<T,S>>
and HashMap<T,S>
. There are many.
In general, a Map
object is used to associate a value with a key and provide means to quickly get the values based on the key. There can usually be a single value associated with one key.
A List
is an ordered container in which objects are stored, provides means to iterate over its contents and get an element based on it's location in list (get Nth element). Usually the same elements may be duplicated within a list.
In your case, when you are using a list, your patients can have a several religions. To find what kind of religion your patient has, you have to manually search the list for the pair with the first element set to religion.
Had you used a map, each patient would have a single religion associated with him (or none). You could also find the religion quickly through Map<String,String>.get(String key)
Actually, your code has been changed away from typical Java approach into what is most often referred to as "object denial". The replacement that happened is from a dedicated domain object Patient
into a generic Pair<String, String>
.
So you are asking about the difference between List<Pair<String,String>>
and a Map<String,String>
. The former contains more information because it maintains the insertion order of the individual pairs and also allows random access based on position in the list.
A Map
, on the other hand, allows efficient lookup based on the key, which is what is really needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With