Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
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 -> ...
l a s's user avatar
  • 3,915
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 { ...
sairajgemini's user avatar
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) ...
Thilo _'s user avatar
  • 13
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> ...
cnmdestroyer's user avatar
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....
Victoria Antolucci's user avatar
-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 : ...
nanosoft's user avatar
  • 3,081
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", ...
Raoul Duke's user avatar
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 ...
Sumantra Chakraborty's user avatar
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", "...
Clem's user avatar
  • 3
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 ...
Bekz's user avatar
  • 3
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 ...
de5tro's user avatar
  • 33
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 ...
antnewbee's user avatar
  • 1,899
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 ...
artificialdeathinthewest's user avatar
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 =...
Scientist's user avatar
  • 1,464
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. ...
OptimusPrime98's user avatar

15 30 50 per page
1
2 3 4 5
96