"Concept of containers"
Container: A series of instances provided by the Java API for storing objects in a program.
"Container API"
The container API provided by J2SDK is located in the Java.util package.
{Collection[set (hashset,linkedlist), Srraylist]} {Map[hashmap]}
Collection interface-Defines a method for accessing a set of objects, and its sub-interface set and list define the storage method separately.
Data objects in set are not sequential and cannot be duplicated
The data objects in the list are sequential and repeatable
The map interface defines the method that stores the key (key)-value mapping pair.
"Collection Interface"
The methods defined in the collection interface are:
int size ();
Boolean isEmpty ();
void Clear ();
Boolean contains (Object element);
Boolean Add (Object element);
Boolean remove (Object element);
Iterator Iterator ();
Boolean containsall (Collection c);
Boolean AddAll (Collection c);
Boolean RemoveAll (Collection c);
Boolean retainall (Collection c);
Object[] ToArray ();
Container class objects need to compare objects for equality when calling remove, contains, and so on, which involves the Equals method and the Hashcode method of the object type, and for a custom type, You need to override the Equals and Hashcode methods to implement custom object equality rules.
Note: Equal objects should have equal hash codes.
Add the Equals and Hashcode methods for the name class as follows:
public boolean equals (Object obj) {if (obj instanceof name) {name name = (name) Obj;return (Firstname.equals (name.firstname ) && (Lastnmae.equals (Name.lastname));} return super.equals (obj); public int hashcode () {return Firstname.hashcode ();}
"Iterator Interface"
All container classes that implement the collection interface have a iterator method that returns an object that implements the iterator interface.
The iterator object is called an iterator to facilitate the traversal of elements within the container.
The iterator interface defines the following methods:
Boolean hasnext (); Determine if there are elements to the right of the cursor
Object next (); Returns the element to the right of the cursor and moves the cursor to the next position
void Ermove (); Removes the element to the left of the cursor, which can only be executed once after next has been executed
The Remove method of the Iterator object is the only security method that removes the element during the iteration.
"JDK1.5 Enhanced for Loop"
Instance:
Import java.util.*;p Ublic class Enhancedfor {public static void main (string[] args) {int[] arr = {1,2,3,4,5};for (int I:arr ) {System.out.println (i);} collection<string> C = new arraylist<string> (), C.add (New string ("AAA")), C.add (New string ("BBB")); C.add ( New String ("CCC")); for (Object o:c) {System.out.println (o);}}}
Note: Generics are used after Java1.5. (e.g. <String> specifies the type of container storage element)
The enhanced for loop is fairly simple for traversing an array or collection
Defects:
Array:
Unable to easily access subscript values
Collection:
It is not easy to delete the contents of the collection compared to using iterator
Inside is also called iterator
Summary: In addition to simply traversing and reading out the contents, it is not recommended to use the enhanced for
"Set Interface"
The set interface is a sub-interface of the collection, and the set interface does not provide an additional method, but the elements in the container class that implement the set interface are not sequential and cannot be duplicated.
The set container can correspond to the concept of "set" in mathematics.
The Set container classes provided in the J2SDK API are Hashset,treeset and so on.
"Set Method Example"
Import java.util.*;p Ublic class Test {public static void main (string[] args) {set<string> S1 = new hashset<string > (); set<string> s2 = new hashset<string> () S1.add ("a"); S1.add ("B"); S1.add ("C"); S2.add ("D"); S2.add ("a"); S2.add ("B");//set and list container classes all have constructor (Collection c)//constructor method used to initialize the container class set<string> sn = new hashset<string> (s1); Sn.retainall (S2); set<string> su = new hashset<string> (s1); Su.addall (S2); SYSTEM.OUT.PRINTLN (SN); System.out.println (su);}}
"List Interface"
The list interface is a sub-interface of the collection, and the elements in the container class that implement the list interface are ordered and can be duplicated.
The elements in the list container correspond to the ordinal of an integer that records its position in the container, and the elements in the container can be accessed according to the sequence number.
The list container class provided by J2SDK has arraylist,linkedlist and so on.
Object get (int index);
Object set (int index,object element);
void Add (int index,object element);
Object Remove (int index);
int indexOf (Object o);
int lastIndexOf (Object o);
"List common algorithms"
Class Java.util.Collections provides some static methods to implement some common algorithms based on the list container.
void sort (list) sorts the elements in the list container
void Shuffle (list) randomly sorts objects within a list container
void Fill (List,object) overrides the entire List container with a specific object
void copy (list dest,list src) copies the SRC list container contents to the Dest list container
int BinarySearch (list,object) for a sequential List container, find a specific object using this method of finding
"List Common algorithm examples"
Import java.util.*;p Ublic class Test {public static void main (string[] args) {list<string> L1 = new Linkedlist<st Ring> (); list<string> L2 = new linkedlist<string> (); for (int i=0;i<=9;i++) {L1.add ("a" +i);} System.out.println (L1); Collections.shuffle (L1); Random sort System.out.println (L1); Collections.reverse (L1); Reverse System.out.println (L1); Collections.sort (L1); Sort System.out.println (L1); System.out.println (Collections.binarysearch (L1, "A5")); Binary Find}}
"Comparable Interface"
Question: What does the algorithm above determine the "size" order of the objects in the container?
All classes that can be "sorted" implement the Java.lang.Comparable interface, with only one method in the comparable interface
public int CompareTo (Object obj); This method:
Returns 0 indicating this = = obj
Returns a positive number indicating this > obj
Returns a negative number indicating this < obj
A class that implements the comparable interface determines how the class objects are sorted by implementing the CompareTo method.
"How to choose a data structure"
Metrics: efficiency of reading and efficiency of change
Array degree fast cover full
Linked the Fast Duman
Hash between the two
"Map Interface"
The class that implements the map interface is used to store key-value pairs.
The implementation classes of the map interface are HashMap, TreeMap, and so on.
Key-value pairs stored in the map class are identified by building, so the key values cannot be duplicated.
Object put (object Key,object value);
Object get (object key);
Object remove (object key);
Boolean ContainsKey (Object key);
Boolean Containsvalue (Object value);
int size ();
Boolean isEmpty ();
void Putall (Map t);
void Clear ();
"Map Method Example"
Import java.util.*;p Ublic class Test {public static void main (string[] args) {map<string,integer> m1 = new HASHMAP&L T String,integer> (); map<string,integer> m2 = new treemap<string,integer> () m1.put ("One", New Integer (1)), M1.put ("One", new Integer (2)), M1.put ("Three", New Integer (3)), M1.put ("A", New Integer (1)), M2.put ("B", New Integer (2)); System.out.println (M1.size ()); System.out.println (M1.containskey ("one")); System.out.println (M2.containsvalue (New Integer (1))), if (M1.containskey ("II")) {int i = ((Integer) M1.get ("n")). Intvalue (); System.out.println (i);} map<string,integer> m3 = new hashmap<string,integer> (m1); M3.putall (m2); System.out.println (m3);}}
"Auto-boxing/unboxing"
Automatic packaging at the right time, Jaguar
Automatically convert the underlying type to an object
Automatically convert an object to the underlying type
"JDK1.5 generics"
Cause:
JDK1.4 previous types are ambiguous:
The type of the mounted collection is treated as object, thus losing its actual type.
When removed from the collection, it is often necessary to transform, inefficient and prone to errors.
Workaround:
Defines the type of objects in the collection at the same time as the collection is defined
can be defined when collection is specified
You can also specify it with iterator in the loop
Benefits: Enhances readability and stability of the program.
Summary
A graph; a class collections; three points of knowledge for,generic,auto-boxing/unboxing; six interfaces
Java Summary of the container