I have an HashMap object, which has with key and value pair.
It has 10 keys and values in that object. Now the question
is I want insert new key and value in middle or any where in
that list but not at the end or at the top. Is it possible
or not. If yes how can we achieve this one?

Answers were Sorted based on User's Feedback



I have an HashMap object, which has with key and value pair. It has 10 keys and values in that obje..

Answer / alka

It is not in the programmers hand to decide location of any
key/value pair in hashmap.These pairs are randomly placed in
Hashmap based on some calculated hash codes.

Is This Answer Correct ?    20 Yes 0 No

I have an HashMap object, which has with key and value pair. It has 10 keys and values in that obje..

Answer / manoj

package manu;

import java.util.HashMap;



public class Manu_HashMapTest
{
public static void main(String[] args)
{
HashMap<String,Integer> m=new HashMap<String,Integer>();
m.put("a",100);
m.put("b",200);
m.put("c", 300);
m.put("b", 800);//insert a new element....
System.out.println(m);
}
}

o/p->{a=100, c=300, b=800}
so Hash map does not give guarantee in order,so its not
possible to insert element in right position.

Manoj sahu,jajpur

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More Core Java Interview Questions

Difference between string, string builder, and string buffer?

0 Answers  


Can we nested try statements in java?

0 Answers  


If an object reference is set to null, will the garbage collector immediately free the memory held by that object?

0 Answers  


What are the types of methods in java?

0 Answers  


What is number data type?

0 Answers  






Explain the selection sort algorithm?

0 Answers   Hexaware,


What are different types of classloaders?

0 Answers  


What is length in java?

0 Answers  


Can an interface have a constructor?

0 Answers  


What is the difference between hashmap and hashtable in java?

0 Answers  


What is the purpose of stub and skeleton?

0 Answers  


11. static class A { 12. void process() throws Exception { throw new Exception(); } 13. } 14. static class B extends A { 15. void process() { System.out.println(”B”); } 16. } 17. public static void main(String[] args) { 18. new B().process(); 19. } What is the result? 1 B 2 The code runs with no output. 3 Compilation fails because of an error in line 12. 4 Compilation fails because of an error in line 15.

6 Answers  


Categories