This document is a reprint of the material created for new engineers about 10 years ago. There may be some old-fashioned parts such as API and syntax, but once you learn the idea, I intend to organize the basic parts that can be used for a long time, so I hope that it will be helpful for young engineers now.
--Understand the difference between List and Set in Java. --List holds duplicate elements. --Set does not hold duplicate elements. ――What are duplicate elements in Java in the first place?
Use the User class implemented in equals and hashCode. The Object # toString () method is overridden to output the state of the object.
User.java
class User {
/** ID */
private int id;
/**name*/
private String name;
/**
*It is a constructor.
*
* @param id ID
* @param name name
*/
public User(int id, String name) {
this.id = id;
this.name = name;
}
// (Abbreviation)getter etc.
/**
*Returns a hash value of this class.
*
* @return Hash value of this class
*/
public int hashCode() {
return this.id * 13;
}
/**
*The instance of this class and the object passed as an argument
*Returns true if they are equivalent.
*The object passed as an argument is an instance of User class
*If the id values are equal, they are considered equivalent.
*
* @True if the object passed in the return argument is an instance of the User class and the ids are equal.
*/
public boolean equals(Object other) {
if (this == other) { //True if the object passed as an argument is this object itself
return true;
}
if (!(other instanceof User)) { //The object passed as an argument is an object of User class
return false; //False if not.
}
User otherUser = (User) other;
if (this.id == otherUser.getId()) { //Compare the ID values, true if they are equal, false if they are not equal.
return true;
}
return false;
}
/**
*A string representation of an instance of this class.
*
* @return A string representation of an instance of this class.
*/
public String toString() {
return "User : id = " + id + " name = " + name;
}
}
--Guarantees the order in which elements are added. In other words, you can get the elements by specifying the index in the order of add (). --Keep duplicate elements. What are overlapping elements in the first place? Will be explained later.
--We do not guarantee the order in which elements are added. That is, you cannot get the element by specifying the index. --Does not retain duplicate elements.
Judge equivalence by id value (return true if id values are equal in equals method) Add an instance of User class to ArrayList as shown below, and output the added element as a character string as standard output. Then ...
User user1 = new User(1, "Maekawa");
User user2 = new User(2, "Suzuki");
User user3 = new User(3, "Maekawa");
User user4 = new User(1, "Sato"); //ID is duplicated with user1
List userList = new ArrayList();
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
for (Iterator userIter = userList.iterator(); userIter.hasNext(); ) {
User user = (User) userIter.next();
System.out.println("User in userList: " + user); //Output the contents of the toString method call
}
The output result is as follows. It is output in the order in which the elements are added, and duplicate elements are retained.
*****************************************
User in userList: User : id = 1 name =Maekawa
User in userList: User : id = 2 name =Suzuki
User in userList: User : id = 3 name =Maekawa
User in userList: User : id = 1 name =Sato
*****************************************
Judge equivalence by id value (return true if id values are equal in equals method) Add an instance of User class to HashSet as shown below, and output the added element as a character string as standard. Then ...
User user1 = new User(1, "Maekawa");
User user2 = new User(2, "Suzuki");
User user3 = new User(3, "Maekawa");
User user4 = new User(1, "Sato"); //ID is duplicated with user1
Set userSet = new HashSet();
userSet.add(user1);
userSet.add(user2);
userSet.add(user3);
userSet.add(user4);
for (Iterator userIter = userSet.iterator(); userIter.hasNext(); ) {
User user = (User) userIter.next();
System.out.println("User in userSet: " + user); //Output the contents of the toString method call
}
The output result is as follows. It is not output (not guaranteed) in the order in which the elements are added, and duplicate elements are not retained.
*****************************************
User in userSet: User : id = 3 name =Maekawa
User in userSet: User : id = 2 name =Suzuki
User in userSet: User : id = 1 name =Maekawa
*****************************************
In the first place, "Set" in Java is an abstract expression of the concept of Set in mathematics.
Every element contained in a Set is unique.
That is, elements contained in the same Set cannot be considered the same (equivalent).
Expressing this in Java
It does not have element pairs of e1 and e2, which are e1.equals (e2). Can be expressed as. In a collection such as Set or List in Java, "duplicate elements" are nothing but multiple elements that return true as the return value when comparing two elements (objects) with the equals method.
Since the equals method must be implemented by the programmer (unless you use the default equals method defined in the Object class), the rules for "duplicate" objects in Java vary depending on the business rules of the application and the programmer's implementation. It means that it can be defined by the method.
In the previous example, the equivalence of instances of the User class was determined by the value of "ID".
If you implement to judge the equivalence of the instance of User class by the value of "name", how will it behave when the instance of User class is added to Set?
User.java
//Implementation example of User class that determines equivalence by the value of name
// (However, for convenience, null is not stored in the value of name.)
/**
*Returns a hash value of this class.
*
* @return Hash value of this class
*/
public int hashCode() {
return this.name.hashCode();
}
/**
*The instance of this class and the object passed as an argument
*Returns true if they are equivalent.
*The object passed as an argument is an instance of User class
*If the values of name are equal, they are considered equivalent.
*
* @True if the object passed in the return argument is an instance of the User class and the names are equal.
*/
public boolean equals(Object other) {
if (this == other) { //True if the object passed as an argument is this object itself
return true;
}
if (!(other instanceof User)) { //The object passed as an argument is an object of User class
return false; //False if not.
}
User otherUser = (User) other;
if (this.name.equals(otherUser.getName())) { //Compare the values of name, true if equal, false if not equal. Return true;
}
return false;
}
Judge equivalence by the value of name (return true if id values are equal in equals method) Add an instance of User class to HashSet as shown below, and output the added element as a character string as standard. Then ...
*****************************************
User in userSet: User : id = 1 name =Sato
User in userSet: User : id = 2 name =Suzuki
User in userSet: User : id = 1 name =Maekawa
*****************************************
--What happens if you don't implement the equals and hashCode methods? --What happens if you implement the equals method and not the hashCode method?
Please check
Recommended Posts