How to use Java Streams to extract Specific Data Into Hashmap [duplicate] - java

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.

Related

Does java HashMap.entrySet() iterate through the HashMap in order of insertion? [duplicate]

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

Java ArrayList<String> to single string without using loop [duplicate]

This question already has answers here:
A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]
(15 answers)
Best way to convert an ArrayList to a string
(27 answers)
What's the simplest way to print a Java array?
(37 answers)
Java function for arrays like PHP's join()?
(24 answers)
Closed 3 years ago.
I have an ArrayList<String> that is added to periodically. What I want to do is to cast the entire ArrayList to a String without doing a loop. Can anybody tell me is it possible without using loop?
Edited:
So we found some solutions, Like
list.stream().collect(Collectors.joining());
Or
String result = String.join(",", list);
or some others as well. Now Just for getting knowledge I put a question which one is the most optimal way for compiler?
You could make a stream out of your list and collect it using joining collector :
list.stream().collect(Collectors.joining());
You can pass the separator to Collectors::joining method for example :
list.stream().collect(Collectors.joining("-"));
Or you can use String::join method :
String result = String.join(",", list);
where , is the separator.
I Believe you can do
String.join(", ", list);
Hope this helps :)
Given some ArrayList<String> s, all you need to use to get a String representation is s.toString().
The format is the following
[element1, element2, element3]

How to Sort Date in ArrayList in Java [duplicate]

This question already has answers here:
Sort an ArrayList based on an object field [duplicate]
(3 answers)
Closed 5 years ago.
I have a Map< String,ArrayList< BonusEntity > > Here key is EmpId of Employee and key will contains the List< BonusEntity >
BonusEntity will contains the following fields(All fields are String)
Empid
BonusDate
BonusAmount
Dept
I need to sort the ArrayList in Map based on the BonusDate descending order,Can anyone help Thanks in advance.
You should create a Comparator that applies to your class BonusEntity and use it via the Collections.sort method.

best performance method to sum collection of collections [duplicate]

This question already has answers here:
Java Stream API - count items of a nested list
(3 answers)
Closed 6 years ago.
Let's say I have a Collection named "cities"(class City) and each item in the collection has a member "streets" (class Street) and I want to iterate on all the cities and count the total number of streets in the collection.
what is the best way to do it using Streams while avoiding as many unnecessary operations (un\boxing, redundant calculations etc.)
int streetCount = cities
.stream()
.map(City::getStreets)
.mapToInt(Collection::size)
.sum();

How to sort a map on map values [duplicate]

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.

Categories

Resources