哈希泄露与java配置有关问题
哈希泄露与java配置问题
package com.itcast.simulate; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Properties; public class ReflectTest { public static void main(String[] args) throws Exception{ //用完整的路径不是硬编码,而是运算出来的 //eclipse把非java文件搬到class下面去了 //InputStream ips=new FileInputStream("config.properties"); //第二种方式classpath,以后面临不会给src // InputStream ips=ReflectTest.class.getClassLoader().getResourceAsStream("config.properties"); //其实class里面也提供了方法,如下 InputStream ips=ReflectTest.class.getResourceAsStream("/config.properties"); Properties props=new Properties(); props.load(ips); ips.close(); String className=props.getProperty("className"); Collection collection=(Collection)Class.forName(className).newInstance(); Dog dog1=new Dog(1,"jude"); Dog dog2=new Dog(1,"jude"); Dog dog3=new Dog(3,"ted"); Dog dog4=new Dog(4,"maodan"); collection.add(dog1); collection.add(dog2); collection.add(dog3); collection.add(dog4); collection.add(dog4); // System.out.println(collection.remove(dog1)); // System.out.println(collection.remove(dog2)); System.out.println(collection.size()); System.out.println("dog1==dog2吗?--->>"+(dog1==dog2)); System.out.println("dog1 eques dog2吗?--->>"+(dog1.equals(dog2))); } }
package com.itcast.simulate; public class Dog { private int id; private String name; public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Dog(int id, String name) { this.id = id; this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Dog other = (Dog) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public void setId(int id) { this.id = id; } }