#Java Streams but with a Map!

1 messages ยท Page 1 of 1 (latest)

distant trail
#

Hello ๐Ÿ™‚

trim adderBOT
#

Helper please have a look, thanks.

distant trail
#

So in Java you have forEach so I have the following (Sorry I said streams)

#

Is there anyway to simplify this and put it into a Map?

young thistle
#

You can use a collector to collect to a map

distant trail
#

I saw this using somehting like a HashMap but unsure if I have to just double loop it anyway

#

and then loop the Map

#

I don't think u can directly set them to a map

distant trail
#

Hello

mental lichen
#

i think the way you mentioned is already simplified

#

your purpose is to loop through a collection

#

and in each element, you call a field (which is a collection too)

#

and perform an operation on it

distant trail
#

It just double loops it which looks abit bad

#

And I only need the second loop

#

Well I need both but the second one is where its using it

mental lichen
#

hmm maybe you can use flatMap

distant trail
#

Tried that seems u need to loop twice anyway

mental lichen
#

how did you do ?

young thistle
#

Flatmap with an inner stream should work

feral umbra
distant trail
#

but how is it accessing the repoo

#

Like how is it getting the current repoo to get the information

mental lichen
#

use peek()

#

like this :

repita.stream()
      .flatMap(rep -> rep.getRepo().stream().peek(rapo -> doSomething(rep, rapo)))
      .toList();
#

it should work

distant trail
#

Sweet let me try

mental lichen
#

well im not sure this is the right way actually ๐Ÿค”

distant trail
#

If I do that I can't access the rep

#

Just the rapo

mental lichen
#

i think you need to use a key-value objecyt

#

inside your flatMap you can do

distant trail
#

With a HashMap of both Objects?

mental lichen
#

no

#

inside flatMap use stream.map()

#

hmm let me check an example

distant trail
#

Oh that would just go back to the first issue haha I might as well do 2 forEach

mental lichen
#

it's a nested loop

distant trail
#

ok

mental lichen
#
repita
    .stream()
     .flatMap(rep -> rep.getReppo().stream().map(rapo -> new SimpleEntry<>(rep, rapo)))
      .forEach(entry -> doSomething(entry.getKey(), entry.getValue()))
#

i think this also works, but it's longer

#

The SimpleEntry object helps create key value objects

distant trail
#

Ah I see

#

Thank you I will look into this

mental lichen
#

then in forEach you call that method

young thistle
#

this did work for me :

class Scratch {
    public static void main(String[] args) {
        Set<Bucket> buckets = Set.of(
                new Bucket(Set.of(new BucketItem(), new BucketItem(), new BucketItem())),
                new Bucket(Set.of(new BucketItem(), new BucketItem(), new BucketItem())),
                new Bucket(Set.of(new BucketItem(), new BucketItem(), new BucketItem()))
        );

        List<BucketItem> list = buckets.stream()
                .flatMap(bucket -> bucket.items()
                        .stream()
                        .peek(bucketItem -> System.out.println(bucketItem.toString() + bucket.toString())))
                .toList();
    }
}

record Bucket(UUID uuid, Set<BucketItem> items) {
    Bucket(Set<BucketItem> items) {
        this(UUID.randomUUID(), items);
    }
}

record BucketItem(UUID uuid) {
    BucketItem() {
        this(UUID.randomUUID());
    }
}
distant trail
#

Sweet I will try this

feral umbra
feral umbra
distant trail
distant trail
#

List<ObjectName>

#

Inside this object there is other objects

feral umbra
#
List<BucketItem> list = buckets.stream()
    .flatMap(bucket -> bucket.items()
    .stream()
    .toList();