Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How to sort the elements in HashMap

Answers were Sorted based on User's Feedback



How to sort the elements in HashMap ..

Answer / jyoti

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class format {
public static void main(String args[]) {
Map<String, Person> people = new
HashMap<String, Person>();
Person jim = new Person("Jim", 25);
Person scott = new Person("Scott", 28);
Person anna = new Person("Anna", 23);
people.put(jim.getName(), jim);
people.put(scott.getName(), scott);
people.put(anna.getName(), anna);
// not yet sorted
ArrayList<Person> peopleByAge = new
ArrayList<Person>(people.values());
Collections.sort(peopleByAge, new
Comparator<Person>()
{
public int compare(Person o1,
Person o2)
{
return o1.getAge() -
o2.getAge();
}
});
for (Person p : peopleByAge) {
System.out.println(p.getName()
+ "\t" + p.getAge());
}
}
}
class Person
{
String name = null;
int age;
Person()
{

}
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
}

Is This Answer Correct ?    16 Yes 0 No

How to sort the elements in HashMap ..

Answer / srinivasa

By implementing comparable interface we can sort the
HashMap

Is This Answer Correct ?    11 Yes 2 No

How to sort the elements in HashMap ..

Answer / srinivasa

we can use to Collections.syncroniseMap() to synchronize
the HashMap

Is This Answer Correct ?    7 Yes 12 No

Post New Answer

More Core Java Interview Questions

State the difference between strings and arrays.

0 Answers   Syntel, Visa,


What is the difference between superclass and subclass?

0 Answers  


For technical interview question please sir send me because tomorrow my interview

1 Answers   FactSet Systems,


Write a program to print fibonacci series up to count 10.

0 Answers  


Is string is a data type in java?

0 Answers  


real time example for deadlock,starvation,livelock

5 Answers  


how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion

2 Answers   SAP Labs,


What do bitwise operators do?

0 Answers  


What is dynamic dispatch in java?

4 Answers  


What are the four pillars of java?

0 Answers  


Can you achieve runtime polymorphism by data members?

0 Answers  


What is a substitution variable?

0 Answers  


Categories