Container of Java

Source: Internet
Author: User
Tags comparable map class sorts

Container
The collection interface defines a method for accessing a set of objects, and its sub-interface set and list define the storage mode separately.
· Data objects in set are not sequential and cannot be duplicated HashSet
· The data objects in the list are sequential and can be repeated linkedlist (underlying list) ArrayList (underlying array)

· Array read fast change slow
· Linked fast reading Slow
· Hash between the two

The map interface defines a method for storing key (key)-Value mapping pairs "HashMap

Methods defined in the ①collection interface
int size ()
Boolean IsEmpty ()
void Clear ()
Boolean contains (object element)//contains element elements
Boolean Add (object Element)
Boolean remove (object element)
Iterator Iterator ();
Boolean containsall (Collection C)//contains all elements of the set C
Boolean AddAll (Collection c)
Boolean RemoveAll (Collection C)//Remove all elements inside the set C
Boolean retainall (Collection C)//intersection with another set
Object[] ToArray ()

②iterator interface in order to traverse the collection method uniformly,
• All container classes that implement the collection interface have a iterator method to return 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 on the right side of the cursor
object Next ()//returns the element to the right of the cursor and moves the cursor to the next position
void Remove ()//delete the element to the left of the cursor, which executes only once after next
Supplemental: Enhanced for loop
Defects:
• Arrays: No easy access to subscript values
• Collection: It is not easy to delete the contents of the collection compared to using iterator
Summary: In addition to simply traversing and reading the contents
Int[] arr = {1,2,3,4,5};
for (int i:arr) {
System.out.println (i);
}

Collection C = new ArrayList ();
C.add ();
C.add ();
C.add ();
for (Object o:c) {
System.out.println (o);
}


③set interface
· Collection, 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, etc.

④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 an integer ordinal indicating their 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.
Common algorithms for list containers
void sort (list) sorts the elements in the list container
void Shuffle (list) randomly sorts the objects in the list container
void Reverse (list) lists the objects in the list container in reverse order
void Fil (List, object) overrides the entire List container with a specific object
coid Copy (list dest, list src) copies the SRC list container contents to the Dest list container
int BinarySearch (list,object) for sequential List container, use binary Find method to find specific object

⑤comparable interface
• All classes that can be "sorted" implement this method,
• Only one method public int compareTo (object obj);
Returns 0 indicating this = = obj
Returns a positive number indicating this > obj
Returns a negative number indicating this < obj

⑥map interface
• Classes that implement the map interface are used to store key-value pairs
· The implementation classes of the map interface are HashMap and TREEMAP, etc.
· Key-value pairs stored in the map class are identified by key, so 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 ()

Automatic packaging auto-boxing/unboxing
• Automatically package and unpack at the time of verification
• Automatically convert objects to the underlying type



Generic generic
Cause:
· JDK1.4 previous types are ambiguous:
• The type of the mounted collection is treated as an object, thus losing its actual type
• Often need to be transformed when removed from the collection, inefficient and prone to errors
Solutions
• Define the type of objects in the collection while defining the collection
Instance
• Specify when collection can be redefined
• Can also be specified with iterator at cycle time
list<string> C = arraylist<string> ();
Iterator<string> it = C2.iterator ();


Benefits
• Enhanced readability and stability of the program


Add: Object is the parent class of all classes, some of which
ToString () method
public String toString () describes information about the current object
• When a string is connected to another type of data (for example: System.out.println ("info" + person), the ToString () method of the object class is called automatically
• You can override the ToString () method in a user-defined type as needed

Hashcode () method
Hash code

The Equals () method is equivalent to "= =", so the Equals () method is overridden in most classes
public boolean equals (Object obj)

Compare objects for equality, overriding
public boolean equals (Object obj) {
if (obj = = null) return false;
else{
if (obj instanceof Class) {//class class, determine if obj belongs to class
Class C = (Class) obj;
if (C.color = = This.color && ...) {//All properties are the same
return true;
}
}

}
}



Summary: 1136
• A diagram
Collection Map
/    \                 |
Set list HashMap
/        /   \
HashSet arrayList LinkedList
• One class
· Collections
• Three points of knowledge
for
· Generic (generic type)
· Auto-boxing/unboxing
• Six interfaces
Colleciton
Set
List
Map
· Iterator
comparable

Container of Java

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.