ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Core Java  >>  Java J2EE  >>  Java Related
 
 


 

 
 Core Java interview questions  Core Java Interview Questions
 Advanced Java interview questions  Advanced Java Interview Questions
 Swing interview questions  Swing Interview Questions
 EJB interview questions  EJB Interview Questions
 Servlets interview questions  Servlets Interview Questions
 Struts interview questions  Struts Interview Questions
 JDBC interview questions  JDBC Interview Questions
 JMS interview questions  JMS Interview Questions
 SunOne interview questions  SunOne Interview Questions
 J2EE interview questions  J2EE Interview Questions
 Weblogic interview questions  Weblogic Interview Questions
 Websphere interview questions  Websphere Interview Questions
 Java Networking interview questions  Java Networking Interview Questions
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions
Question
How to sort the elements in HashMap
 Question Submitted By :: Srinivasa
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How to sort the elements in HashMap
Answer
# 1
we can use to Collections.syncroniseMap() to synchronize 
the HashMap
 
Is This Answer Correct ?    1 Yes 4 No
Srinivasa
 
  Re: How to sort the elements in HashMap
Answer
# 2
By implementing  comparable interface we can sort the 
HashMap
 
Is This Answer Correct ?    1 Yes 2 No
Srinivasa
 
 
 
  Re: How to sort the elements in HashMap
Answer
# 3
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 ?    1 Yes 0 No
Jyoti
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
JSP is by default thread safe or not? what is the meaning of isThreadSafe="true" and isThreadSafe="false". Explain it? Thanks, Seenu Huawei3
What Method and class used for Connection pooling ? Wipro4
How you can create a radio buttion using Checkbox class?  1
if i have one string class then how can you achive this class functionality of this class? Fidelity3
when to use abstract class and when to use interface? IonIdea5
Nullpointer exception is a very common exception. Why is it not made as a checked exception? Ness-Technologies1
what is mean by overriding in which situation we wil use? Atlas-Systems4
what is d difference between deep cloning and shallow cloning in core java? Satyam2
what is meta-Inf? Polaris2
What is audio clip interface? Name few methods of it ?  1
What is the difference between getCodeBase and getDocumentBase methods?  1
What is the difference between the >> and >>> operators?  1
I have a Arraylist object, it has duplecate values also. Now question is i want delete duplecate data in that objet with out using Set? Aricent5
While opening the file, what type of exceptions can be caught?  3
Why do inner class cannot have static declaration except static nested class? Wipro1
There are 2 classes, 1 LandAnimal and another WaterAnimal. There is another class Animal which wants to have the properties of both LandAnimal and WaterAnimal. How will you design this situation? KPIT3
In what types of operations an ArithmeticException will be thrown?  2
What happens if an exception is not caught?  4
what is the difference between pagecontext and servletcontext? JBA-Infotech4
What is difference between Iterator and for loop  4
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com