360 Written Questions-hashmap realization

Source: Internet
Author: User

Customizing a HashMap to implement the Map_put,map_delete,map_get method requires:

1. Find time complexity O (1)

2..

3..

Because Java comes with HashMap, usually directly use, also did not consider, a period of time just realized Arraylist,vetor,quene, and did not consider HashMap. The time of the written test is tight, I just define two ArrayList in HashMap, a save key, a save value, now think is definitely wrong, this is not implemented as required. The original intention of the topic is to enable the implementation of the list, the ability to investigate the operation of the list. When I came back, I thought about it, and realized the general list in Java:

<span style= "FONT-SIZE:14PX;"        >/** * * @author lip * with zipper method HashMap * Principle: There is a list array in HashMap * The size of the general array is a prime number */public class hashmap<t1,t2>{        private final int length=31;        Private ENTRY&LT;T1, t2>[]table; private int Size=0;public HashMap () {table=new entry[length];}        Insert value to HashMap public void put (T1 key,t2 value) {size++; The hash value of the T1 is calculated, and then P=hash (key)%length, the key is placed in the array in the list of P position///If the value exists in the map, then the value is updated//does not exist, then inserted in the last position of the list int pos=        Key.hashcode ()%length;          if (table[pos]==null) {table[pos]=new entry<t1,t2> (key,value);        Return        }//Traverse list to see if key-value already exists entry<t1, t2> Entry=table[pos];    Boolean exist=false; while (Table[pos]! = null) {if (Table[pos].key = = key)//exists, the update is OK {table[pos].value = Value;size--;exist = True;break;}        if (table[pos].next==null) break;else table[pos] = table[pos].next;} if (!exist) table[pos].next=new entry<t1,t2>(Key,value);                Table[pos]=entry;        }//delete a key public void Delete (T1 key) {int pos=key.hashcode ()%length;        ENTRY&LT;T1, t2> Entry=table[pos]; while (Entry!=null) {if (Entry.key==key) {//Deletes the current node entry<t1, t2>tempentry=entr        Y.next;        Entry=entry.next;        if (entry!=null) Entry.next=tempentry.next;        size--;        Break        } Entry=entry.next;        }}//Get key value public T2 GetValue (T1 key) {int pos=key.hashcode ()%length;        ENTRY&LT;T1, t2>entry=table[pos];        while (Entry!=null) {if (Entry.key==key) return entry.value;        Entry=entry.next;        } return null;        }//Get the collection of keys public set<t1> Getkeyset () {set<t1> set=new hashset<t1> (); for (int i=0;i<length;i++) {entry<t1, t2> entry=table[i];       while (Entry!=null) {set.add (Entry.key);        Entry=entry.next;        }} return set;        }//Get map size public int size () {return size; }class entry<t1,t2>{private T1 key;private T2 value;public entry<t1, T2>next;public Entry (T1 key,T2 value) {T His.key=key;this.value=value;}} public static void Main (string[] args) {//TODO auto-generated method stub hashmap<string, Integer>has                Hmap=new hashmap<string, integer> ();                Hashmap.put ("language", 82);                Hashmap.put ("Mathematics", 99);                Hashmap.put ("English", 90);                Hashmap.put ("Physics", 88);                Hashmap.put ("chemistry", 93);                Hashmap.put ("Biology", 86);                Hashmap.put ("Biology", 88);                System.out.println ("HashMap Size:" +hashmap.size ());                System.out.println ("Biology:" +hashmap.getvalue ("Biology"));   System.out.println ("Language:" +hashmap.getvalue ("language"));             Set<string> Set=hashmap.getkeyset ();                For (iterator<string> iterator=set.iterator (); Iterator.hasnext ();)                {String key=iterator.next ();                System.out.println (key+ ":" +hashmap.getvalue (key)); }}}</span>
Run

360 Written Questions-hashmap realization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.