between the key-value pairs is stored in the set collection, and the EntrySet traversal modesetEnhanced for .....IteratorsIterator while (It.hasnext ()) {System.out.println (It.next ());map.entryInteger key = Entry.getkey ();String value = Entry.getvalue ();System. out. println (key + "..... +value");}Note: I built the inputkeyandvalue() method in the Java project to create a map collection that calls the method in different methods to generate the M
One /*** Keyset () iterator with map (low performance) **/ Public voidCompareMap1 () {MapNewHashmapNewHashmapIteratorM1.keyset (). iterator (); while(Iter1.hasnext ()) {String M1key=(String) iter1.next (); if(!m1.get (M1key). Equals (M2.get (M1key))) {//if the value of the same key in the two map is not equal//...... } } }Two /*** Iterator with Map entryset () (High performance efficiency)*/ Public voidcompareMap2
, so thread is not safe, suitable for single thread, but if it is single-threaded, it is faster than StringBuffer.
13. Traverse HashMap using EntrySet
When you need to traverse HashMap, please try to use EntrySet, instead of using keyset,entryset more efficient than keyset, actually use EntrySet is only need to travers
Map set implemented by javascript and javascriptmap set
A few days ago, the project wanted to use a map set of the same things. It took a while to get the object. Today, I am idle to implement it.Don't laugh
Code
Var Map = function () {/************ basic variable ***************/var hashmap = {}; var keys = []; var vals = []; var entrys = []; var size = 0; var index = {}; var Entry = function (key, value) {var entryKey = key; var entryValue = value; this. getKey = function () {return entryKey;}
Reprinted: http://blog.sina.com.cn/s/blog_6e10b9110100qiuz.html
The complexity of containskey is O (1). It calculates the hashcode directly based on the given parameter key to see if there is any related location. If the location is occupied, search for the next location. The following is the main code for JDK to implement containskey: int hash = hash (k); int I = indexfor (hash, table. length); entry E = table [I]; while (E! = NULL) {If (E. hash = hash eq (K, E. key) return true; E = E. next;}
]If you add your own implementation of the Hashcode method and the Equals method in the person classpublicinthashCode(){ returnthis.id; } publicbooleanequals(Object obj){ person p=(person)obj; returnthis.id==p.id; }The result of the run isThe element of the collection [ID is: 123 name is: Zhang San]The traversal method of the set has the usual method of the for loop iterator EntrySet, etc.Package CN. Xlucas. List;Import J
impose restrictions on the type of their keys.
Abstract class AbstractMap
public abstract class AbstractMap
This class provides the backbone implementation of the Map interface, and its function is similar to the AbstractCollection in the Collection interface, all of which are required to implement the corresponding interface to the maximum extent. This class does not implement the entrySet () method, and limits the put (K key, V value) method.
To
hashMapTest1 ()
{
Map
Map. put (new Integer (1), "");Map. put (new Integer (2), "B ");Map. put (new Integer (3), "c ");
System. out. println (map );System. out. println (map. entrySet ());System. out. println (map. keySet ());System. out. println (map. values ());}
This outputs the Element Information in the HashMap, as shown below.Copy codeCode: {1 = a, 2 = B, 3 = c}[1 = a, 2 = B, 3 = c][1, 2, 3][A, B, c]
The following example demonstrates null:Cop
The following example shows how to traverse a collection of list, set, and map types stored as key-value pairs from the collection interface, using normal for, enhanced for, iterator, and so on to iterate through the collection:Import Java.util.arraylist;import java.util.hashset;import Java.util.iterator;import Java.util.List;import Java.util.Set; public class Main {public static void main (string[] args) {//List traversal listtest (); The traversal settest () of the set set; } priva
iteration, the String is
Converted to a stringbuffer/stringbuilder, appended to, and converted back to a String. This can leads to a
Cost quadratic in the number of iterations, as the growing string are recopied in each iteration.Better performance can be obtained by using a stringbuffer (or StringBuilder in Java 1.5) explicitly.string concatenation using methods in a loop +The method seems to be built in loops using string concatenation. In each iteration, the string is converted to a stringbu
the map. Both the key and the value can be null. However, you cannot add a map as a key or value to itself.The Map.entry interface (entry is the inner class in the map interface): The map's EntrySet () method returns an object set set that implements the Map.entry interface, where each object is a specific key-value pair in the underlying map. Implementation classes: HashMap, TreeMap2. Realization class1) ArrayList, LinkedListwhich of the two lists i
outermost layer JSONObject object = JSONObject. fromObject (json); for (Object k: object. keySet () {Object v = object. get (k); map. put (k. toString (), v);} Map
Map2 = new HashMap
(); // The second layer of resolution may or may not be for (Map. Entry
Entry: map. entrySet () {try {JSONArray array = new JSONArray (entry. getValue (). toString (); // determines whether it is a json array. // It is a json
calling the v8 interface through jni. js will write an article about WebKit js for special analysis later on WebKit. So far, LoadUrl simply uses a String to pass strings.
// WebCoreFrameBridge.cppstatic void LoadUrl(JNIEnv *env, jobject obj, jstring url, jobject headers){ WebCore::Frame* pFrame = GET_NATIVE_FRAME(env, obj); ALOG_ASSERT(pFrame, "nativeLoadUrl must take a valid frame pointer!"); WTF::String webcoreUrl = jstringToWtfString(env, url); WebCore::KURL kurl(WebCore::KURL(
"); // iterating through the collection for (Iterator+ "" + Person.getname ());}We can also declare a HashMap object and assign the HashMap object to TreeMap.New TREEMAP (map);Sorting by key value is simple, but because the key value is unique, cannot be duplicated, all, in the actual process may be used to sort value value;Sort Value values: (Descending)//declaring HashMap objectsmapNewHashmap();//Convert a Map collection to a list collectionlistNewArraylist(Map.entryset ());//Sort by Collecti
submits data to the network through HTTP protocol. For data of less than 2 K, post must be used. To use the get method, you need to append the parameter to the path.Public class NewsService{Public static boolean save (String title, String length){String path = "http: // 192.168.1.100: 8080/web/ManageServlet ";Map
Params = new HashMap
();
Params. put ("title", title );
Params. put ("timelength", length );
Try
{
Return sendGETRequest (path, params, "UTF-8 ");
} Catch
value setsBecause the map cannot contain duplicate keys, each key can be mapped to at most one value. Therefore, the key-value set, the keyset is set, and the value set is collection.Map provides methods such as key-value pair, get value by key, delete key, get capacity size, and so on.2:map.entry:Map.entry is defined as follows:Interface entryMap.entry is an internal interface in the map, Map.entry is a key-value pair , and Map obtains the Map.entry key-value pair collection through the
Java Map and Map. Entry
Map is an interface in java, and Map. Entry is an internal interface of Map.
Map provides some common methods, such as keySet () and entrySet.
The Return Value of the keySet () method is the Set of key values in the Map. The return value of entrySet () is also a Set, which is of the Map. Entry type.
Map. Entry is an internal interface declared by Map. This interface is generic and de
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.