The Java collection is divided into three interfaces, namely Collection,map,iterator, the collection interface and class in the Java.util package, this time mainly introduces the collection interface of one of the three interfaces.
Some collection allow repeating elements, while others do not, some collection are ordered, others are unordered. 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 that provides precise control over the insertion position of each element of the user queue table of the interface, and can access elements based on an integer index of the element and search for elements in the list.
The list interface implementation class mainly includes: Arraylist,vector,linkedlist
1) ArrayList
1. The default ArrayList size is 10
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 extension length is about half the length of the original array, the expansion requires a new array, and then the original data copied over.
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 can result in the massive shift of internal data, which will affect the performance. If we already know the number of elements required, we can initialize the capacity of the specified ArrayList, which can effectively avoid multiple expansion of the array, thereby improving efficiency, but also can not initialize too large, wasting 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 expanded or shrunk as needed.
1.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)
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;
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 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 over ArrayList, whereas 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 because it does not guarantee that the order is immutable, and this class allows the use of NULL elements. 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 determine.
In the Java collection, determine whether two objects are the same object:
1. Determine whether the hashcode values of the 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:
Technology sharing: www.kaige123.com
Collection interface of Java collection