Java stream how to add value in nested List of List

how to iterate nested list in java
java-stream nested list
nested list java
flatten nested list java
how to iterate list inside list in java 8
java flatten nested map
java flatten list of lists
java-stream<list inside list

I have a List<List<Object>> type data. I need to add the same value in the List list. I tried the following but it doesn't work as its type is List<Boolean>.

List<List<Object>> data = ...;
data.stream().map(v -> v.add("test")).collect(Collectors.toList());

How can I get in same type List<List<Object>> ?

I have the following data:

"data": [
        [
            5,
            "Johnny",
            "Lollobrigida"                
        ],
        [
            6,
            "Bette",
            "Nicholson"               
        ],
        [
            7,
            "Grace",
            "Mostel"                
        ],
        [
            8,
            "Matthew",
            "Johansson"                
        ]
     ]

I want to change it to:

"data": [
        [
            5,
            "Johnny",
            "Lollobrigida",
            "test"
        ],
        [
            6,
            "Bette",
            "Nicholson",
            "test"               
        ],
        [
            7,
            "Grace",
            "Mostel" ,
            "test"               
        ],
        [
            8,
            "Matthew",
            "Johansson",
            "test"                
        ]
     ]

@Boris the Spider is right : use forEach :

data.forEach(v -> v.add("test"));

stream creating List of List (nested List) using forEach, Java 8, Map<Long, List<String>> resultSet = listEntityComposite.stream() utilises merge to compute the key (i.e. firstId ) and value (i.e. List<String> ) you would have to create some logic to achieve this with good performance (or� Converting or transforming a List and Array Objects in Java is a common task when programming. In the tutorial, We show how to do the task with lots of Java examples code by 2 approaches: Using Traditional Solution with basic Looping Using a powerful API – Java 8 Stream Map Now let’s do details with … Continue reading "How to use Java 8 Stream Map Examples with a List or Array"

List.add() returns a boolean, but you want your map() to return the List to which you added the new element.

You need:

List<List<Object>> out = 
    data.stream()
        .map(v -> {v.add("test"); return v;})
        .collect(Collectors.toList());

Note that if you don't want to mutate the original inner Lists, you can create a copy of them:

List<List<Object>> out = 
    data.stream()
        .map(v -> {
            List<Object> l = new ArrayList<>(v);
            l.add("test"); 
            return l;
        })
        .collect(Collectors.toList());

Flattening Nested Collections in Java, Flattening the List With forEach. In order to flatten this nested collection into a list of strings, we can use forEach together with a Java 8 method� As of Java 8, we can also use the Stream API to find an element in a List. To find an element matching specific criteria in a given list, we: invoke stream() on the list; call the filter() method with a proper Predicate

You need to return the list from map after adding the element. Below solution should work for you:

List<List<Object>> dataModified = data.stream().map(v -> {
    v.add("test");
    return v;
}).collect(Collectors.toList());

Flattening a List of List to a List with Java 8 stream API, java-stream nested list get list convert list of list into single list java 8 new ArrayList<>(); for (List<String> value: listOfListValues) { listOfValues.add(String. It just does not match your expectations. In all six examples, we are going to filter a list of persons. Java 8 nested streams : return a value in last stream. Something like a nested for loop. ; employeeList is a static list of 8 Employees. Nested for loop as a stream (Features new in Java 8 forum at Coderanch) FAQs.

stream creating List of List (nested List) using forEach, Java 8, stream creating List of List (nested List) using forEach, Java 8 Stream forEach( Consumer action) performs an action for each element of the stream. to iterate over the stream of name lists and add How to group List of Lists using a certain� The problem is that the first map doesn’t return a Stream<Item> but actually a Stream<List<Item>>. In order to actually merge/flatten all those nested lists, you can use Stream#flatMap . The difference is quite simple. #map allows you to transform an element in the stream into another element.

Python List of Lists – A Helpful Illustrated Guide to Nested Lists in , square bracket notation. For example, to create a list of lists of integer values, use [[1, 2], [3, 4]] . Each list element of the outer list is a nested list itself. the list of lists. lst = [[1, 2, 3], [4, 5], [6, 7, 8]] Bob 'Java Dev', 99000],. Ann 'Python Dev'� Learn Java on Your Own in 20 Days - Free! What is a Webcrawler and where is it used? How to iterate over a 2D list (list of lists) in Java; Average of Cubes of first N natural numbers; Sum of all Palindrome Numbers present in a Linked list; Java program to print Even length words in a String; Difference between Compile-time and Run-time

Java, Stream API's flatMap operation can create a flattened stream of elements If we use the map method, we will get a stream of name lists, not a� A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Comments
  • what's wrong with list.add?
  • Why no data.forEach(l -> l.add("test"))?
  • I can't get what you want to do. Ahhh okay, then don't use map.
  • You are mutating the inside lists, why do you need anything back? Just use forEach.
  • @Seelenvirtuose don’t use peek for that; that’s nowhere better than map. Neither is meant to be used to manipulate the source of the Stream.
  • I don't get why this has been tackled so complex when the answer was just to use a forEach. There is no need in creating a stream even here :)
  • Right, always something more to remove ^^