Java class notes------Map, queue, stack

Source: Internet
Author: User

*Java.util.Map*The map looks like a two-column table with multiple rows.*Each record is divided into two parts: Key,value*one of the keys in a map is not allowed to be duplicated. (Equals comparison)*  *Common Implementation classes: JAVA.UTIL.HASHMAP (hash algorithm implementation)*TreeMap (binary tree implementation) Map<String,Integer>Map=NewHashmap<string,integer>(); *v put (K k,v v)*The given key and value are stored in the map. *because key in map does not allow duplication, there are two scenarios:* 1: The key stored in the map does not already exist, then directly* key-value is stored in map with a return value of NULL* 2: The key stored in the map already exists, then the corresponding*value replaces the value of the original key and will be*the value returned for the replacement. Map.put ("Chinese", 98); Map.put ("Mathematics", 97); Map.put ("English", 95); Map.put ("Physics", 92); Map.put ("Chemistry", 96); *The order of deposit is inconsistent with the internal order Map.put ("Chemistry", 99); *V get (k k)*gets the corresponding value according to the given key*If the current map does not contain the given key, the return value is null Integer num= Map.get ("math")); *V remove (k k)*removes this pair of content from the map for the given key*and returns the corresponding value. Num= Map.Remove ("chemistry")); System.out.println ("Deleted value:" +num); *BooleanContainsKey (k k)*See if the current map contains a given key*The included judgment is the result of comparing the equals of keyBooleanContainsKey = Map.containskey ("Chinese"); *Traverse Map*There are three ways of traversing a map:* 1: Traverse all keys* 2: Iterates through each set of key-value pairs* 3: Traverse all the value (relatively infrequently used)*Linkedhashmap*internal use of a linkedlist maintenance sequence can be done*the traversal is consistent with the order of the put. If no order is required,*Typically, this class is not used. *Traverse all the keys:* set<k>KeySet ()*This method will return all the keys to a set set, so traversing the collection is the equivalent of traversing through all the keys set<String> KeySet =Map.keyset ();  for(String key:keyset) {System.out.println ("Key:" +key); }                         *iterate through each set of key-value pairs* set<entry>EntrySet ()*entry is the inner class of map, and each instance represents a set of key-value pairs*It contains two properties, Key,value. *after each set of key-value pairs (entry instances) is stored in a set set*to return it. Set<Entry<String,Integer>> EntrySet =Map.entryset ();  for(entry<string,integer>E:entryset) {String key=E.getkey (); Integer value=E.getvalue (); SYSTEM.OUT.PRINTLN (Key+":"+value); }                         * collection<v>values ()*returns all value values in the current map to a collection after Collection<Integer> values =map.values ();  for(Integer value:values) {System.out.println ("Value:" +value); }                                                                 *This class is used to practice overriding equals with hashcode considerations*When we use an instance of a custom type as the value of key in HashMap*, the result of this class equals and hashcode directly affects*hashmap efficiency of Queries*to avoid the creation of something:* Hashcode values, but equals does not compare to true!this will*Severe impact on hashmap efficiency*A rewrite of the two methods in the JAVA API documentation is required:*when we rewrite the Equals method of a class, it should be accompanied by a rewrite*Hashcode method. *It should also meet both:*When the equals of two objects is true, Hashcode returns the*values should be equal. In turn not mandatory, but preferably also different, otherwise affect*HashMap performance. *the Hashcode method should return a stable value, meaning that:*when the value of the property that determines the comparison result of the Equals method is not changed*The value returned by the Hashcode method should be the same if multiple calls are made. * Public classCell {Private intRow; Private intCol;  PublicCell (intRowintCol) {        Super();  This. Row =Row;  This. Col =Col; } @Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime * result +Col; Result= Prime * result +Row; returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL)            return false; if(GetClass ()! =Obj.getclass ())return false; Cell Other=(Cell) obj; if(Col! =Other.col)return false; if(Row! =Other.row)return false; return true; }        }         *Queue*save a set of elements, but for access requirements, you must follow the FIFO principle Queue<String> queue =NewLinkedlist<string>(); *BooleanOffer (e e)*appends an element to the end of the queue Queue.offer ("One"); Queue.offer ("Both"); Queue.offer ("Three"); Queue.offer ("Four"); *E poll ()*gets the element from the first team. Note that the element is retrieved from the* Removed from the queue!queue Operation String Str=Queue.poll ();         System.out.println (str); *E-Peek ()*You can also get the first element of the team, but unlike poll,*the element will not be removed from the queue. STR=Queue.peek (); System.out.println (str); *Stack: Store a set of elements, access must follow the advanced post-out principle* Typically used to implement a "back"function of the place. *Deque is the sub-interface of the queue. *the characteristic of a double-ended queue is that both ends of the queue can enter and exit elements. *if we only invoke one side of the access method, it forms a stack structure Deque<String> stack =NewLinkedlist<string>(); *voidpush (e E)* Press the element "into"stack, into the stack operation. Elements of the new stack*will be at the top of the stack (the first element in the stack) Stack.push ("One"); Stack.push ("Both"); Stack.push ("Three"); Stack.push ("Four"); *E pop ()*The stack operation, gets the top element of the stack, gets the element after the*the String str is deleted from the stack=Stack.pop ();         System.out.println (str); *You can also use peek to refer to the top element of the stack without making a stack operation str=Stack.peek ();        System.out.println (str); 

Java class notes------Map, queue, stack

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.