class Person1 {
String name;
int age;
public Person1() {
// TODO Auto-generated constructor stub
}
public Person1(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
//重写hashCode方法
@Override
public int hashCode() {
// TODO Auto-generated method stub
return (this.name + this.age * 1000) .hashCode();
}
@Override
public boolean equals(Object obj) {
if( !(obj instanceof Person1)) {
throw new ClassCastException();
}
Person1 person = (Person1) obj;
return person.age == this.age && person.name.equals(this.name);
}
}
public class Demo7 {
public static void main(String[] args) {
HashSet set = new HashSet<>();
//在string中重写了hashCode和equals方法。
//在add内部实现了去重的功能,默认调用的是字符串的hashCode和equals方法,实现去重
set.add("java1");
set.add("java2");
set.add("java2");
set.add("java5");
set.add("java3");
set.add("java4");
System.out.println(set);
HashSet set1 = new HashSet<>();
//在string中重写了hashCode和equals方法。
//在add内部实现了去重的功能,默认调用的是字符串的hashCode和equals方法,实现去重
set1.add(new Person1("小明", 20));
set1.add(new Person1("小明", 20));
set1.add(new Person1("小红", 20));
set1.add(new Person1("小李", 20));
set1.add(new Person1("小花", 20));
set1.add(new Person1("小白", 20));
System.out.println(set1);
}
}
* 分析**(重点)**:要重写Person1的HashCode和equals方法,按照自己定义的规则来重写,比较规则是按照年龄和性别比较。先是用hashcode方法来比较年龄,来确定它是不是同一个年龄,如果年龄相同了,那么则判定他们的姓名,相同则是同一个对象,否则,就不是同一个对象。
public class Demo8 {
public static void main(String[] args) {
//默认是升序并且是按照字典排序
set.add("java2");
set.add("java8");
set.add("java3");
set.add("java4");
set.add("java4");
System.out.println(set);
//实例:将Person2的对象存储入TreeSet,实现排序和去重
TreeSet set1 = new TreeSet<>();
set1.add(new Person2("bobo", 20));
set1.add(new Person2("bobo", 18));
set1.add(new Person2("bobo3", 200));
set1.add(new Person2("bobo5", 20));
set1.add(new Person2("bobo", 20));
System.out.println(set1);
}
}
class Person2 implements Comparable
分析 (重点):能够实现排序的原因是方法中调用了字符串的 compareTo方法,compareTo方法是来自于Comparable接口。有时候我们需要按照自己定义的规则去实现TreeSet的排序规则的话,就得实现comparator接口,并且重写compare方法,也就是重新定义一个比较器。
比较器的创建
class ComWithLength implements Comparator
比较器的使用
public class Demo9 {
public static void main(String[] args) {
//创建一个比较器对象
ComWithLength comWithLength = new ComWithLength();
//将比较器对象指定给当前的TreeSet对象。
TreeSet
public class Demo8 {
public static void main(String[] args) {
//介绍Map接口的方法
Map map = new HashMap<>();
//1.增加
//V put(K key,V value) 增加一个键值对
//关于返回值:如果当前的key没有对应值,返回null.如果已经有值了,会将原来被替换的值返回.
map.put("01", "java");
map.put("02", "iOS");
map.put("03", "html");
map.put("04", "python");
System.out.println(map.put("01", "php"));
System.out.println(map);
//void putAll(Map extends K,? extends V> map) 增加多个
//2.删除
//V remove(Object key) 根据key删除元素
map.remove("01");
System.out.println(map);
//void clear() 删除全部
//3.获取
//V get(Object key) 根据key查找元素
System.out.println(map.get("02"));
//int size() 获取键值对的个数
System.out.println(map.size());
//Set keySet() 遍历方法一
//Set> entrySet() 遍历方法二
//4.常用的判断
//boolean isEmpty()
System.out.println(map.isEmpty());//false
//boolean containsKey(K key) 是否包含当前的key
System.out.println(map.containsKey("02"));//true
//boolean containsValue(V value) 是否包含当前的value
System.out.println(map.containsValue("html"));//true
}
}
Map的遍历:
public class Demo9 {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("01", "java");
map.put("03", "php");
map.put("05", "BigData");
map.put("02", "python");
System.out.println(map);
//通过Set keySet() 遍历方法一
Set set1 = map.keySet();
//获取迭代器
Iterator iterator = set1.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
//再去获取value
System.out.println("key:"+key+" value:"+map.get(key));
}
}
}
public class Demo9 {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put("01", "java");
map.put("03", "php");
map.put("05", "BigData");
map.put("02", "python");
System.out.println(map);
//Set> entrySet() 遍历方法二
//先得到装着entry的set
Set> set = map.entrySet();
//获取迭代器
Iterator> iterator2 = set.iterator();
while (iterator2.hasNext()) {
Map.Entry entry = iterator2.next();
//entry.setValue("hah");
//再去获取key,value
System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
}
System.out.println(map);
}
}
HashMap:
class Animal{
String name;
int age;
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age + "]";
}
//重写hashCode
@Override
public int hashCode() {
return name.hashCode()+age*1000;
}
//重写equals
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Animal)) {
throw new ClassCastException("类型转化错误");
}
Animal animal = (Animal)obj;
return age==animal.age && name.equals(animal.name);
}
}
public class Demo10 {
public static void main(String[] args) {
HashMap map = new HashMap<>();
//在put方法中调用了作为key的String的hashCode和equals方法
map.put("01", "java");
map.put("03", "java2");
map.put("05", "java4");
map.put("02", "java6");
map.put("01", "java9");
System.out.println(map);
//实例:将Animal类的对象存入HashMap,名字和年龄相同的认为是同一条狗
HashMap map1 = new HashMap<>();
//在put方法中调用了作为key的Animla的hashCode和equals方法
map1.put(new Animal("拉布拉多", 20), "java");
map1.put(new Animal("柯基", 10), "java2");
map1.put(new Animal("黑贝", 2), "java4");
map1.put(new Animal("茶杯犬", 21), "java6");
map1.put(new Animal("藏獒", 12), "java9");
map1.put(new Animal("藏獒", 12), "java9");
System.out.println(map1);
}
}
TreeMap:
class Animal1 implements Comparable{
String name;
int age;
public Animal1(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Animal1 [name=" + name + ", age=" + age + "]";
}
//重写compareTo方法
@Override
public int compareTo(Animal1 o) {
int num = age-o.age;
return num==0?name.compareTo(o.name):num;
}
}
public class Demo11 {
public static void main(String[] args) {
TreeMap map = new TreeMap<>();
//String实现了Comparable接口的compareTo方法,所以实现了排序和去重
map.put("01", "java");
map.put("03", "java2");
map.put("05", "java4");
map.put("02", "java6");
map.put("01", "java9");
System.out.println(map);
TreeMap map1 = new TreeMap<>();
map1.put(new Animal1("labu", 20), "java");
map1.put(new Animal1("keji", 20), "java2");
map1.put(new Animal1("zangao", 12), "java9");
map1.put(new Animal1("zangao", 12), "java9");
System.out.println(map1);
}
}
class MyCompera implements Comparator{
@Override
public int compare(Now2 o1, Now2 o2) {
int num = o1.age - o2.age;
return num == 0 ? o1.name.compareTo(o2.name):num;
}
}
class Now2 {
public String name;
public int age;
public Now2() {
super();
// TODO Auto-generated constructor stub
}
public Now2(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Three {
public static void main(String[] args) {
TreeMap treeMap = new TreeMap<>(new MyCompera());
}
}