#Weird stream problem. Only duplicates are put into toList()

1 messages · Page 1 of 1 (latest)

versed lagoon
#

Using Fabric for Minecraft modding (but it should not be relevant for this problem).

Trying to use Streams to filter a number of BlockPos Objects down to a list.

When iterating in the stream via .forEach and printing the .toString() to the console, I get the correct output, different BlockPos Objects which have different X,Y,Z fields.

As soon as I run toList(), toArray() or even build a Collection manually using the very same forEach on the stream (see below), I suddenly only get the first Object (or at least its X,Y,Z values) duplicated in my output. What gives? 😦

                    Set<BlockPos> natureBlocks = new HashSet<>();

                    BlockPos.stream(player.getBoundingBox().expand(BONUS_RADIUS))
                    .filter(pos -> isInSphericalRange(pos, player.getBlockPos(), BONUS_RADIUS))
                    .filter(pos -> isInTags(world, pos))
                    .forEach(natureBlocks::add);

                    natureBlocks.forEach(System.out::println); //duplicates

                    //Same deal with .toList(), .toArray()
winter orioleBOT
#

This post has been reserved for your question.

Hey @versed lagoon! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

heavy jacinth
#

Firstly, don't use the forEach() to add anything to a separate list.
Secondly, you can always collect to a Set (which will guarantee unique Objects by their Equals/Hash implementation).