Map <K, V>: The ing between key-value pairs. The key is unique.
| Hashtable: The underlying layer is the data structure of the hash table,The null key cannot be saved. synchronized (1.0)
| -- Treemap: The underlying layer is a binary tree and threads are not synchronized,It can be used to sort keys in the map set.
Common map methods:
1: Add:
Put (K, V)
Putall (Map <? Extends K, <? Extends v> m)
2: delete:
Clear ()
3: judgment:
Containskey (Object key)
Containsvalue (object value)
Isempty ()
4: get:
Get (Object key)
Size ()
Values ()
Entryset ()
Keyset ()
Import java. util .*;
Class demo
{
Public static void main (string [] ARGs)
{
Map <string, string> map = new hashmap <string, string> ();
Map. put ("01", "zhangsan1"); // the same key when the key is added. The value added later will overwrite the original value, and the put method returns the overwritten value.
Map. Put ("02", "zhangsan2 ");
Map. Put ("03", "zhangsan3 ");
System. Out. println (Map. containskey ("012"); // false
System. Out. println (Map. Remove ("02"); // return (after deletion) zhangsan2
System. Out. println (Map. Get ("01"); // obtain zhangsan1. You can use the returned value to determine whether a key exists. return NULL to determine whether a key exists.
Map. Put ("04", null); // added successfully, but hashtable cannot
Collection <string> coll = map. Values ();
System. Out. println (Coll); // [null, zhangsan1, zhangsan3]
}
}
Import java. util .*;
Class demo {
Public static void main (string [] ARGs ){
// Get all the values by getting all the keys. He does not have the iterator keyset () or set, which is the set. // It has an iterator and can be used to remove all the keys by iteration, obtain another value
Map <string, string> map = new hashmap <string, string> ();
Map. put ("01", "zhangsan1"); map. put ("04", "zhangsan2"); map. put ("02", "zhangsan2"); map. put ("03", "zhangsan3 ");
Set <string> keyset =Map. keyset ();
Iterator <string> it = keyset. iterator ();
While (it. hasnext ()){
String key = it. Next ();
String values = map. Get (key );
System. Out. println (Key + ":" + values );}
// Set <map. Entry <K, V> entryset. The second method is used to retrieve the map. Entry returned by the ing relationship.
Set <map. Entry <string, string> entryset =Map. entryset (); // Also returns a set
Iterator <map. Entry <string, string> it1 = entryset. iterator ();
While (it1.hasnext ()){
Map. Entry <string, string> me = it1.next (); // check the type of the map. Entry lock of the API object.
String key = me. getkey ();
String values = map. Get (key );
System. Out. println (Key + ":" + values );
}
}
}
Exercise: store student objects in a collection. A student of the same age has the same name. What if there is a hash table structure and a binary tree structure?
Import java. util .*;
Class studentImplements compareable <student>
{
Private string name;
Private int age;
Student (string name, int age)
{
This. Name = Name;
This. Age = age;
}
Public String getname ()
{
Return name;
}
Public int getage ()
{
Return age;
}
Public String tostring ()
{
Return name + ":" + age;
}
/* In this case, there is a hash table structure set */
Public int hashcode ()
{
Return name. hashcode () + age * 32; // age multiplied by a number ensures the uniqueness of the hash value
}
Public Boolean equals (Object OBJ)
{
If (! (OBJ instansof student ))
Throw new classcastexception ("type conversion exception ");
Student s = (student) OBJ;
Return this. Name. Equals (S. Name) & this. Age = S. Age;
}
/* In this case, a binary collection exists.Natural orderTo implement compareable */
Public int compareato (student s)
{
Int num = new INTEGER (this. Age). compareto (New INTEGER (S. Age ));
If (num = 0)
Return this. Name. compareto (S. Name );
Return num;
}
Class maptest
{
Public static void main (string [] ARGs)
{
Hashmap <student, string> Hm = new hashmap <studeng, string>
Hm. Put (new student ("lisi1", 21), "Beijin ");
Hm. Put (new student ("lisi2", 22), "Shanghai ");
Hm. Put (new student ("lisi2", 22), "Shanxi"); // the key is not unique, but the preceding two methods can be rewritten.
Hm. Put (new student ("lisi3", 23), "Tianjin ");
Hm. Put (new student ("lisi4", 24), "Wuhan ");
// Method 1
Set <student> keyset = hm. keyset ();
Iterator <student> it = keyset. iterator ();
While (it. hasnext ())
{
Student Stu = it. Next ();
String adrr = hm. Get (Stu );
System. Out. println (STU + ":" + adrr );
}
// Entryset
Set <map. Entry <student, string> entryset = hm. entryset ();
Iterator <map. Entry <student, string> iter = entryset. iterator ();
While (ITER. hasnext ())
{
Map. Entry <student, string> me = ITER. Next ();
Student Stu = me. getkey ();
String adrr = hm. Get (Stu );
System. Out. println (STU + ":" + adrr );
}
}
}
Class maptest2 // This method can implement natural sorting
{
Public static void main (string [] ARGs)
{
Treemap <student, string> TM = new treemap <studeng, string>
TM. Put (new student ("lisi1", 21), "Beijin ");
TM. Put (new student ("lisi2", 22), "Shanghai ");
Hm. Put (new student ("lisi2", 22), "Shanxi"); // the key is not unique, but the preceding two methods can be rewritten.
TM. Put (new student ("lisi3", 23), "Tianjin ");
TM. Put (new student ("lisi4", 24), "Wuhan ");
// Entryset
Set <map. Entry <student, string> entryset = TM. entryset ();
Iterator <map. Entry <student, string> iter = entryset. iterator ();
While (ITER. hasnext ())
{
Map. Entry <student, string> me = ITER. Next ();
Student Stu = me. getkey ();
String adrr = hm. Get (Stu );
System. Out. println (STU + ":" + adrr );
}
}
}
//Customizes a name-based comparator and sends it to the call function.
Class stunamecomparator implements comparator <student>
{
Public int compare (student S1, student S2)
{
Int num = s1.getname (). comparato (s2.getname (); // if the names are the same, kill u is sorted by the age of the secondary condition. If the ages are equal, you cannot add
If (num = 0)
Return new INTEGER (s1.getage (). compareto (New INTEGER (s2.getage ()));
Return num;
}
}
Map set extension:Set nesting: Chuanzhi podcasts and classes are a set, and classes and students are a set.
Tool set:
CollectionIs a top-level interface in the Collection framework, which defines the common methods of a single column set. It has two common sub-interfaces: List: defining indexes for all elements. Ordered. Elements can be repeated. Set: No
Duplicate element. Unordered.
CollectionSIs a tool class in the Collection framework. The methods in this class are static provided, including sorting the list set and binary search. Generally, common collections are thread-insecure. Because
High efficiency. If multiple threads operate on these sets, you can use the synchronization method in this tool class to convert the sets with unsafe threads into secure ones.
In the collection interfaceToarrayMethod
How long does an array of the specified type need to be defined? :If the length of an array of the specified type is smaller than the size of the set, a new array is created in this method. The length is the size of the set. When the length of an array of the specified type is
If the value is greater than the size of the set, no new array is created. Instead, use the passed array. Therefore, it is best to create an array that is just right.
Why do we need to change the set to an array? :To restrict operations on elements. No addition or deletion is required.
Arraylist <string>Al= New arraylist <string> ();
Al. Add ("ABC1"); Al. Add ("abc2"); Al. Add ("abc3 ");
String [] arr =Al. toarray(New String [Al. Size()]);
Aslist: Converts an array to a list set.
What are the advantages of converting an array into a list set? :You can use the idea and method of the set to operate the elements in the array.Note: changing an array to a set does not allow you to add or delete a set because the length of the array is fixed.
.Otherwise, unsupportedoperationexception occurs.
If all elements in the array are objects. When it becomes a set, the elements in the array are directly converted into elements in the set. If all elements in the array are of the basic data type, the array is used as the collection
.
String [] arr = {"ABC", "cc", "KKKK "};
List<String>List =Arrays. aslist (ARR); // There are generic
Enhance the For Loop:Only collection elements can be obtained. However, the set cannot be operated.
For (data type variable name: The retrieved collection or array)
{
}
Arraylist <string> Al = new arraylist <string> (); Al. add ("ABC1"); Al. add ("abc2"); Al. add ("abc3 ");
For (string S: Al)
{
System. Out. println (s );
}
Variable parameters (type... ARGs ):Variable parameters must be defined at the end of the parameter list.
Public static void show (string Str, Int... arr)
{
System. Out. println (ARR. Length );
}
Static import:When the class name is duplicate, you must specify the specific package name. When the method name is duplicate, specify the object or class to which it belongs.
Import static java. util. arrays .*;