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
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 |
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 |
How are observer and observable used in java programming?
What does next mean in java?
What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?
What is a treeset in java?
What is variable and rules of variable?
Why do we use public static with the main function in Java?
13 Answers College School Exams Tests, Infosys,
What is an arraylist in java?
what is the logic inside any default constuctor?
What are assertions in java?
While opening the file, what type of exceptions can be caught?
I want to print “hello” even before main is executed. How will you acheive that?
What is actual difference between statement,prepared statement and callable statement and when we have to use it? pls post a answer with code and clear explanation. thanks