java_Hashtable 인스턴스

1530 단어 Hashtable
package ming;



import java.util.Hashtable;



class D {

	int count;



	public D(int count) {

		this.count = count;

	}



	public boolean equals(Object obj) {

		if (obj == this) {

			return true;

		}

		if (obj != null && obj.getClass() == D.class) {

			D d = (D) obj;

			return this.count == d.count;

		}

		return false;

	}

	

	public int hashCode(){

		return this.count;

	}

	

}



class E{

	@Override

	public boolean equals(Object obj) {

		return true;

	}

}



public class HashtableTest {



	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		Hashtable ht = new Hashtable();

		ht.put(new D(6000), "java");

		ht.put(new D(87563), "C++");

		ht.put(new D(1232), new E());

		System.out.println(ht);

		

		/*  equals    true

		 * Hashtable         value

		 * Hashtable     E  

		 *         equal       ,       true

		 */

		System.out.println(ht.containsValue("testing value"));

		/*

		 *   D     count  ,  equals()      true, hashcode  

		 * Hashtable       ,      true

		 * */

		System.out.println(ht.containsValue(new D(6000)));

		/*

		 *       key-value

		 * */

		ht.remove(new D(1232));

		/*

		 *      key-value

		 * */

		for(Object key:ht.keySet()){

			System.out.print(key+"-->");

			System.out.print(ht.get(key));

			System.out.println();

		}

	}



}

//    key  !!!!!!!!!!!!!!!!!!!!!!!!!!

좋은 웹페이지 즐겨찾기