Collection interface of Java collection

Source: Internet
Author: User

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

    1. public static void Main (string[] args) {
    2. String Okstring=null;
    3. List<string>lists=new arraylist<string> ();
    4. Lists.add ("AA");
    5. Lists.add ("AA");
    6. Lists.add (1, "BB");//Specify position insertion
    7. Lists.add ("CC");
    8. Lists.add (okstring);
    9. Check if ArrayList is empty
    10. System.out.println (Lists.isempty ());
    11. Finds the first occurrence of a specified element
    12. System.out.println (Lists.indexof ("AA"));
    13. See if this element is included
    14. System.out.println (Lists.contains ("CC"));
    15. The length of the output list
    16. System.out.println (Lists.size ());
    17. Output
    18. for (int i=0;i<lists.size (); i++)
    19. System.out.println (Lists.get (i));
    20. Clear List
    21. Lists.clear ();
    22. }

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

    1. public static void Main (string[] args) {
    2. String Okstring=null;
    3. List<string>vectors=new vector<string> ();
    4. Vectors.add ("AA");
    5. Vectors.add ("AA");
    6. Vectors.add (1, "BB");//Specify position insertion
    7. Vectors.add ("CC");
    8. Vectors.add (okstring);
    9. Check if ArrayList is empty
    10. System.out.println (Vectors.isempty ());
    11. Finds the first occurrence of a specified element
    12. System.out.println (Vectors.indexof ("AA"));
    13. See if this element is included
    14. System.out.println (Vectors.contains ("CC"));
    15. The length of the output list
    16. System.out.println (Vectors.size ());
    17. Delete element;
    18. Vectors.remove (0);
    19. Output
    20. for (int i=0;i<vectors.size (); i++)
    21. System.out.println (Vectors.get (i));
    22. Clear List
    23. Vectors.clear ();
    24. }

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

    1. public static void Main (string[] args) {
    2. String Okstring=null;
    3. List<string>link=new linkedlist<string> ();
    4. Link.add ("AA");
    5. Link.add ("AA");
    6. Link.add (1, "BB");//Specify position insertion
    7. Link.add ("CC");
    8. Link.add (okstring);
    9. Check if ArrayList is empty
    10. System.out.println (Link.isempty ());
    11. Finds the first occurrence of a specified element
    12. System.out.println (Link.indexof ("AA"));
    13. See if this element is included
    14. System.out.println (Link.contains ("CC"));
    15. The length of the output list
    16. System.out.println (Link.size ());
    17. Delete element;
    18. Link.remove (0);
    19. Output
    20. for (int i=0;i<link.size (); i++)
    21. System.out.println (Link.get (i));
    22. Clear List
    23. Link.clear ();
    24. }

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

    1. public class Person {
    2. public String name;
    3. public int age;
    4. Public person (String Name,int age) {
    5. This.name=name;
    6. This.age=age;
    7. }
    8. @Override
    9. Public String toString () {
    10. return "person [name=" + name + ", age=" + Age + "]";
    11. }
    12. }

Set Add person Element

Java code

    1. public static void Main (string[] args) {
    2. Set<person>sets=new hashset<person> ();
    3. Person Ok=new person ("xiaoming", 18);
    4. Person Ok1=new person ("Little Red", 16);
    5. Person Ok2=new person ("small white", 15);
    6. Sets.add (OK);
    7. Sets.add (OK1);
    8. Sets.add (OK2);
    9. Add not to go in
    10. Sets.add (OK1);
    11. can add in
    12. Sets.add (New person ("xiaoming", 18));//Same as OK data
    13. SYSTEM.OUT.PRINTLN ("Size:" +sets.size ());
    14. }

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

    1. @Override
    2. public int hashcode () {
    3. final int prime = 31;
    4. int result = 1;
    5. result = Prime * result + age;
    6. result = Prime * result + ((name = = null)? 0:name.hashcode ());
    7. return result;
    8. }
    9. @Override
    10. public boolean equals (Object obj) {
    11. if (this = = obj)
    12. return true;
    13. if (obj = = null)
    14. return false;
    15. if (GetClass ()! = Obj.getclass ())
    16. return false;
    17. person other = (person) obj;
    18. if (age! = other.age)
    19. return false;
    20. if (name = = null) {
    21. if (other.name! = null)
    22. return false;
    23. } else if (!name.equals (other.name))
    24. return false;
    25. return true;
    26. }

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

    1. public class person implements comparable<person>{
    2. public String name;
    3. public int age;
    4. Public person (String Name,int age)
    5. {
    6. This.name=name;
    7. This.age=age;
    8. }
    9. public int compareTo (person o) {
    10. if (o==null)
    11. throw new NullPointerException ();
    12. if (this.age>o.age)
    13. return 1;
    14. if (this.age<o.age)
    15. return-1;
    16. return 0;
    17. }
    18. @Override
    19. Public String toString () {
    20. return "person [name=" + name + ", age=" + Age + "]";
    21. }
    22. }

TreeSet using:

Java code

    1. public static void Main (string[] args) {
    2. Set<person>sets=new treeset<person> ();
    3. Person Ok=new person ("xiaoming", 18);
    4. Person Ok1=new person ("Little Red", 16);
    5. Person Ok2=new person ("small white", 15);
    6. Sets.add (OK);
    7. Sets.add (OK1);
    8. Sets.add (OK2);
    9. Add not to go in
    10. Sets.add (OK1);
    11. Output
    12. Iterator<person> Iterator=sets.iterator ();
    13. while (Iterator.hasnext ())
    14. {
    15. System.out.println (Iterator.next ());
    16. }
    17. }

Operation Result:

  

Technology sharing: www.kaige123.com

Collection interface of Java collection

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.