3

I searched everything about this but I found nothing. So I have an Integer array and I would like to remove elements duplicated on it with stream api, for example:

Integer[] buffer = new Integer[]{10,23,8,10,8,1,2,1};

after this -> buffer = Stream.of(buffer)...

buffer now has these numbers {10,23,8,10,1,2}

So I hope you understand what I mean and I thank you for helping me.

1
  • Did you intend to keep the 10s? Commented Mar 21, 2016 at 19:22

2 Answers 2

2

Using the Stream API:

Integer[] buffer = {10,23,8,10,8,1,2,1};
buffer = Stream.of(buffer).distinct().toArray(Integer[]::new);
Sign up to request clarification or add additional context in comments.

Comments

0

It would be much simpler to just use a Set to remove duplicates:

Integer[] buffer = new Integer[]{10,23,8,10,8,1,2,1};
Set<Integer> set = new HashSet<>(Arrays.asList(buffer));
buffer = set.toArray(new Integer[set.size()]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.