虚拟币网站建设,昆明建设网站的公司,在百度备案网站,抖音代运营大概多少钱一个月文章目录 HashSet判断是否两次add都能加入成功HashSet编码遍历HashMap判断输出中是否有abc HashSet判断是否两次add都能加入成功
HashSet set new HashSet();
set.add(new String(hsp));
set.add(new String(hsp));第一次可以#xff0… 文章目录 HashSet判断是否两次add都能加入成功HashSet编码遍历HashMap判断输出中是否有abc HashSet判断是否两次add都能加入成功
HashSet set new HashSet();
set.add(new String(hsp));
set.add(new String(hsp));第一次可以第二次不能。因为hashset 的底层源码决定如果equals能够度量并确定两个对象相同String可以使用equals直接判断两个串是否相同所以检测到两个String对象相同时无法重复加入hashset。
HashSet编码 hashCode 是 Object 类中的一个方法它返回对象的哈希码。其默认实现在 Object 类中是基于对象的内存地址的这就意味着不同对象即使是同一个类通常会有不同的哈希码。 【ps在课堂上老师为了让同一个类的对象聚集到hashset中的同一个key下重写同一个类的hashcode使之返回相同的值。但由于equals比较的结果是不同的对象即使是同一个类而返回false所以不会被判为相同而无法加入hashset】 下面重写equals和hashCode方法如果name 和 age 值相同则返回相同的hash值保证映射到hashset的相同键值下equals返回true从而在同一键值的链表比较中防止拥有相同name 和 age 值的两个对象重复equals返回true则插入失败。IDEA快捷键altinsert
public class HashSetExercise {public static void main(String[] args) {HashSet hashSet new HashSet();hashSet.add(new Employee(milan, 18));//okhashSet.add(new Employee(smith, 28));//okhashSet.add(new Employee(milan, 18));//加入不成功.}
}//创建Employee
class Employee {private String name;private int age;public Employee(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}Overridepublic String toString() {return Employee{ name name \ , age age };}public void setAge(int age) {this.age age;}//如果name 和 age 值相同则返回相同的hash值Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Employee employee (Employee) o;return age employee.age Objects.equals(name, employee.name);}Overridepublic int hashCode() {return Objects.hash(name, age);}
}遍历HashMap 考察遍历的HashMap的熟练使用。 另外Object的toString方法被其他类重写时即使编译类型是Object类还是能够在输出类时调用被重写的toString方法。
SuppressWarnings({all})
public class Exercise {public static void main(String[] args) {HashMap hashMap new HashMap();Employee a new Employee(a, 19000, 1);Employee b new Employee(b, 18000, 2);Employee c new Employee(c, 19000, 3);hashMap.put(a.getId(), a);hashMap.put(b.getId(), b);hashMap.put(c.getId(), c);Set set hashMap.keySet();for (Object key :set) {Employee e (Employee) hashMap.get(key);if (e.getSal() 18000)System.out.println(e); // 根据key输出value// PS:// System.out.println(hashMap.get(key))输出的是Employee中重写的toString方法// 因为此时Object的toString方法被Employee重写// 但Employee中独有的方法不能被调用需要先转成Employee类}System.out.println();Set set1 hashMap.entrySet();Iterator iterator set1.iterator();while (iterator.hasNext()) {Map.Entry next (Map.Entry) iterator.next();Employee e (Employee) next.getValue();if (e.getSal() 18000)System.out.println(e);}}
}
SuppressWarnings({all})
class Employee {private String name;private double sal;private String id;public Employee(String name, double sal, String id) {this.name name;this.sal sal;this.id id;}Overridepublic String toString() {return Employee{ name name \ , sal sal , id id \ };}public String getName() {return name;}public double getSal() {return sal;}public String getId() {return id;}
}判断输出中是否有abc 没有。因为按照长度大小进行比较根据底层源码两个String由于长度相同加上匿名类中重写的比较方法以长度作为唯一度量标准所以长度相同的就被认为是相同对象而不能加入treeset。