All Questions
Tagged with lambda java-stream
1,434
questions
0
votes
2
answers
195
views
Predicates and functions
public static void main(String[] args) {
List<String> strings = Arrays.asList("a", "b", "c", "d");
strings.stream()
.filter(s -> ...
2
votes
2
answers
82
views
Compare two different java collection objects with a common attribute using java streams api
I have two Sets containing 2 different objects
1. Set<A>
2. Set<B>
class A {
private String id;
private String name;
private B objB;
}
class B {
...
0
votes
1
answer
210
views
Java stream().filter() cannot find local variable with lamba expression
I want to get the amount of a specific product that is in all warehouses. Warehouses contain a list of products. .filter doenst find the local variable "p" (https://i.sstatic.net/ml1zC.png)
...
0
votes
2
answers
123
views
Using stream for Java Enum to generate map
I have a Enum class
public enum MyEnum {
A("A"),
B("B");
private final String key;
}
And I want to generate a map like this:
private Map<MyEnum, MyClass> ...
2
votes
3
answers
93
views
Two objects supplier
I have a binary function:
public Integer binaryFunction(Integer a, Integer b){
//some stuff
}
And I have this stream:
Integer fixedValue = ...
List<Integer> otherValues = ...
otherValues....
-1
votes
1
answer
107
views
Convert nested for loop to stream in java 11
Convert to Streams:
private static List<Location> extractLocation(List<Location> locations){
List<Location> locationList = new ArrayList<>();
for(Location l : ...
0
votes
1
answer
146
views
How to convert a List<String> to Map<String, Long> using Lambda, where the key is the String and the value is a count of the number of vowels?
Given:
List<String> myStrings = Arrays.asList("broom", "monsoon");
Return:
Map<String, Long> stringToNumberOfVowels = Map.of("broom", 2, "monsoon", ...
1
vote
1
answer
107
views
Java Stream distinctByKey filter with variable arguments
I have a requirement where I need to get distinct elements from a list and the distinct key fields are variable. I was trying to pass variable argument in distinctByKey but it's not working. Here's ...
0
votes
2
answers
120
views
Stream.max() on a set of elements from a stream
I have a java List of CodeObj. CodeObj has 2 attributes : codeName and version.
My java List looks like this when mapped to a JSON object :
[
{ "codeName": "test1", "...
0
votes
1
answer
60
views
How to use an if statement in a Java stream to avoid a duplicate stream process
Here is my code from the Java MOOC.fi course where I am learning about streams. The code below works but the way I wrote it is quite amateur. I was thinking of somehow integrating an if statement in ...
1
vote
2
answers
160
views
Chaining Optional and Stream in Java(preferably 11)
I have a list of Foo objects, where each Foo contains a name and a List<Strings>.
I am trying to
find if the fooList contains a Foo object with a target name, and if such element exists,
find ...
0
votes
2
answers
93
views
Converting a java for loop to stream is asking to handle exception again
I have a for loop as below which is working fine.
try {
Accountant accountant = getAccountant(employees);
for (Employee person : employees) {
accountant.pay(person, this); // throws ...
1
vote
2
answers
493
views
Filtering list of Objects on condition while counting on different condition
Let's say I have a list of Objects that I would like to filter on condition (x && y) but I'd also want to count the occurences of a different condition while it's streaming, let's say (!x and ...
0
votes
1
answer
53
views
java streams executed a method limited number of times
I am writing a code in old style as follows :
public List<Long> generateSequenceList(final int count) {
List<Long> sequenceList = new ArrayList<>(count);
for (int i =...
0
votes
2
answers
56
views
How to rewrite a Java for loop which returns a matching element using streams [closed]
I would like to optymalize my method.
I have list of objects and I would like to iter by them, compare fields with input and in case of match I would like to use that first object in other method. ...