Map
Map is used to save data that has a mapping relationship
Key and value correspond, one-way relationship
Key together makes up a set set (key cannot be duplicated, Map.keyset () returns this collection)
Most are the same as set sets.
The properties class is a subclass of the Hashtable class. Its objects are used to process the properties file (INI file under Windows)
Weakhashmap implementation class, keeping only weak references to actual objects by key
The IDENTITYHASHMAP implementation class differs from the ordinary implementation class in that the key is equal to Key1=key2; (same object) instead of equals of the normal HashMap (value equality)
Tool class for manipulating collections collections
The collection object can be sorted, queried, modified, and the collection object can be set to immutable, synchronous control of the collection object, etc. (All static class methods)
Generic type
Adding generic support is to a large extent to let the collection remember the data type of its elements
Use of generics
public class G
{
publicstatic void Main (string[] args) {
Create a list collection that just wants to save the string
List<string>strlist=new arraylist<string> ();
In fact, the above statement can be simplified to list<string>strlist=new arraylist<> (); Rib syntax
Strlist.add ("Generic Test");
Strlist.add (5); Compile error
}
}
Generics can also be defined by classes, interfaces, and methods (except for the parameters of the collection class instantiated above)
Public interface list<e>//e is just a type parameter
{
In this interface, E can be used as a type
Voidadd (E x);
Iterator<e>iterator ();
...
}
When defining an interface, specify a type parameter, E
public interface iterator<e>
{
Enext ();
Booleanhasnext ();
...
}
Two type parameters are specified when defining the interface, and its formal parameter name is K,V
public interface map<k,v>
{
Within this interface k,v can be used entirely as a type
Set<k>keyset ();
Vput (K key,v value);
...
}
We can add generic declarations for any class or interface (not only for collection classes)
eg
public class Apple<t>
{
Defining instance parameters using the T type parameter
Privatet info;
Publicapple () {};
constructor of type T to make arguments
Publicapple (T info) {
This.info=info;
}
Publicvoid setInfo (T info) {
THIS.INFO=OMFO;
}
Publict GetInfo () {
Returnthis.info;
}
publicstatic void Main (string[] args) {
Use
Apple<string>a1=new apple<> ("Apple");
System.out.println (A1.getinfo ());
Because the type passed to the T parameter is not correct
Apple<string>a2=new apple<> (31415926);
System.out.println (A2.getinfo ());
}
}
Generic class-derived subclasses are best trained for generic parameters
public class A extends apple<string>; If it doesn't, it might call the police.
Note: Type parameters are not allowed in the declaration and initialization of static methods, static initialization blocks, or static variables (type parameters are a surrogate, not a true type, non-static can)
Type wildcard character
If you want to define a method, there is a set parameter in the method, and the element type of the set parameter is indeterminate.
eg
public void Test (List c)
{
for (inti=0;i<c.size;i++)
{System.out.println (C.get (i));}
}
The list above is an interface with a generic declaration that does not pass in the actual type interface, and the generic warning
You can't do this
public void Test (list<object> c) {...}
If you use
List<string> strlist=newarraylist<> ();
Test (strlist); Error,list<string> object can not be used by list<object> object, no covariance
So to use the type wildcard (resolved)
public void Test (list<?> c) {...} Wildcard characters can be set to the upper limit such as: ... (List<?extends shape> c) ...
Lower... (LIST<? super Type> C) ...
The syntax of the wildcard with lower bounds makes it easy to flexibly select the appropriate comparator when creating TreeSet objects (convenient to pass all comparator as parameters)
The type parameter can actually set the upper limit ... (List<textends Shape & java.io.serializable> C) ...//& behind is the implemented interface
You can also use a generic method (upper and lower limits can also be used with the same wildcard character)
Static <T> voidfromarraytocollection (t[] a,collection<t> c)//Where this <T> is a declaration of a generic method instead of a return value
{...}
Unlike a generic parameter in a class or interface, a generic parameter in a method (a generic method) does not need to be explicitly passed in to the actual type parameter
generic method and type wildcard (if this parameter is used only once, it is better to use a type wildcard character)
Crazy Java Note 4: Collection Class (2)