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

How can you set an applet’s height and width as a percentage?

0 Answers  


What is the meaning of I ++ in java?

0 Answers  


Is it safe to install java on my computer?

0 Answers  


What is the difference between java and .Net?

39 Answers   Bosch,


What are other modifiers?

2 Answers   Wipro,






Is Cegonsoft Pvt.Ltd. a good Institute?

4 Answers  


my method "abc" return array of interface "xyz" and "pqr" is abstract class implements abc and class "jkl" extends pqr My problem 1) when i call abc it retrun array xyz how can i do this hint xyz refer_xyz = new jkl(); but i can't create array. 2)I want to access method of jkl using reference of xyz??

1 Answers  


What is the difference between length and size in java?

0 Answers  


What are internal and external variables?

0 Answers  


what is the collable collections in java?

2 Answers   Persistent,


What is protected in java?

0 Answers  


What is the use of set in java?

0 Answers  


Categories