-3

What i'm trying to accomplish is to run a method in my stream's map, based in that if the return is correct, to add it to the success list which would go in the collection's map key, if failure, it would be added to the failure list which would go in the collection's value:

        List<String> success = new ArrayList<>();
        List<String> failure = new ArrayList<>();
        Map<List<String>,List<String>> masterLista = new HashMap<>();
        masterLista.put(success, failure);

        masterLista = list.stream().map(l -> {
            String newS=serviceApi.getClearSongName(l);
            if(newS!=null){    
                //Here the return should update the sucess list
                return serviceApi.getClearSongName(l);
            } else{
                //Here the return should update the failure list
                return l;
            }
        }).collect(Collectors.toList());

Actually the collection's map is not something necessary, the only thing i'd like to have is to inside the Stream's map already update two lists with successes and failures to avoid having to do a further processing inside a single list.

Edit: I'm using stream because actually the goal is to use parallelStream

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
sylleryum
  • 130
  • 1
  • 9

2 Answers2

3

You don't need a Map and you don't even need a Stream for this purpose. A simple for loop will do the work, or a List::forEach if you prefer

For loop

for (String str : list) {
    String newS = serviceApi.getClearSongName(str);
    if (newS != null)
        success.add(newS);
    else
        failure.add(newS);
}

Foreach

list.forEach(str -> {
    String newS = serviceApi.getClearSongName(str);
    if (newS != null)
        success.add(newS);
    else
        failure.add(newS);
});

PartitioningBy

You can also use the Collector::partitioningBy which returns a Map<Boolean, List<String>>

Map<Boolean, List<String>> map = 
    list.stream()
        .collect(Collectors.partitioningBy(str -> serviceApi.getClearSongName(str) != null));

map.get(true) will return the ones that classify ass success in your logic

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

I am guessing you need to stream a list, call an api and gather the success and failures separately. Below is a small snippet on how this can be done (Its in groovy not java)

def a = [1,2,3,4,5,6,7,8,9]
Map<Boolean,List> x = a.stream().map{it -> new Tuple2<Boolean,Integer>(it>5, it)}.collect(Collectors.partitioningBy{i -> i[0]})
println(x)

Collectors.partitionBy splits the stream into two groups based on a predicate(condition). If you need to split into multiple groups, use the Collectors.groupingBy() method. The below link has more details on this specific use case Can you split a stream into two streams?

Do pay heed to the 'cannot work on infinite streams' warning

Your code would look something like below

Map<Boolean,List> masterLista = list.stream()
.map(l -> {
                String newS=serviceApi.getClearSongName(l)
                return new Tuple2<Boolean,String>(newS!=null, l)

}).collect(Collectors.partitioningBy{i -> i[0]})
Arpan Kanthal
  • 475
  • 2
  • 7