exclusive or operation .}
Public static class SimpleImmutableEntry
An Entry defined as immutable is actually immutable because it does not provide the setValue method. It cannot be modified by using the setValue method when multiple threads access it at the same time. Compared with SimpleEntry, its key and value member variables are defined as the final type. An UnsupportedOperationException will be thrown when the setValue method is called.
Its equals and hashCode methods are consistent with
the comparator:Map Public int compare (string obj1, string obj2 ){// Sort in descending orderReturn obj2.compareto (obj1 );}});Map. Put ("month", "The Month ");Map. Put ("Bread", "the bread ");Map. Put ("attack", "the attack ");Set Iterator While (ITER. hasnext ()){String key = ITER. Next ();System. Out. println (Key + ":" + map. Get (key ));}If you want to sort treemap by value, or sort hashmap, hashtable, and linkedhashmap, you can use the map. Entry Eg.1 for treemap in ascending order of val
feature implementations:A. Sorting by valueHashMap sorted by value by collections the Sort method, before implementing the sort, let's look at some of the HashMap traversal methods:Collectionand Map public static void Testcm () {//collection Map; int i =0; Hs. put (199,"Serial Number:" +201); while (i) {HS. put (I,"Serial number:" +i); i++; } HS. Put (-1,"Serial Number:" +200); Hs. put (200,"Serial Number:" +200); Traversal mode one: For each traversal hashmap
)); } }EntrySet entryset traversal for map , You can also get key and value at the same time, in general, performance is better than the previous one, which is also most commonly used traversal method // EntrySet get key and value Public void Testentry () { for (Map.entry entry:map.entrySet () ) { System. out. println (entry.getkey () + ":
The first is a small example of traversal:
public static void Main (string[] args) {
//TODO auto-generated method Stub
map
As for the performance of the two:
The test found that the performance of the second approach is usually one-fold higher than the first method.
Examples are as follows:
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map.Entry; /** * Test keyset () and
:
abstract void Clear ()
Abstract Boolean ContainsKey (Object key)
Abstract Boolean Containsvalue (Object value)
Abstract setAbstract Boolean equals (object)
Abstract V get (Object key)
abstract int hashcode ()
Abstract Boolean IsEmpty ()
Abstract setAbstract v put (K key, V value)
abstract void Putall (mapAbstract V Remove (Object key)
abstract int size ()
Abstract collection
Description
The map provides an interface for returning a keyset, a set of values, or a key-value mappin
Map stores data in the form of key/value pairs. Similar to arrays, indexes in arrays are also objects.
Map Interface
Map --- implement Map
Map. Entry -- Internal class of map, which describes the key/value pairs in map.
Sortedmap --- extended map, enabling keys to be sorted in ascending order
Generally, map subclass is selected instead of map class.
The following uses hashmap as an example.
Public static void main (string ARGs [])
{
Hashmap = new hashmap ();
Hashmap. Put ("item0", "value0 ");
H
Traverse hashmap
Hashmap attmap;
Iterator I = attmap. entryset (). iterator ();
While (I. hasnext ()){Object o = I. Next ();String key = O. tostring ();// You can traverse the key value of the hashmap.
}
Of course, you can also traverse map. Entry items and values. The method is similar.Yes.Object [] obja = attmap. keyset (). toarray ();
According to our support, it is faster to use entryset to tr
, but the same as the collection, map and collection still have a lot of features similar or even the same function, the following table compares the following:
comparison of methods for map and collection interfaces
Method description
Map Method Name
Collection Method Name
Number of elements in the container
Size ()
Size ()
Determine if the container is empty
IsEmpty ()
IsEmpty ()
adding elements
or operation. 3}
Public static class SimpleImmutableEntry
An Entry defined as immutable is actually immutable because it does not provide the setValue method. It cannot be modified by using the setValue method when multiple threads access it at the same time. Compared with SimpleEntry, its key and value member variables are defined as the final type. An UnsupportedOperationException will be thrown when the setValue method is called.
Its equals and hashCode methods are consistent with SimpleEnt
In Java map According to the different data structure, there are many different implementations, common such as hash HashMap, linked list linkedmap, hash list linkedhashmap, tree table (binary tree) TreeMap.
In this paper, we discuss several different traversal methods of the HASHMAP hash list which are most commonly used in our programming, and compare the writing and efficiency among them.
First, prepare a map hash table, storing 100,000 records.
Key is from Key0 start to key99999
Value start
you want to iterate all of its elements (whatever the reason), you first need to obtain the "View" of the map ". There are three possible views (seeTable 3)
All key-value pairs-see entryset ()
All keys-see keyset ()
All values-see values ()
The first two views return the set object, and the third view returns the collection object. In both cases, the problem is not solved because you cannot directly iterate the collection ob
", "111"); map. put ("bbb", "222"); // because generic is used, so the key and value types must be String // retrieve the map element ----- two // (1) traverse the Map through keySet (2) traverse the Map through EntrySet to retrieve each key-value Pair Map. entry defines a key-value pair to traverse through the getKey and getValue of the Entry // The first method is to traverse the Set through the Map keySet.
Keys = map. keySet (); for
First look at the source code: Map is often used in the source code/** * Returns a {@code Set} containing all of the mappings in this {@code Map}. Each mapping are * An instance of {@link map.entry}. As the {@code Set} is backed by this {@code Map}, * Changes in one would be reflected in the other . * * @return A set of the mappings * * 1.EntrySet () is used to return a Set/** * Returns A set of the keys contained in thi
count;54}Public Boolean contains (Object o){ContainsKey return (o);57}+/-Public boolean remove (Object o){59return Hashtable.this.remove (o)! = null;60}public void Clear (){62Hashtable. This. Clear ();63}64}65 an implementation of the Iterator interface method in the internal class keyset, called the Getiterator (KEYS) of the host class{if (count = = 0){(iterator---} else{enumerator71}72}In 73getIterator, a new inner class enumerator object is used to access the entry array of Hashtable using e
is suitable for modifying the redundant view, the method of the chain head and the tail of the chain.3.HashSet:Elements cannot be duplicated, so require elements to override the Hashcode and Equals methodsadd(E e)remove(Object o) 遍历:1、foreach 2、Iterator4.TreeSet:Elements can be sorted or provide a sort of business class5.HASHMAP:Key cannot be repeated must override Hashcode+equals method @**@put(K key, V value)、Remove(Object key)、ContainsKey(Object key)、Containsvalue(Object value)Traversal: Ge
Package com.xiawei.sort;Import java.util.ArrayList;Import java.util.Collections;Import Java.util.Comparator;Import Java.util.HashMap;Import java.util.List;Import Java.util.Map;Import Java.util.Map.Entry;public class TestString {Requires that characters in a string be sorted from small to large by character occurrencesIdea: 1. Converts a string into an array of characters in the form of a single character;2.for iterates through this character array, and each character is stored as a key in the ma
=arrays.copyof (Elementdata, newcapacity); } } ArrayList each time a new element is added, it will detect whether the current capacity of ArrayList has reached the critical point, and if it reaches the critical point, it will expand 1.5 times times.However, the expansion of ArrayList and the creation of a new array of copies of the array are quite resource-intensive.On the premise of large data volume, it is more advantageous to specify the capacity of initialization, the improvemen
Definition of Map interface map interface in Java: public interfacemapIt is obvious that this is a generic interface and accepts two parameters: K, v. K represents key,v for value. Map stores a series of key-value pairs, each of which is mapped to a value.Some points to note in the map interface:
A key can only be mapped to a value
Cannot contain duplicate keys
The structure of the map is unordered and cannot be accessed by subscript
The map interface provides three collect
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.