Skip to content

Latest commit

 

History

History
215 lines (162 loc) · 4.65 KB

10. Equals and HashCode.md

File metadata and controls

215 lines (162 loc) · 4.65 KB

10. Equals and HashCode

两个内容相同的对象具有相等的HashCode

==如果equals为true,则hashcode一定相等==

Set/HashSet:

HashSet底层通过HashMap实现;

Set中没有get方法,存放的数据是无序的;

元素不能重复(equals不能相等,相等的话后add的将之前的覆盖);

add()和remove()常用;

package com.alexanderli95.collections;

import java.util.HashSet;
import java.util.Set;

/**
 * 测试Set和HashSet
 */
public class TestSet {
    public static void main(String[] args) {
        Set set=new HashSet();
        set.add("123");
        set.add("234");
        set.add("123");  //重复覆盖
        System.out.println(set.size());
        set.remove("123");
    }
}

HashSet实现:

package com.alexanderli95.collections;

import java.util.HashMap;

/**
 * HashSet实现
 */
public class AlexHashSet {

    HashMap map;
    public static final Object PRESENT=new Object();
    public AlexHashSet(){
        map=new HashMap();
    }

    public void add(Object obj){
        map.put(obj,PRESENT);

    }

    public int size(){
        return map.size();
    }

    public static void main(String[] args) {
        AlexHashSet set=new AlexHashSet();
        set.add("123");
        System.out.println(set.size());
    }
}

数据存储实例:

Employee:

package com.alexanderli95.collections02;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Employee {   //JavaBean, 实体类
    private int ID;
    private String name;
    private String department;
    private int money;
    private Date joinDate;

    public Employee(int ID, String name, String department, int money, String joinDate) {
        this.ID = ID;
        this.name = name;
        this.department = department;
        this.money = money;

        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM");
        try {
            this.joinDate = dateFormat.parse(joinDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public int getID() {
        return ID;
    }

    public String getName() {
        return name;
    }

    public String getDepartment() {
        return department;
    }

    public int getMoney() {
        return money;
    }

    public Date getJoinDate() {
        return joinDate;
    }
}

2.Test01:

package com.alexanderli95.collections02;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Test01 {

    //用ArrayList实现

    public static void main(String[] args) {
        Employee e1=new Employee(001,"Alex","lab01",3000,"2019-06");
        Employee e2=new Employee(002,"Anna","lab02",5000,"2015-06");
        Employee e3=new Employee(003,"Jenny","lab04",5100,"2016-06");

        List<Employee> list=new ArrayList<Employee>();  //泛型:加了泛型容器中只能放Employee
        list.add(e1);
        list.add(e2);
        list.add(e3);

        new Test01().printEmployeeNames(list);
    }

    public void printEmployeeNames(List<Employee> list){
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).getName());
        }
    }


}

3.Test02:

package com.alexanderli95.collections02;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test02 {
    //用Map实现
    public static void main(String[] args) {
        Map map1=new HashMap();
        map1.put("id",001);
        map1.put("name","Alex");
        map1.put("department","lab01");
        map1.put("money",3000);
        map1.put("joinDate","2019-07");

        Map map2=new HashMap();
        map2.put("id",002);
        map2.put("name","Anna");
        map2.put("department","lab02");
        map2.put("money",3100);
        map2.put("joinDate","2015-07");

        Map map3=new HashMap();
        map3.put("id",003);
        map3.put("name","Jenny");
        map3.put("department","lab03");
        map3.put("money",4000);
        map3.put("joinDate","2016-07");

        List<Map> maps=new ArrayList<Map>();
        maps.add(map1);
        maps.add(map2);
        maps.add(map3);

        printEmployeeData(maps);
    }

    public static void printEmployeeData(List<Map> maps){
        for(int i=0;i<maps.size();i++){
            Map temp=maps.get(i);
            System.out.println(temp.get("id")+"--"+temp.get("name")+"--"+temp.get("money"));

        }
    }
}