import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.util.Map;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;

// java 泛型
@SuppressWarnings("unchecked")
public class OverrideEqualsAndHashCode
{
	private static final Logger LOGGER = LogManager.getLogger(OverrideEqualsAndHashCode.class);

	public static void main(String[] args)
	{
		Dog lasi = new Dog(1, "拉斯");
		Dog lasi1 = new Dog(1, "拉斯");
		Dog wangcai = new Dog(2, "旺财");

		LOGGER.info("lasi.hashCode={}", lasi.hashCode());
		LOGGER.info("lasi1.hashCode={}", lasi1.hashCode());
		LOGGER.info("lasi与lasi1是否相同？:{}", lasi.equals(lasi1));

		Map<Dog, String> map = new HashMap<>();
		map.put(lasi, lasi.toString());
		map.put(lasi1, lasi1.toString());
		map.put(wangcai, wangcai.toString());
		LOGGER.info("map中共有{}个元素", map.size());

		Set<Dog> dogSet = new HashSet();
		dogSet.add(lasi);
		dogSet.add(lasi1);
		dogSet.add(wangcai);

		for(Dog dog : dogSet)
		{
			LOGGER.info("{}", dog);
		}

		LOGGER.info("dogSet中共有{}个元素", dogSet.size());

		Set<String> stringSet = new HashSet<>();
		stringSet.add("123");
		stringSet.add(new String("123"));
		stringSet.add(new StringBuffer("123").toString());
		stringSet.add(new StringBuffer().append("123").toString());

		LOGGER.info("stringSet中共有{}个元素", stringSet.size());

		Set<Integer> integerSet = new HashSet<>();
		integerSet.add(123);
		integerSet.add(new Integer(123));

		LOGGER.info("stringSet中共有{}个元素", integerSet.size());

	}
}

class Dog
{
	private static final Logger LOGGER = LogManager.getLogger(Dog.class);

	private Integer id;
	private String name;

	public Dog()
	{

	}

	public Dog(Integer id, String name)
	{
		this.id = id;
		this.name = name;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public Integer getId()
	{
		return id;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	@Override
	public String toString()
	{
		return String.format("{id = %s, name = %s}", this.id, this.name);
	}

	@Override
	public boolean equals(Object another)
	{
		LOGGER.info("equals...");

		if(this.getId().equals(((Dog)another).getId()))
		{
			return true;
		}
		return false;
	}

	@Override
	public int hashCode()
	{
		LOGGER.info("hashCode...");
		return this.id;
	}
}