This question already has answers here:
How to Maintain order of insertion [duplicate]
(3 answers)
Closed 5 years ago.
I am initializing a map with a bunch of values. The order of the values is important, and when I do myMap.values(), I'd like them to all come out in the order they went in. What data structure should I be using for that?
Map<String, String> myMap = new SomeKindOfMap<String, String>();
myMap.put(key1, value1)
myMap.put(key2, value2)
//etc
You're looking for a LinkedHashMap. Pro tip: If you go to the JavaDoc for an interface like Map, there's a "All Known Implementing Classes" section where you can see a list of all implementations in the JDK, and see if any meet your needs...
Related
This question already has answers here:
Java Class that implements Map and keeps insertion order?
(8 answers)
Closed 7 years ago.
I'm using a HashMap. When I iterate over the map, the data is returned in (often the same) random order. But the data was inserted in a specific order, and I need to preserve the insertion order. How can I do this?
LinkedHashMap is precisely what you're looking for.
It is exactly like HashMap, except that when you iterate over it, it presents the items in the insertion order.
HashMap is unordered per the second line of the documentation:
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
Perhaps you can do as aix suggests and use a LinkedHashMap, or another ordered collection. Take a look on javapractices.com's guide on Choosing the right Collection.
This question already has answers here:
HashMap: One Key, multiple Values
(15 answers)
Closed 4 years ago.
I would like to create an object with Map<String,Map<Integer, String>> the inner should not be a Map type because the inner key (Integer) is not a primary key (unique). And as far as I know when it comes to Map if there is a similar key value it will override the previous similar data.
What should be the datatype of my inner Map?
It depends on what you are going to do with the map. If the values of the outer map are just pairs, you can use Map<String, Set<ClassContainingIntAndString>>, or if you already know what it is (like you said there are only three values?) Map<String, SomeClassThatMakesSense>. However, if you want fast access to the final Strings given the first and second Integer, you should use Map<String, Map<Integer, List<String>>> (or something similar except encapsulated in some user-defined classes, as it may be bad practice to have too many nested generics).
This question already has answers here:
What is the difference between the HashMap and Map objects in Java?
(13 answers)
What does it mean to "program to an interface"?
(33 answers)
Closed 5 years ago.
I always initialize map like Map<String,Object> hsmap = new Hashmap<>()
(Assuming hashmap key is a String and value can be any object)
instead of
HashMap <String,Object> hsmap = new Hashmap<>()
As a best practice ,though I'm sure that I will never change this map implementation apart from hashmap for my requirement.
Is ther any other reason to initialize with 1st approach apart from being a best practice to code to interface ?
This question already has answers here:
What does <> mean for java generics?
(2 answers)
Closed 7 years ago.
I've see that since java 1.5 or maybe a later version, you can initialize java collection leaving generics blank, i.e. using <> instead of writing out the whole < A,B >. but I can't find the official documents on this, and I'm wondering whether this has any benefits (or maybe I'm not remembering this correctly, in which case do point out the correct form). Thank you.!
It's called the diamond operator. It was introduced in Java 1.7.
The benefit is just that you need to write less code.
Compare
List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
to
List<Map<String, Integer>> list = new ArrayList<>();
What you're looking for is called the diamond operator. It was introduced in Java 7.
This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 9 years ago.
What's the difference between the following two declaration statements:
HashMap<Character, Character> map = new HashMap<Character, Character>();
Map<Character, Character> map = new HashMap<Character, Character>();
Any advantages of using the interface Map instead of HashMap in this particular case?
In the following case, is Map definitely better because the method can accept different types of maps?(if it is intended to)
public void method(Map map){
}
There is no underlying difference. It is more about the interface. There's an advantage of using a Map though, that is you can change the object to be a different kind of a Map without breaking the contract of the code using it.
The HashMap is an implementation of Map, which is part of the Java Collections Framework. If you settle on using the HashMap and then the other party wishes for something different, like LinkedHashMap (preserves iteration order), then you have to change things around. Here's a diagram (courtesy ProgramCreek).
There are other things like computational time complexity, if you care about performance. Here's a small table that helps. Choosing the right thing is a question of design and need i.e. what are you trying to do. It varies from project to project.
The second version is preferred because if you want to write code later to change map to a different kind of Map, you will need to use the second version. But it really is a matter of personal preference.
in the perspective of the object-oriented,During compilation,
the method is bound reference class type,
so HashMap map = new HashMap();
You can us hashMap methods ,Including the realization map and extended. But ,
Map map = new HashMap();
You can only use methods declared in map .Can not used hashMap methods.