??????Java????????У?HashSet??HashMap???????????????????ArrayList??LinkedList????????????????????????????????????????Vector??HashTable??????????????????????????????JDK???????????????????????????????????JDK?????????????Collections?????????????????????
?????????????
????1.??????HashMap??
??????Thinking In Java???????????????????????????????key-value??demo?????????????????????????????????????????????????????洢???????????????е????????????????????????????飬????????????????????е???????????????????????????????Java??????equals????==??????????????????????
????2.HashMap????????????
????Hash?????????????????????????????????????????????????????????????????????????????????????
????3.Java?е?Hash??
?????????Java???????????????????????????????(????C??????????????????飬??Щ??????2??????????????????????顣??????????????????????????κ??????????Java?У?ArrayList??????????Objec???????????????HashMap????????????????????????????????????????????????Entry?????????????key??value??hash(????hashCode)??next(????????????????)?????HashSet???HashMap????????????????????Entry??value???????key?????????HashSet?????????
????public HashSet() {
????map = new HashMap<E??Object>();
????}
??????????????????????????????HashSet?????????????????HashMap?????
????HashMap???
?????????????????hash????????????????????3???????????????????length?????飬??????С??length??????????????п?????????????????????????????16???????д???????(?????????HashMap???????)??????????????????????hashCode()?????????????????hashCode????????HashMap????????????????????????е?λ??????????? i??
????int hash = hash(key.hashCode());
????int i = indexFor(hash?? table.length);
???????????????hashCode???????2???????????????i?????i?????????е?λ???????????????????indexFor(hash??table.length)?????????
????static int indexFor(int h?? int length) {
????return h & (length-1);
????}
????????????????????h??(length-1)??λ????????????????????length??????????????table???????????????????????????????????????????????С??????С????????????????????ο????????????????Щ????????????????????table?е?λ???????????????????????????hashCode()?????????????????????????????????hashCode???????????????????????????????????????????????????????????????????????λ????????????????????洢?????hashCode?????????????????HashMap??size??????????1????????2????????????????????????С?HashMap?е?size???????????????????????????size()??????????map??????
????public int size() {
????return size;
????}
????????????????????????hash???????????????????????HashMap????????п????????hashmap????????????table????capacity????????????????????threshold??????????????????????????????????
????public HashMap() {
????this.loadFactor = DEFAULT_LOAD_FACTOR;
????threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
????table = new Entry[DEFAULT_INITIAL_CAPACITY];
????init();
????}