Java List remove custom object by object reference

Description

Java List remove custom object by object reference

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] a) {
    List<Language> langs = new ArrayList<Language>();

    Language lang1 = new Language("CSS", "1");
    Language lang2 = new Language("HTML", "2");
    Language lang3 = new Language("Java", "3");

    langs.add(lang1);//from  www.ja v a 2 s  .  c  om
    langs.add(lang2);
    langs.add(lang3);

    System.out.println(langs);

    langs.remove(lang2);

    System.out.println(langs);
  }
}


class Language {
  private String name;

  private String id;


  public Language(String n, String id) {
    this.name = n;
    this.id = id;

  }

  public String getName() {
    return this.name;
  }

  public void setName(String lastName) {
    this.name = lastName;
  }

  public String getId() {
    return this.id;
  }

  public void setId(String firstName) {
    this.id = firstName;
  }

  @Override
  public String toString() {
    return "Language [name=" + name + ", id=" + id + "]";
  }
  
}



PreviousNext

Related