HashMap Save key value pairs (key-value), adopt key Can be uniquely obtained value. Whether the key values are the same is determined by two functions , namely hashCode() and equals().hashCode() Determine the storage location of key value pairs ,equals() Determines whether the two objects are the same . Usually, common data types are rewritten hashCode() and equals() method . for example HashMap The stored key is String type , So as long as the value of the string is the same , It can be regarded as the same key . For example, the following example :

AcWing3814. Matrix transformation

Given a   matrix .

You can select several columns ( You can also choose not to choose ), And transform all elements on these columns ( change  , change ).

Your goal is to satisfy as many rows as possible in the matrix : All elements in a row are .

Output the maximum number of qualified rows that can be obtained .

  thinking
: You can know by analyzing the topic , The string with the largest number of strings formed by each row of data is the maximum number of strings that meet the conditions , So I thought of using hash table to save , The key is 01 character string , The value is the corresponding number of strings . And we know that each string object is different , So after saving, the corresponding values of each key are 1. however String Class overridden hashCode() and equals() method , stay HashMap with String When is a key , As long as the value of the string is the same , Then the key is the same , So it can be used to count .

code (Java)
import java.util.*; class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); int n = sc.nextInt(); Map<String,Integer>
map = new HashMap<>(); for(int i = 0;i < n;i++) { String s = sc.next();
map.put(s,map.getOrDefault(s,0) + 1); } int res = 0; for(Map.Entry
e:map.entrySet()) { res = Math.max(res,(int)e.getValue()); }
System.out.println(res); } }

But when we use objects of other classes as keys , For example, when using a class written by yourself as a key , Then every object is different , It cannot be used at this time HashMap To count . If you want to generate counting function , Then you have to do something about your own class , rewrite hashCode() and equals() method . For example, a Person class , As long as the object of this class id Is the same , Then it's the same object , Thus, it can be used as a key . At this point, write the condition into the above two methods .
public class Person { private String id; public Person(String id) { this.id =
id; } @Override public boolean equals(Object o) { if (this == o) return true;
// The same object returns directly true if (o == null || getClass() != o.getClass()) return false;
//o Is empty or the generated classes are inconsistent , Direct return false Person person = (Person) o;
// both id One is empty , The other is not empty , return false; Neither is empty , But the values are different. Return false if (id != null ?
!id.equals(person.id) : person.id != null) return false; return true; }
@Override public int hashCode() { return id != null ? id.hashCode() : 0; } }

Technology