Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Pair Class and why have the implemented here?

Tags:

java

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()]));
  1. Why have they implemented a Pair Class here? What could be the reason.
  2. What is the difference between a Pair Class and a HashMap.

Note: Both the code works perfectly.

like image 519
theJava Avatar asked Jun 03 '13 13:06

theJava


People also ask

How are Pair classes implemented in Java?

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.

Why do we use Pair in Java?

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.

Do we have Pair class in Java?

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.

Where is Pair in Java?

The pair class in Java is under the util package of the JavaFX package.


2 Answers

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)

like image 161
Dariusz Avatar answered Nov 02 '22 20:11

Dariusz


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.

like image 31
Marko Topolnik Avatar answered Nov 02 '22 20:11

Marko Topolnik