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 ?
Related
This question already has answers here:
Type List vs type ArrayList in Java [duplicate]
(15 answers)
Closed 2 years ago.
What is the difference between
List<String> lst = new LinkedList<>();
and
LinkedList<String> lst = new LinkedList<>();
There's no difference other than convenience - in the first case you can change LinkedList to other List implementation (such as ArrayList), if you find that it works better in your application, for example, without the need to change any other code.
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:
Generic array creation error
(5 answers)
Closed 5 years ago.
I've declared a variable as:
LinkedHashMap<Integer, String>[] function_labels;
but when I try and instantiate it with:
function_labels = new LinkedHashMap<Integer, String>[2];
I get a 'generic array creation' error.
I've searched on here, and although there are many posts about this error message,
no-one seems to have offered a solution which actually works, so I'm trying again...
I don't mind what type of collection function_labels is, as long as it works and I can access indexed members of it later. A simple array seems the 'lightest' solution, but there may be others which will work.
Thanks
you have to cast type to declare like this Generic Array Creation
function_labels = ( LinkedHashMap<Integer, String>[]) new LinkedHashMap<?,?>[2];
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...
This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 9 years ago.
Say I have a variable test with the value example. I want to make a new variable that is called the value of test, so the new variable would be called example.
Is this possible? If so, how?
No, this is not possible in Java. Variable names cannot be set at runtime. However, you can mimic this using a Map that maps String identifiers to values.
String test = "example";
...
Map<String, String> vars = new HashMap<String, String>();
vars.put(test, "...");