HashMap ã®è¿·åã¡ãã
ãªãã¨ãªãï¼Key ãå¯å¤ã ã£ããã©ããªããã ãããªãã¨ã
æã£ãã®ã§ãã£ã¦ã¿ãâª
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class Main { public static void main(String[] argv) { Map<Hoge,String> map = new HashMap<Hoge, String>(); map.put(new Hoge("test1"), "value1"); System.out.println(map.size()); System.out.println(map.get(new Hoge("test1"))); Hoge hoge = new Hoge("test2"); map.put(hoge, "value2"); System.out.println(map.size()); System.out.println(map.get(new Hoge("test2"))); hoge.setKey("test"); System.out.println(map.size()); System.out.println(map.get(new Hoge("test1"))); System.out.println(map.remove(new Hoge("test1"))); System.out.println(map.size()); for(Hoge hoge2 : map.keySet()) { System.out.println(map.containsKey(hoge2)); } } static class Hoge { private String key; public Hoge(String key) { this.key = key; } /** * @param key the key to set */ public void setKey(String key) { this.key = key; } /** * @return the key */ public String getKey() { return key; } @Override public boolean equals(Object obj) { if(obj == null || !(obj instanceof Hoge)) { return false; } Hoge hoge = (Hoge)obj; if(this.key == null) { return hoge.key == null; } return this.key.equals(hoge.getKey()); } @Override public int hashCode() { return key.hashCode(); } } }
çµæ
1 value1 2 value2 2 value1 value1 1 false
keySet ãåãã¦ãã®ã« containsKey ã false ã¨ãã
æ¬å½ã®è¿·åã¡ãããåºæ¥ã¾ããï¼ããã¾ã