createNewVal(s)).collect(Collectors.toList());} static public List getSpecialVals() {// but can return..." />

How to create a different List if Stream is empty

is there a nice way to create a different List if stream is empty?

Here if special is empty I want to create a new List with another value.

But if special is not empty I want to create List based on special.

I don't know is there a nice way to combine these with a stream

Here is a not so nice solution

class X

{

public static void main(String [] args)

{

String myString = "test";

List special = getSpecialVals();

List newVals = special.isEmpty() ?

Arrays.asList(myString) :

special.stream().map( s->createNewVal(s)).collect(Collectors.toList());

}

static public List getSpecialVals() {

// but can return...

Read More »

By: StackOverFlow - 5 days ago

Related Posts