best performance method to sum collection of collections [duplicate] - java

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();

Related

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

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.

Is there a way to use streams to return value from doing operations on elements [duplicate]

This question already has answers here:
How to sum a list of integers with java streams?
(12 answers)
Is mapToDouble() really necessary for summing a List<Double> with Java 8 streams?
(3 answers)
Closed 1 year ago.
I have a Train class, with a List<TrainCar> containing objects from a TrainCar class.
I have a function in Train called getTotalWeight(). Each TrainCar has a function called getWeight() which returns a value.
Can I use streams in some way to make the body of the getTotalWeight() function looking like this?
return cars.stream().//some function//;
You could use mapToDouble to get a stream of the weights and then sum them:
return cars.stream().mapToDouble(TrainCar::getWeight).sum();

how to split a list into a given number of sub-lists? [duplicate]

This question already has answers here:
Is there a common Java utility to break a list into batches?
(21 answers)
Closed 6 years ago.
I have a list that may be over 1000 strings, however I do not know how many exactly.
What is the best way to split this list into smaller lists without loosing any members of the list?
For example If I have a list of 1323 members, how can I best split it into 3 almost evenly sized lists?
I have seen the Guava and Commons way of splitting lists by the partition function, but that function will split the list into given size of chunks and not given number of groups (sub-lists).
Guava has a function Lists.partition which will do this for you
Usage:
Lists.partition(mylist, mylist.size()/3);

Need to Remove From List at Specified Multiple Indices, without new-size problems [duplicate]

This question already has answers here:
Remove multiple elements from ArrayList
(16 answers)
Closed 7 years ago.
I have an array of indices at which elements must be removed from a List, e.g.: (4), (7), (8).
The problem is:
1) Can't use a For-loop, the size changes after each delete (ArrayList.remove(i))
2) Can't use an Iterator with an updated counter, the counter will also not work anymore (Iterator.remove()).
Found a nice solution here: Remove multiple elements from ArrayList
Collections.sort(indices, Collections.reverseOrder());
for (int i : indices)
strs.remove(i);
That seems to be the only fast and simple solution. Reverse-sort first, then go through the indices and remove.

Dividing an array list into two equal parts [duplicate]

This question already has answers here:
How to split array list into equal parts?
(9 answers)
Split array into two parts without for loop in java
(5 answers)
Closed 8 years ago.
I was wondering if there is a way (simple way hopefully) to take the elements of an array list and put it into 2 other array lists. If the original array list contained ten numbers, is there a way to split the array list in half giving 5 numbers to each new array list? Thanks for your help!
You could use this method:
newArrList = arrayYouWantToSplit.subList(int fromIndex, int toIndex);

Categories

Resources