Why stream() doesn't work from just declared Array [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
It's probably sth simple, but I can't find answers why I can't start stream straight after creating List using Arrays.asList. 'list' is working fine, but 'list2' doesn't, no help from IntelliJ
List<Book> list = Arrays.asList(lalka,dziady,chlopi,jutrzenka);
list.stream()
.map((Book var)-> var.getAuthor().getName())
.forEach(var-> System.out.println(var));
List<Book> list2 = Arrays.asList(lalka, dziady).stream()
.map((Book var) -> var.getAuthor().getName())
.forEach(var-> System.out.println(var));

Your stream pipeline doesn't return anything (the terminal operation forEach has a void return type) so you can't assign it to a List variable.
You can write:
Arrays.asList(lalka, dziady)
.stream()
.map((Book var) -> var.getAuthor().getName())
.forEach(var-> System.out.println(var));

You're trying to assign result of .forEach to a List<Book> list2 variable.
.forEach(...) is of type void.
Remove this unnecessary assignment.

The problem is with the assignment to List list2.
You'll probably be getting an error that you cannot convert void to list
as .forEach() does not return anything.
Remove the assignment to list2 and this compiles and runs just fine.

Related

list.stream().collect(Collectors.toList()); returns empty list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
choices is a List of two elements
but choices.stream().collect(Collectors.toList()); returns an empty list
Would anyone know why?
//returns poll with list of choices
public Poll accessPoll(String pollId) {
return pollRepository.findById(pollId).orElseThrow(
() -> new IllegalStateException(String.format("No poll found for the ID: %s.", upperCasePollId)));
}
List<Choice> choices = pollManager.accessPoll(pollId).getChoices(); //returns list of choices
List<Choice> choices1 = pollManager.accessPoll(pollId).getChoices()
.stream().collect(Collectors.toList()); //returns empty list
Look carefully at your screenshots. Your method getChoices() returns not a regular list but IndirectList which extends not a regular Collection but a Vector and that is why streams don't work as expected. This is a known bug in EclipseLink,
you can read about it more here and here.
To overcome this behaviour, you can try to update your EclipseLink version up to 2.6.0, or you may try to wrap it with a new collection, like new ArrayList<>()

How to iterate through nested java collections using lambda [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I read through several threads about operating on the nested java collections using Lambda but none addressed my specific situation, although this one came close and then took off to a different direction (flatMap). Please show me how to write the following code in Lambda.
for(AppUser user : users){
List<CustomerOrder> orders = user.getOrders();
for(CustomerOrder order : orders){
order.setConsumer(user);
List<LineItem> items = order.getLineItems();
for (LineItem item : items){
item.setOrder(order);
}
}
}
Thanks
If all you want to do is iterate, the forEach method on either the Stream.java or the Iterable.java should be enough. Here is the "streamy" way of doing what you are trying to do with the for loops
users.stream().forEach( user ->
user.getOrders().stream().forEach( order -> {
order.setConsumer(user);
order.getLineItems().stream().forEach(
lineItem -> lineItem.setOrder(order)
);
}
));
Although you don't really need to convert the iterable returned by by those getters to streams.
This is a solution I came up with but I see that Smarth Ktyal already posted an answer.
users
.stream()
.forEach(e->e.getOrders()
.stream()
.forEach(k->{
k.setConsumer(e);
k.getLineItems()
.stream()
.forEach(l->l.setOrder(k));
}
)
);

Unreachable If Statements with Remove Function for Singly Linked Lists [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to create my own singly linked list in Java and am running into trouble when writing my remove function (which will take the desired node out of the list). 'hasNext' is a boolean that returns true if there is a node after something. The error I'm getting is that the if statements I have are unreachable. Any idea how to go about fixing this?
Change
if (hasNext == false) // If you're removing the final value
to
if (hasNext() == false) // If you're removing the final value
Since you have no code that sets hasNext to false you can't get into that if block.

Counting the repetition of an element of an array java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to count the frequency of an element in arrays.
I used the method
Collections.frequency(Arrays.asList(arr),element);
but I get zero all the times
any ideas ?!
If you are ArrayList consists of elements of custom type
example person bean, or employee object.
Make sure you have overridden equals() method and hash() methods
if you have not overridden these methods that Collection method wont work.
You need to give details about "arr" & element. However, I did came across this some time back when I tried to use an array of primitives such as int[], converting them to a List using Arrays.asList()
There is nothing like List of "int". An Integer would work however, Integer arr[] = {1,1,1,1,3,3,4,5,5,5,6};

Count number of occurrences in various lists Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've got in Java a map of this type
Map<Group, List<Person>>
that is a set of groups with the whole list of members.
I want to find the Person that is in the largest number of groups using streams and lambda expressions, I tried something but it wasn't successful.
Can you help me please? Thanks
What you need is .flatMap() followed by a .collect() which finds the frequency of each person in the overall Map.
Something like this:
Person socialButterfly = groupMap.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.max(Map.Entry.comparingByValue())
.get().getKey();
Ideone Tested

Categories