2

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"                
        ]
     ]
6
  • what's wrong with list.add? Commented Mar 21, 2019 at 14:02
  • 5
    Why no data.forEach(l -> l.add("test"))? Commented Mar 21, 2019 at 14:03
  • I can't get what you want to do. Ahhh okay, then don't use map. Commented Mar 21, 2019 at 14:03
  • You are mutating the inside lists, why do you need anything back? Just use forEach. Commented Mar 21, 2019 at 14:03
  • 2
    @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. Commented Mar 21, 2019 at 15:13

3 Answers 3

5

@Boris the Spider is right : use forEach :

data.forEach(v -> v.add("test"));
Sign up to request clarification or add additional context in comments.

2 Comments

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 ^^
2

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());

Comments

2

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());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.