[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
HashMap存的是键值对(key-value),通过key可以唯一获取value。键值是否相同是通过两个函数确定的,分别是hashCode()和equals()。hashCode()确定了键值对存放的位置,equals()确定了两个对象是否相同。通常常见数据类型都是重写了hashCode()和equals()方法。例如HashMap存的键是String类型,那么只要字符串的值相同时,就可以看作是同一个键。例如下面一个例子:
AcWing3814. 矩阵变换
给定一个的 矩阵。
你可以选择若干列(也可以不选),并将这些列上的所有元素进行变换(变 ,变 )。
你的目标是使得矩阵中有尽可能多的行满足:一行中的所有元素都为 。
输出可以得到的满足条件的行的最大数量。
思路
:分析题目可以得知,各行数据形成的字符串的最多的那个字符串就是满足条件的最大数量,所以想到使用哈希表来存,键就是01字符串,值是字符串相应的个数。而我们知道每个字符串对象是不相同的,所以存完之后各个键对应的值都是1。但是String类重写了hashCode()和equals()方法,在HashMap以String为键时,只要字符串的值相同,那么键就相同了,所以可以用来计数。
代码(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); } }
但是当我们使用别的类的对象作为键,例如将自己写的类作为键时,那么每个对象都是不相同的,此时就无法使用HashMap来计数。如果要产生计数功能,那么就要对自己的类,重写hashCode()和equals()方法。例如一个Person类,只要这个类的对象的id是相同的,那么就是同一个对象,从而可以作为键。此时就将该条件写入以上两个方法中。
public class Person { private String id; public Person(String id) { this.id =
id; } @Override public boolean equals(Object o) { if (this == o) return true;
//同一个对象直接返回true if (o == null || getClass() != o.getClass()) return false;
//o为空或者两者生成类不一致,直接返回false Person person = (Person) o;
//两者id一个为空,另一个不为空,返回false;两个都不为空,但是值不一样返回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; } }