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

What is difference between core java and java ee?

0 Answers  


How to create an instance of a class without using "new" operator? Plz help me out properly.Thank u.

10 Answers   CSC,


How many types of thread in java? give the name

13 Answers   Cisco, NIIT,


How many times finalize method will be invoked? Who invokes finalize() method in java?

0 Answers  


if the memory capacity is 700 presently occupied by process is 690. then another process request space(40) how this situation handled in java.

4 Answers   Wipro,


What is an empty list in java?

0 Answers  


What are three advantages of using functions?

0 Answers  


When should I use stringbuffer?

0 Answers  


Howmany number of objects we can store in an ArrayList. Is there any limit?

7 Answers   TCS,


What is size_t?

0 Answers  


Does treeset use compareto?

0 Answers  


What’s the difference between applets and standalone program?

0 Answers  


Categories