This question already has answers here:
Sorting HashMap by values [duplicate]
(12 answers)
Closed 9 years ago.
I have a java Map
Map<String , String>
The map is like following
olah : 3
vola : 2
sola : 5
jingle : 9
i want to sort the map on the value string like sort on 3,2,5,9 for example...is there any efficient way possible.
I also want to know what difference it will make if i put a map with same values but
like
Map<String , long>
Does it improve any performance...?
See: Sort a Map<Key, Value> by values (Java)
Something like
Map<String , long>
is not possible because Java Collections can not store primitive types. If you want to do that, you can use fastutil http://fastutil.di.unimi.it/ or trove http://trove.starlight-systems.com/. Memory consumption will be better, since no Long objects are created.
Related
This question already has answers here:
How can I count occurrences with groupBy?
(6 answers)
Counting occurrences in a list with Java 8 [duplicate]
(1 answer)
Closed 1 year ago.
I have a record for Employees:
public record Employee(
String name,
String id,
string imm_supervisor,
){}
I have a stream of Employee objects, and I want to extract a hashmap that gives me a mapping between each imm_supervisor and the amount of employees that the supervisor directly supervises.
I'm frankly confused by documentation, and I've been getting errors not knowing how stream this properly. This is as close as I got:
Map<String, Integer> mp = streamWithEmployees.map(x->supervisorofEmployee(x)).collect(groupingBy(/*String*/, counting()));
supervisorofEmployee(employee) returns a string, imm_supervisor. I'm not sure how to grab the returned strings from the map and put them into the map.
Hope this makes sense.
This question already has answers here:
is the Java HashMap keySet() iteration order consistent?
(12 answers)
Closed 2 years ago.
While iterating through the HashMap of Integer to String, suppose I insert values in the following order
HashMap<Integer, String> hash_map = new HashMap<>();
hash_map.put(0, "check");
hash_map.put(1, "ok");
hash_map.put(2, "what");
hash_map.put(4, "why");
When I iterate through the HashMap, using hash_map.entrySet(), will I iterate through it in order of insertion? Or will it be a random order?
When you need a Map and you don't care about the order (when you iterate through it), then HashMap is the right choice.
If you want the order of insertion try LinkedHashMap
try reading this : https://www.w3resource.com/java-tutorial/java-maps.php
This question already has answers here:
Java associative-array
(15 answers)
Closed 2 years ago.
I'm beginner in Java environment, accustomed to PHP.
And in Php we can create an array like the code below :
$array['params1'] = 'the first param';
$array['params2'] = 'the second param';
And when i will output $array['params1'] it will be 'the first param'.
But i do not find any similar solutions in Java, do you know something similar ?
Thanks in advance
As #mrblewog said, you might want to readup on data structures and the syntax in Java as it is quite different than php.
To give you an Example:
// Key Value
HashMap<String, String> map = new HashMap<>();
map.insert("key1", "value1");
map.get("key1"); // returns "value1"
If you want to store other objects than Strings you will need to change the generic types (written in the <X, Y>).
This question already has answers here:
Is there a concise way to iterate over a stream with indices in Java 8?
(24 answers)
Closed 3 years ago.
I am trying to iterate over a list of maps in java, but I can not figure it out how. I need to keep the index as well.
for example, I have a list of maps like
List<Map<String, String>> ListOfMaps = new ArrayList<>();
and the output that I want is something like:
"ListOfMaps[index].map(k) => ListOfMaps[index].map(k).getValue()"
I appreciate if you can give me some hint how I can do it using java stream operators, with high performance.
As an advocate for streams, I personally don't like using them here. But I will anyhow:
IntStream.range(0, mapList.size())
.forEach(index -> {
Map<String, String> map = mapList.get(index);
System.out.println("ListOfMaps[" + index + "]" ...);
})
This question already has answers here:
Join two maps by key
(3 answers)
Closed 4 years ago.
I have two Maps, both sharing the same keys, .
Map<Long/*JOIN.ID*/, Long/*Temp ID*/> tempIDsMap;
Map<Long/*JOIN.ID*/, Long/*Real ID*/> realIDsMap;
What I want to get (maybe using Java 8 Stream API and avoiding loops) is the JOIN of these maps on the JOIN.ID keys, to get a new Map like below:
Map<Long/*Temp ID*/. Long/*Real ID*/> realIDsByTempMap;
Use Collectors.toMap:
Map<Long,Long> realIDsByTempMap =
tempIDsMap.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getValue,
e -> realIDsMap.get(e.getKey())));