The Java collection is divided into three interfaces, the collection,map,iterator, the collection interface and the class in the Java.util package, the main
The collection interface to describe one of the three Interfaces.
Some collection allow repeating elements while others do not, some collection are ordered, others are unordered
Of Collection does not provide any direct implementation of the interface, it provides more specific sub-interfaces list and Set.
1.List interface
The list is an ordered collection, where each element of the user queue table of this interface is precisely controlled by the insertion position, which can be based on the element
The integer index accesses the element and searches for the element in the List.
The list interface implementation class mainly Includes: Arraylist,vector,linkedlist
1) ArrayList
1. The default ArrayList size is 10
650) this.width=650; "src=" http://img.mp.itc.cn/upload/20160804/f7fd762a3ce444b9b5c499a363d1ca92.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
2.ArrayList internal implementations are using dynamic arrays
3.ArrayList is thread insecure
4. If the array is full, you need to expand dynamically, the expansion length is about half the length of the original array, the expansion requires a new number
group, and then copy the original data back.
650) this.width=650; "src=" http://img.mp.itc.cn/upload/20160804/4de4eaff776e4188ba2c8c54925a7ce4.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
ArrayList use Example:
Java code
public static void main (string[] Args) {
String okstring=null;
List<string>lists=new arraylist<string> ();
Lists.add ("aa");
Lists.add ("aa");
Lists.add (1, "bb");//specify Position Insertion
Lists.add ("cc");
Lists.add (okstring);
Check if ArrayList is empty
System.out.println (lists.isempty ());
Finds the first occurrence of a specified element
System.out.println (lists.indexof ("aa"));
See if this element is included
System.out.println (lists.contains ("cc"));
The length of the output list
System.out.println (lists.size ());
Output
For (int i=0;i<lists.size (); I++)
System.out.println (lists.get (i));
Clear List
Lists.clear ();
}
Conclusion: Arra can get the data directly through subscript, the data reading is very convenient, however, the insertion and deletion of ArrayList will result in
A large number of data shifts can affect performance. If we already know the number of elements required, we can initialize the specified ArrayList
capacity, which effectively avoids multiple expansions of the array, which increases efficiency, but cannot initialize too large and waste memory.
2) Vector
Vector classes can implement an array of objects that grow, and, like arrays, can use subscripts to access data directly. The size of the vector can be
To expand or shrink as Needed.
Vector internal use of dynamic array implementation
2. The default construction size is 10 and the increment is 0. you can specify the size and increment vector in the construction method (int Size,int Increment)
650) this.width=650; "src=" http://img.mp.itc.cn/upload/20160804/1bff801101a249df86ee021c25ebb287.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
3. Expansion Mode: If there is a specified increment, it is the current capacity + increment, if the increment equals 0, then is the current capacity;
650) this.width=650; "src=" http://img.mp.itc.cn/upload/20160804/9de75251264d4544977fdedf36b797ce.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
4.Vector is Thread-safe
Vector usage is almost the same as arraylist, sample code
Java code
public static void main (string[] Args) {
String okstring=null;
List<string>vectors=new vector<string> ();
Vectors.add ("aa");
Vectors.add ("aa");
Vectors.add (1, "bb");//specify Position Insertion
Vectors.add ("cc");
Vectors.add (okstring);
Check if ArrayList is empty
System.out.println (vectors.isempty ());
Finds the first occurrence of a specified element
System.out.println (vectors.indexof ("aa"));
See if this element is included
System.out.println (vectors.contains ("cc"));
The length of the output list
System.out.println (vectors.size ());
Delete element;
Vectors.remove (0);
Output
For (int i=0;i<vectors.size (); I++)
System.out.println (vectors.get (i));
Clear List
Vectors.clear ();
}
3) LinkedList
LinkedList is the list Interface's linked list Implementation. Implements all optional list operations and allows all elements to include Null. Its basic
The usage is similar to arraylist, such as:
Java code
public static void main (string[] Args) {
String okstring=null;
List<string>link=new linkedlist<string> ();
Link.add ("aa");
Link.add ("aa");
Link.add (1, "bb");//specify Position Insertion
Link.add ("cc");
Link.add (okstring);
Check if ArrayList is empty
System.out.println (link.isempty ());
Finds the first occurrence of a specified element
System.out.println (link.indexof ("aa"));
See if this element is included
System.out.println (link.contains ("cc"));
The length of the output list
System.out.println (link.size ());
Delete element;
Link.remove (0);
Output
For (int i=0;i<link.size (); I++)
System.out.println (link.get (i));
Clear List
Link.clear ();
}
ArrayList and vectors are implemented using dynamic arrays, and vectors can specify increments compared to arraylist, while LinkedList is
Implemented with a linked List. Their differences are mainly reflected in the difference between the array and the linked list
2.Set interface
A colletion that does not contain duplicate Elements. That is, the set does not contain an element that satisfies e1.equals (e2), and the set contains a maximum of one null element
The implementation classes of set Include: Hashset,treeset,linkedhashset
1) HashSet
Implements the set interface, which does not guarantee the set iteration order, especially it does not guarantee that the order is immutable, this class allows null elements to be used
Of The bottom layer is implemented using HASHMAP.
Below we mainly explain cannot contain the duplicate Element.
Such as:
Define the person Class:
Java code
public class Person {
Public String name;
public int age;
Public person (String Name,int Age) {
this.name=name;
this.age=age;
}
@Override
Public String toString () {
return "person [name=" + name + ", age=" + age + "]";
}
}
Set Add person Element
Java code
public static void main (string[] Args) {
Set<person>sets=new hashset<person> ();
Person Ok=new person ("xiaoming", 18);
Person Ok1=new person ("little red", 16);
Person Ok2=new person ("small white", 15);
Sets.add (ok);
Sets.add (ok1);
Sets.add (ok2);
Add not to go in
Sets.add (ok1);
can add in
Sets.add (new person ("xiaoming", 18));//same as OK data
System.out.println ("size:" +sets.size ());
}
The Ok1 of the same object can only be added once, but the same data as OK is added multiple times. Set uses E1.equals (e2) to convict
It's Broken.
In the Java collection, determine whether two objects are the same object:
The first is to determine whether the hashcode values of two objects are equal, and if they are not equal, the two objects are considered unequal; if equal, the condition 2
2. Determine whether the equals operation of two objects is equal, and equality considers two objects Equal.
So we need to rewrite the hashcode and equals method of person
Add a method to the person class:
Java code
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + age;
result = Prime * result + ((name = = null)? 0:name.hashcode ());
Return result;
}
@Override
public boolean equals (Object Obj) {
if (this = = Obj)
Return true;
if (obj = = Null)
Return false;
If (getclass ()! = Obj.getclass ())
Return false;
person other = (person) obj;
If (age! = Other.age)
Return false;
if (name = = Null) {
If (other.name! = Null)
Return false;
} else if (!name.equals (other.name))
Return false;
Return true;
}
The same object that follows the OK data cannot be added to the collection at this Time.
2) TreeSet
TreeSet elements are naturally ordered, and the underlying is implemented using treemap, customizing the comparable interface to be displayed
Defining the Person class
Java code
public class person implements comparable<person>{
Public String name;
public int age;
Public person (String Name,int Age)
{
this.name=name;
this.age=age;
}
public int CompareTo (person O) {
If (o==null)
throw new NullPointerException ();
If (this.age>o.age)
Return 1;
If (this.age<o.age)
return-1;
Return 0;
}
@Override
Public String toString () {
return "person [name=" + name + ", age=" + age + "]";
}
}
TreeSet using:
Java code
public static void main (string[] Args) {
Set<person>sets=new treeset<person> ();
Person Ok=new person ("xiaoming", 18);
Person Ok1=new person ("little red", 16);
Person Ok2=new person ("small white", 15);
Sets.add (ok);
Sets.add (ok1);
Sets.add (ok2);
Add not to go in
Sets.add (ok1);
Output
Iterator<person> Iterator=sets.iterator ();
While (iterator.hasnext ())
{
System.out.println (iterator.next ());
}
}
Operation Result:
650) this.width=650; "src=" http://img.mp.itc.cn/upload/20160804/438b654d7fa84884b51a825ba16fa584.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>
Technology Sharing: Edith Academy
This article from the "11247808" blog, reproduced please contact the author!
Collection interface of Java collection