Map (hashmap) with inner Map (3 values) [duplicate] - java

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).

Related

java maintain insertion order in map [duplicate]

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...

Sort Map By Value Object Member [duplicate]

This question already has answers here:
Sort a Map<Key, Value> by values
(64 answers)
Closed 8 years ago.
I have a generic Map which is defined thus
Map<Integer, Book> bookCollection
The Integer is the book ID and the Book is obviously a Book object.
The Book object has a member called publicationYear.
How do I sort the Map so that its Book objects are in order by the Book's publicationYear member. So, if I iterate through the sorted map, the oldest book appears first up to the newest book.
At the moment, the Map is randomly sorted.
the publicationYear has to be part of the key. to sort based on that.
One solution to this sort of problem is to write your own data structure that uses a two or more standard data structures to implement the messy details.
For example, you could use a HashMap<Integer, Book> to allow rapid look-up by Id. You could also have a TreeSet<Book> in the required order. If it is not the case that Book is Comparable with the key you need for this, use a Comparator<Book> to create the TreeSet.
Most methods for the composite data structure will be two or three lines long. The fact that the two underlying structures exist and are being kept consistent is hidden from the rest of the program, which just accesses the composite structure.

Difference between using Map and HashMap as declared type [duplicate]

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.

Locate Objects in Collection with multiple keys [duplicate]

This question already has answers here:
How to implement a Map with multiple keys? [duplicate]
(27 answers)
Closed 9 years ago.
Have a class with attributes attr1, attr2, attr3 and attr4.
I am looking for a way to load a bunch of objects of that class in an array (or ArrayList or Map or whatever) and then being able to retrieve (search) these objects based on attr1 or attr2.
It looks like the way to go is HashMap but I will have to:
if I want only one HashMap, I will have two entries for each object, one with key attr1 and one with key attr2
have two HashMap objects, one with key attr1 and the other with key attr2 and based on what I am searching for, use the appropriate Map.
Is there any other elegant way of doing this? Is there a Map or Collection object that will allow me to provide multiple keys for an object?
I find the second solution with two Map objects quite elegant, each Map being a sort of index of your data. If you really want one single structure, then you can use Guava's Table, who is mapping values to a pair of keys.

whats the different between when i create object from map and hashmap [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java - HashMap vs Map objects
whats the different between
private Map<String, Integer> drawableMap = new HashMap<String, Integer>();
and
private HashMap<String, Integer> drawableMap = new HashMap<String, Integer>();
The type of the variable at the left-hand side of your assignment expression has nothing to do with object creation; therefore in both cases you are creating the exact same object. Since in Java you can only store a reference to an object into a variable, the type of that variable constrains the types of object the variable can refer to. In the first case it can refer to any object that implements Map; in the second, only HashMap objects are acceptable.
The other consequence is that in the first case you can only call methods of HashMap that are declared in the Map interface, whereas in the second case you can call any additional methods specific to the HashMap implementation.
In most real-world cases you will prefer the first case since you almost never need implementation-specific methods. The same rule goes for the complete Collections Framework.
In the first example, you can later assign drawableMap to other implementations of Map (e.g. LinkedHashMap). In the second example, you cannot - you are confined to HashMaps (and any of its subclasses). Generally, the first approach would be preferred over the second as it would provide greater flexibility down the road.
Ultimately, the first statement creates a variable of type Map that is an instance of HashMap. The second creates a variable of type HashMap that is also an instance of HashMap.

Categories