How to print set list in object with lambda

I have a class like this:

public class Subject extends Entitet{
   private String personalID;
   private String name;
   private Integer ECTS;
   private Profesor subjectBearer;
   private Set<Student> students;

//all getters and seters

In the main I have created a list that contains all the Subjects.

List<Subjet> subjets= new ArrayList<>();

And now I would like to print all Subjects name with all students that attend the classes.

I tried something like this but it gives me error :

The operator + is undefined for the argument type(s) String, void
    .forEach(subj-> System.out.println(subj.getSubjectBearer().getName()+ " " + subj.getSubjectBearer().getLastName()+" "+
                .forEach(stud->System.out.println(stud.getName()+" "+stud.getLastName()))

I don't know how to make that this second stream returns string of firstName and lastName of every student. Can I map it to string and then somehow collect it? Something like this inside the first stream?

           .map(stud->(stud.getFirstName()+ stud.getLastName() )

This is because forEach does not produce any results (returns void). So, your second forEach on students will not return anything while you expect something to be concatenated to the subject name.

You can either:

Return a string of all students
  .forEach(subj-> System.out.println(
      subj.getSubjectBearer().getName()+ " " + 
      subj.getSubjectBearer().getLastName()+" "+
            .map(stud->stud.getName()+" "+stud.getLastName())

Or print the name without concatenation:
    .forEach(subj-> {
       System.out.println(subj.getSubjectBearer().getName()+ " " + subj.getSubjectBearer().getLastName());
           .forEach(stud->System.out.println(stud.getName()+" "+stud.getLastName()))

How to print elements of a Stream in Java 8, Introduced in Java 8, the Stream API is used to process collections of objects. Program 2: Using Short hand lambda expression Hence the printing of List can be done easily using println() method. How to get ArrayList from Stream in Java 8 · Convert a Set to Stream in Java · Array to Stream in Java · Stream map() in  Print List value using Lambda Expression Print Map key-Value Pair using Lambda Expression Pay attention to forEach function that takes a reference to BiConsumer function interface and prints the key and value. This is so easy against the traditional way of getting an iterator of Map.Entry objects and print key and value for each Map.Entry object.

The + operator error is occurring since you are using this:-

subj.getSubjectBearer().getLastName()+" "+ subj.getStudents().stream().forEach(stud->System.out.println(stud.getName()+" "+stud.getLastName())) )

The + operator is working on a String and void, because the second part which is creating a stream of students and then working on it is not returning any String.

Set to List in Java, Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("​For"); Output : ArrayList or Linked List with following content {"Geeks", "for"}  Sometimes if you have a set of records in a List, it becomes quite easy to query on a list using a Lamda Expression. This article exemplifies methods for performing several tasks and queries over a list. A sample code is also attached with this article to explain the use of Lambda Expressions.

forEach method id is type of void. split your statement into two statements.
    .forEach(subj-> {System.out.println(subj.getSubjectBearer().getName()+ " " + subj.getSubjectBearer().getLastName());
                .forEach(stud->System.out.println(stud.getName()+" "+stud.getLastName()))

Java 8 Lambda Comparator example for Sorting List of Custom , We have already seen how to sort an Arraylist of custom Objects without using lambda expression. In this tutorial we will see how to sort a list of custom. In this example, we iterate over a list of strings with forEach(). This syntax can be shortened with Java lambda expression. Lambda expression. Lambda expressions are used primarily to define an inline implementation of a functional interface, i.e., an interface with a single method only. Lambda expression are created with the -> lambda operator.

In your question, you have some misspelling name in your attribute/method, but I guess it is not in your source code.

I think you should use System.out only once, and thus create the String presentation first, this way :

            .map(subj-> (subj.getName()+ " " + subj.getLastName() + " \n\t" +
                                    stud->stud.getFirstName() + " " + stud.getLastName()).collect(Collectors.joining(","))
            ).reduce("", String::concat)

You can adapt separators as you want.

Let me know if you need further explanations.

Java 8 – Stream Collectors Class with examples, Collectors is a final class that extends the Object class. In this tutorial we will see the examples of Java Stream collectors class using lambda. elements of a list using groupingBy() method of Collectors class and printing Set; import java.util. AWS Lambda context object in Python When Lambda runs your function, it passes a context object to the handler . This object provides methods and properties that provide information about the invocation, function, and execution environment.

You are close, try something like this:
    .forEach(subj-> System.out.println(subj.getSubjectBearer().getName() + " "
                    + subj.getSubjectBearer().getLastName() + " " + subj.getStudents()
                    .map(stud -> stud.getName() + " " + stud.getLastName())

Collection Transformation Operations, The Kotlin standard library provides a set of extension functions for collection function to each subsequent element and returns the list of the lambda results. another collection (array) as an argument, zip() returns the List of Pair objects. In List, Set, Map or anywhere else when we want to define our own sorting method, JVM will always call compare() method internally. When there is Functional Interface concept used, then we can use Lambda Expression at its place. Sorting elements of List(I) with Lambda Expression: –

Using a Lambda Expression Over a List in C#, Lambda expression Sample code is also attached. Email; Bookmark; Print; Other Artcile Sometimes if you have a set of records in a List, it becomes quite Now we create a list of the Person objects in which we have to  Therefore, the parameter to the lambda statement is an index object that has rank 1. At runtime, the lambda expression is executed once for each element in the array_view object. For more information, see Lambda Expression Syntax. Function Object. You can factor the accelerator code into a function object.

How to sort array, list, map and set in Java, Learn to sort array or collection in Java. we will learn to sort set, list and map of primitive types and custom objects in natural and The output of the parallelSort​() and sort(), both apis, will be same at last. Java sort objects using lambda  So when I used — list.sort(key=lambda r:r[0]) lambda r:r[0] is an anonymous function with a single argument, r which would in this case was a list, e.g.: [datetime.datetime(2016, 7, 10, 0, 57

Python: How to Sort a List? (The Right Way), So what about if you have a list of generic objects and you L.sort(key=lambda x​: print([ for  If we try to print without creating a list, the output will be something like <filter object at 0x7f3fb03ce7d0>. Conclusion I hope this introduction to lambda expressions helps take away some of

  • Did you try this solution?
  • Can I ask a good soul to format the code for me. I am from the phone and I am unable to do it. :)