Collection in java map and HashMap [duplicate] - java

This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 7 years ago.
I didn't understand the difference in the two below statement :-
HashMap<Integer,String> hashmap = new HashMap<Integer,String>();
Map<Integer,String> hashmap = new HashMap<Integer,String>();
The above two statement work in the same manner , no difference in the ouptput
OR
there is any time difference on running two statement.

Map<> is the interface which HashMap<> implements. See here for more info.

Related

Java 8: Map of field to list of items [duplicate]

This question already has answers here:
Java 8 lambdas group list into map
(2 answers)
Closed 5 years ago.
Lets say I have a List<Person> Gathering and I want a Map<String, List<Person>>, mapping Person.surname to a List of Person:s that have the same surname. Is there a convenient way to do this using streams?
Yes, by using Collectors.groupingBy(...):
Map<String, List<Person>> personsBySurname = gathering.stream()
.collect(Collectors.groupingBy(Person::get‌​Surname));

How to put value in HashMap? [duplicate]

This question already has answers here:
How to initialize an array in Java?
(11 answers)
Closed 6 years ago.
I have a Map
Map<String,String[]> data = HashMap<String,String[]>();
and want to put value in it. like
"key":["value1","value2"]
I try it but get error:
data.put("key",["value1","value2"]);//syntax error
You could try using the Array initializer for fixed size string arrays like following
Map<String, String[]> data = new HashMap<>();
data.put("key", new String[]{"val1", "val2"});
This should definitely work

What is a equivalent of HashSet Java in Swift? [duplicate]

This question already has answers here:
How to create array of unique object list in Swift
(11 answers)
Closed 8 years ago.
I have specific logic in Java that uses HashSet<String>. Set collection contains only unique items.
For example:
Set<String> mySets = new HashSet<String>();
mySets.add("a");
mySets.add("a");
mySets.add("b");
mySets.add("a");
I get: ["a","b"].
What is equivalent collection in Swift?
Thanks,
The Swift to Java's HashSet is Set.
Example:
var s = Set<Character>()
s.insert("a")
s.insert("a")
s.insert("b")
s.insert("a")
print("s: \(s)")
output:
s: ["b", "a"]
Official docs

Declaring ArrayLists: difference in Functionality? [duplicate]

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 8 years ago.
I'm curious, When declaring ArrayLists, What is the difference in doing this:
List<String> arrayList1= new ArrayList<String>();
and this:
List<String> arrayList2= new ArrayList<>();
i.e. not declaring the <String> twice?
the only difference is that the first form is compatible with earlier java versions than java 7.
And you don't need the <> either in latter versions. e.g
List<String> arrayList2= new ArrayList();

Retrieve values from hashmap (.values()) [duplicate]

This question already has answers here:
How do I convert a Map to List in Java?
(13 answers)
Closed 9 years ago.
I would be pleasure if you can suggest.
Could you suggest how properlly I can retriev all values from hashmap.
In such simplee example:
Map<String, Values> someMap = ....;
List<Values> valuesFromMap = (List<Values>) someMap.values();
After that I've got that, Java couldn't cast to java.util.List
Thank you in advanced.
The values method returns a Collection, not a List. Use
Collection<Values> valuesFromMap = someMap.values();
someMap.values() returns Collection. If you need to convert into List than do that as follows:
List<Values> valuesFromMap = new ArrayList<>(someMap.values());

Categories