43

I have a stream such as:

Arrays.stream(new String[]{"matt", "jason", "michael"});

I would like to remove names that begin with the same letter so that only one name (doesn't matter which) beginning with that letter is left.

I'm trying to understand how the distinct() method works. I read in the documentation that it's based on the "equals" method of an object. However, when I try wrapping the String, I notice that the equals method is never called and nothing is removed. Is there something I'm missing here?

Wrapper Class:

static class Wrp {
    String test;
    Wrp(String s){
        this.test = s;
    }
    @Override
    public boolean equals(Object other){
        return this.test.charAt(0) == ((Wrp) other).test.charAt(0);
    }
}

And some simple code:

public static void main(String[] args) {
    Arrays.stream(new String[]{"matt", "jason", "michael"})
    .map(Wrp::new)
    .distinct()
    .map(wrp -> wrp.test)
    .forEach(System.out::println);
}
dimo414
  • 47,227
  • 18
  • 148
  • 244
Matt Klooster
  • 717
  • 1
  • 5
  • 13
  • 2
    There's the technique I described in [this answer](http://stackoverflow.com/a/27872086/1441122). (Scroll to the end of the answer). This would let you do `filter(distinctByKey(s -> s.charAt(0)))`. – Stuart Marks Jan 12 '15 at 23:24
  • 8
    Side note: for a stream of literals, it's often easier to use `Stream.of("matt", "jason", "michael")`. – Stuart Marks Jan 12 '15 at 23:25

3 Answers3

31

Whenever you override equals, you also need to override the hashCode() method, which will be used in the implementation of distinct().

In this case, you could just use

@Override public int hashCode() {
   return test.charAt(0);
}

...which would work just fine.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
25

Alternative approach

    String[] array = {"matt", "jason", "michael"};
    Arrays.stream(array)
            .map(name-> name.charAt(0))
            .distinct()
            .map(ch -> Arrays.stream(array).filter(name->name.charAt(0) == ch).findAny().get())
            .forEach(System.out::println);
Sujit Kamthe
  • 811
  • 11
  • 14
0

You can achieve it using Stream.filter and HashSet.add:

Set<Character> distinct = new HashSet<>();
Arrays.stream(new String[]{"matt", "jason", "michael"})
    .filter(m -> distinct.add(m.charAt(0)))
    .forEach(System.out::println);

will print

matt
jason

The method HashSet.add is returning false if you try to add some object that already exists in it.

Tamir Adler
  • 341
  • 2
  • 14