Data Weighting in Java List

  • 2021-06-28 09:07:14
  • OfStack

Data deweighting in list is usually done by converting list to set, which is simple and straightforward because the set collection is characterized by no duplicate elements.There are two scenarios to consider:

1.The data type in the List collection is the basic data type

The list collection can be converted directly to set, and duplicate elements are automatically removed.

Examples include the following:


public class Test {
  public static void main(String[] args) {
    List list = new ArrayList();
    list.add(11);
    list.add(12);
    list.add(13);
    list.add(14);
    list.add(15);
    list.add(11);
    System.out.println(list);
    Set set = new HashSet();
    List newList = new ArrayList();
    set.addAll(list);
    newList.addAll(set);
    System.out.println(newList);
  }
}

2. The data type stored in the List collection is the object type

The equals() and hashCode() methods need to be overridden in the object's entity class


public class People {
  private String name;
  private String phoneNumber;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPhoneNumber() {
    return phoneNumber;
  }
  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }
  public People(String name, String phoneNumber) {
    super();
    this.name = name;
    this.phoneNumber = phoneNumber;
}
  @Override
  public String toString() {
    return "People{" +
        "name='" + name + ''' +
        ", phoneNumber='" + phoneNumber + ''' +
        '}';
  }
  @Override
  public boolean equals(Object o) {
    People p = (People) o;
    return name.equals(p.name) && phoneNumber.equals(p.phoneNumber);
  }
  @Override
  public int hashCode() {
    String str = name + phoneNumber;
    return str.hashCode();
  }
}
public static void main(String[] args) {
  List<People> listPeople = new ArrayList<People>();
  listPeople.add(new People(" Zhang 3", "11111"));
  listPeople.add(new People(" Zhang 3", "22222"));
  listPeople.add(new People(" plum 4", "33333"));
  listPeople.add(new People(" Zhang 3", "22222"));
  Set<People> setData = new HashSet<People>();
  setData.addAll(listPeople);
  System.out.println("list : " + listPeople.toString());
  System.out.println("set : " + setData.toString());
}

Finally, we take out the source code of equals () method and hashCode () method in String to further understand:

equals()


  public boolean equals(Object anObject) {
    if (this == anObject) {
      return true;
    }
    if (anObject instanceof String) {
      String anotherString = (String)anObject;
      int n = count;
      if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
          if (v1[i++] != v2[j++])
            return false;
        }
        return true;
      }
    }
    return false;
  }

When comparing two objects, the first step is to determine if they have the same address. If they are references to the same object, they are placed back directly to true.If the addresses are not the same, the proof is either to refer to the same object, and the next step is to compare the contents of the two string objects one by one, returning true exactly equal, otherwise false.

hashCode()


  public int hashCode() {
    int h = hash;
    if (h == 0 && count > 0) {
      int off = offset;
      char val[] = value;
      int len = count;
      for (int i = 0; i < len; i++) {
        h = 31*h + val[off++];
      }
      hash = h;
    }
    return h;
  }

hashCode() Official Definition:

The hashcode method returns the hash code value of the object.This method is supported to provide some advantages to hash tables, such as those provided by java.util.Hashtable.

The general agreement for hashCode is:

During the execution of the Java application, when the hashCode method is called multiple times on the same object, the same integer must be returned consistently by 1, provided that the information used in the equals comparison on the object is not modified.This integer does not need to be consistent from one execution of an application to another execution of the same application.

If the two objects are equal according to the equals (Object) method, then calling the hashCode method on each object in the two objects must produce the same integer result.

The following is not necessary: If the two objects are not equal according to the equals (java.lang.Object) method, calling the hashCode method on one of the two objects must produce different integer results.However, programmers should know that generating different integer results for unequal objects can improve the performance of hash tables.

In fact, the hashCode method defined by the Object class does return different integers for different objects. (This is usually done by converting the object's internal address to an integer, but the JavaTM programming language does not require this technique.)

When the equals method is overridden, it is often necessary to override the hashCode method to maintain the general protocol of the hashCode method, which states that equal objects must have equal hash codes.

summary


Related articles: