-3

So far i tried to add values from a stream to a list with peek() but later I found out that peek() is only used "to support debugging, where you want to see the elements as they flow past a certain point in a pipeline".

Now my question is whats the coding-convention here ?

Do I map it in a second stream or can I map it one String like my Code with Peek() ?

    final int range = 9;

    List <String> help = new ArrayList<String>();

    //random numbers to fill help
    for(int i = 5;i< range;i++)
    {
        help.add(String.valueOf(i+(i*2)+(i*(i+2))) );
    }


    List<Test> others = new LinkedList<>();

    List<Test> tests = help.stream().map(s-> new Test(s,(int) Integer.valueOf("10")))
    .peek(t->System.out.println(t.getText()))
    .peek(t-> others.add(t)).collect(Collectors.toList());

The class Test looks like this:

public class Test
{
    String text;
    int id;

    public Test(String text, int id) {
        this.text = text;
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public int getId() {
        return id;
    }
}
TSH
  • 97
  • 9
  • Can you please note how your case is different than this: https://stackoverflow.com/q/14830313/4636715 – vahdet Jan 30 '20 at 09:13
  • 2
    Do you want to collect the values in `others` as well as `tests`? What for is `others.add`? and regarding the title .. *Add a value of the stream to another List/Collection without using foreach or any terminal Operation* - but why? – Naman Jan 30 '20 at 09:15

1 Answers1

1

You could add that functionality to the lambda in the mapping part:

List<Test> tests = help.stream().map(
    s-> {
      Test t = new Test(s,(int) Integer.valueOf("10")));
      System.out.println(t.getText());
      others.add(t)
      return t;
    }
.collect(Collectors.toList());

That simply moves your extra steps into an existing step, avoiding any further loops etc

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • 1
    This is not better than what the OP already had in the question. Both are similarly discouraged techniques and both have the same number of loops. – Holger Jan 30 '20 at 09:43
  • I am fine with that. It avoids the peek. Feel free to improve this! – f1sh Jan 30 '20 at 09:51
  • 1
    “It avoids the peek” and avoiding peek is good because…? – Holger Jan 30 '20 at 10:02
  • If you read the question, OP hat an obvious problem with the usage of `peek`. And he/she accepted this answer, so it seems to meet some criteria. Again: If you hate this answer, feel free to post a better one and i will delete mine. – f1sh Jan 30 '20 at 10:44
  • 1
    I can not add an answer, besides that, it’s not clear why the OP wants to do something that is a clear abuse of the API. Performance? For `ArrayList`, `others.addAll(tests);` after the stream operation is *more efficient*. The OP’s problem with `peek` [has been described](https://stackoverflow.com/a/33636377/2711488), but perhaps not well understood. If you read the answer carefully, you will recognize that most things said about `peek` there, apply to abusing `map` for a similar purpose as well. There is no clean solution to the OP’s desire to add to a collection without a terminal operation. – Holger Jan 30 '20 at 12:03