JAVA的HashMap改造面向对象编程-----JAVA

import java.util.*;

public class MapTest
{
    public static void main(String[] args)
    {
        Map map = new HashMap();
        map.put(null,null);
        System.out.println(map);
        Map table = new Hashtable();
        table.put("null",123);
        System.out.println(table);
        Map map1 = new LinkedHashMap();
        map1.put(34,"AA");
        map1.put("Te",123);
        map1.put("TOM",new Object());
        System.out.println(map1);
        HashMap map2 = new HashMap();
        map2.put("AA",123);
        map2.put(67,123);
        map2.put("A",123);
        map2.put("qA",new Person("aa",12));
        map2.put(new Person("aaw",12),"qA");
        System.out.println(map2);
        System.out.println(map2.size());
        Object value = map2.remove("AA");
        System.out.println(value);
        System.out.println(map2);
        Object oldValue = map2.put("A",99);
        System.out.println(oldValue);
        System.out.println(map2);
        Object value1 = map2.get("qA");
        System.out.println(value1);
        System.out.println(map2.get(new Person("aaw",12)));
        Set keyset = map2.keySet();
        Iterator iterator = keyset.iterator();
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
        for(Object o : keyset)
        {
            System.out.println(o);
        }
        //方式1
        Collection collection = map2.values();
        for(Object obj : collection)
        {
            System.out.println(obj);
        }
        //方式2
        for(Object key : keyset)
        {
            System.out.println(map2.get(key));
        }
        Set set = map2.entrySet();
        Iterator iterator1 = set.iterator();
        while(iterator1.hasNext())
        {
            Map.Entry entry = (Map.Entry)iterator1.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

class Person
{
    private String name;
    private int age;

    public Person() {
    }

    public Person(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;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person person)) return false;
        return getAge() == person.getAge() && Objects.equals(getName(), person.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

import java.util.*;

public class MapTest
{
    public static void main(String[] args)
    {
        Map map = new HashMap();
        map.put(null,null);
        System.out.println(map);
        Map table = new Hashtable();
        table.put("null",123);
        System.out.println(table);
        Map map1 = new LinkedHashMap();
        map1.put(34,"AA");
        map1.put("Te",123);
        map1.put("TOM",new Object());
        System.out.println(map1);
        HashMap map2 = new HashMap();
        map2.put("AA",123);
        map2.put(67,123);
        map2.put("A",123);
        map2.put("qA",new Person("aa",12));
        map2.put(new Person("aaw",12),"qA");
        System.out.println(map2);
        System.out.println(map2.size());
        Object value = map2.remove("AA");
        System.out.println(value);
        System.out.println(map2);
        Object oldValue = map2.put("A",99);
        System.out.println(oldValue);
        System.out.println(map2);
        Object value1 = map2.get("qA");
        System.out.println(value1);
        System.out.println(map2.get(new Person("aaw",12)));
        Set keyset = map2.keySet();
        Iterator iterator = keyset.iterator();
        while(iterator.hasNext())
        {
            System.out.println(iterator.next());
        }
        for(Object o : keyset)
        {
            System.out.println(o);
        }
        //方式1
        Collection collection = map2.values();
        for(Object obj : collection)
        {
            System.out.println(obj);
        }
        //方式2
        for(Object key : keyset)
        {
            System.out.println(map2.get(key));
        }
        Set set = map2.entrySet();
        Iterator iterator1 = set.iterator();
        while(iterator1.hasNext())
        {
            Map.Entry entry = (Map.Entry)iterator1.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

class Person
{
    private String name;
    private int age;

    public Person() {
    }

    public Person(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;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person person)) return false;
        return getAge() == person.getAge() && Objects.equals(getName(), person.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge());
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值