Question: I have class: 

```
class MyClass {
    String s1;
    String s2;
}

```

List Object include

> 
> [{s1: "a", s2: "b"}, {s1: "a", s2: "c"}, {s1: "a", s2: "b"}, {s1: "a",
>  s2: "d"}]
> 
> 
> 

I want to remove object duplicate 2 properties s1 and s2 

Expect Result:

> 
> [{s1: "a", s2: "b"}, {s1: "a", s2: "c"}, {s1: "a", s2: "d"}]
> 
> 
> 

If can use Java 8 is better 

Help me :(

Answer: 
First override the `equals` and `hashCode` methods in your `MyClass` like so,

```
@Override
public int hashCode() {
    return 31 * s1.hashCode() + s2.hashCode();
}

@Override
public boolean equals(Object obj) {
    return obj instanceof MyClass && ((MyClass) obj).getS1().equals(getS1())
            && ((MyClass) obj).getS2().equals(getS2());
}

```

Then the easiest way to eliminate duplicates would be using a `Set`.

`new HashSet<>(instances)`
You need to override equal and hash code function and then use the distinct function of Streaming API of java 8.

```
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class MyClass {

  private String s1;
  private String s2;

  public MyClass() {
  }

  public MyClass(String s1, String s2) {
    this.s1 = s1;
    this.s2 = s2;
  }

  public String getS1() {
    return s1;
  }

  public MyClass setS1(String s1) {
    this.s1 = s1;
    return this;
  }

  public String getS2() {
    return s2;
  }

  public MyClass setS2(String s2) {
    this.s2 = s2;
    return this;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    MyClass myClass = (MyClass) o;
    return Objects.equals(getS1(), myClass.getS1()) &&
        Objects.equals(getS2(), myClass.getS2());
  }

  @Override
  public int hashCode() {
    return Objects.hash(getS1(), getS2());
  }

  @Override
  public String toString() {
    return "MyClass{" + "s1='" + s1 + '\'' + ", s2='" + s2 + '\'' + '}';
  }

  public static void main(String[] args) {
    List<MyClass> list = new ArrayList<>();
    list.add(new MyClass("a", "b"));
    list.add(new MyClass("a", "c"));
    list.add(new MyClass("a", "b"));
    list.add(new MyClass("a", "d"));
    System.out.println("Original size is : " + list.size()); // Size is 4
    list.forEach(System.out::println);
    //Now remove duplicate
    List<MyClass> expected = list.stream().distinct().collect(Collectors.toList());
    System.out.println(
        "After remove duplication size is : " + expected.size()); // Here is your expected result size is 3
    expected.forEach(System.out::println);
  }
}

```

Here is the output :

Original size is : 4

MyClass{s1='a', s2='b'}

MyClass{s1='a', s2='c'}

MyClass{s1='a', s2='b'}

MyClass{s1='a', s2='d'}

After remove duplication size is : 3

MyClass{s1='a', s2='b'}

MyClass{s1='a', s2='c'}

MyClass{s1='a', s2='d'}