Collection overall framework of Java Collection Summary

Source: Internet
Author: User
Tags int size

The previous time has been busy a IoT project, so Java learning has been shelved, starting today to continue to learn! I hope you have more valuable advice!

The Java.util package contains some of the most exciting enhancements that have been added to Java 2: the class set. A class set (collection) is a set of objects. The increase in the set of classes has resulted in a fundamental change in the structure and architecture of many JAVA.UTIL members. It also expands the scope of tasks that packages can be applied to. The Java class set (Collection) framework enables you to standardize the way your program handles object groups. Before Java 2 appeared, Java provided some specialized classes such as Dictionary,vector,stack and properties to store and manipulate object groups. Although these classes are very useful, they lack a centralized, unified theme. Thus, for example, the use of vectors is different from the method used for properties. Previous specialized methods have not been designed to be easy to expand and adapt to the new environment. And the class set solves these (and some other) problems
Advantages of the Java class set framework:
1) This framework is high-performance. The implementation of basic class sets (dynamic arrays, linked tables, trees, and hash lists) is efficient. In general, it is seldom necessary to manually write code (if any) on these "Data engines".
2) The framework allows different types of class sets to work in the same way and in a highly interoperable manner.
3) class sets are easy to extend and/or modify. To achieve this goal, the class set framework is designed to contain a standard set of interfaces. For these interfaces, there are several standard implementation tools (such as Linkedlist,hashset and TreeSet) that are typically used. You can also implement your own set of classes if you wish. For the sake of convenience, create an implementation tool for a variety of special purposes. Some tools make it easier to implement your own set of classes.
4) added a mechanism to allow a standard array to be fused into a class set framework.

The overall framework of the class set is as follows:

The collection interface is the basis for constructing a class set framework. It declares the core method that all class sets will have. Because all class sets are implemented collection, it is necessary to be familiar with its methods for a clear understanding of the framework.

Boolean Add (E): Adds an element to the collection if the type collection allows duplicate elements (such as: ArrayList, LinkedList, and so on) or returns True if no duplicate elements are allowed but the newly added element is not in the collection. If the type collection does not allow duplicate elements and the newly added element is already in the collection, False is returned.

Boolean AddAll (collection<? extendse> c): Adds all the elements in a class set C to another set of classes (this class set).

void Clear (): Clears all the elements in this class set and will be empty after the method is called.

Boolean contains (Object O): Returns True if this class set contains element E, and satisfies (o==null? E==null:o.equals (e) ) , otherwise false.

Boolean containsall (collection<?> C): Returns True if this class set contains all elements in the specified class set C, otherwise false.

Boolean equals (Object O): If the Equals method of this set of methods is our own rewrite, then the specific two class set how to calculate the equal (is the reference equality or the value is equal and the same order), we decide. If the Equals method of a class set is not overridden by us, then when two class set types are list (including ArrayList and LinkedList, elements can be duplicated), the number of elements in the two-class set must be equal, the element order is the same, and the elements of the same position return true for equality. otherwise false; When two class set types are set (including HashSet and treeset, elements cannot be duplicated), only the number of elements of the two-class set is equal, and any element in a class set can find the same element in another class set (not necessarily in the same order because set is unordered) , true is returned, otherwise false is returned.

int hashcode (): Returns the hash code value for this class set, which depends on the value and type of the elements in the class set, as discussed later in this discussion.

Boolean isEmpty (): Returns True if there are no elements in this class set, otherwise false.

Iterator<e> Iterator (): Returns the iteration function for all elements in this class set, and the order of the elements taken from the function depends on the type of the set, and if the list is sorted in the order of list, if set is unordered.

Booleanremove (Object o): If there are elements in this class that have values equal to the value of O, the element is deleted and true is returned, false if not. When a class set type is list, it is possible that more than one element is equal to the value of O, at which point only the first element with the value O is deleted and the other elements are positioned unchanged.

Boolean RemoveAll (collection<?>c): Removes all elements in this class set that are equal to the elements in the specified class sets C, no matter how many elements, as long as it appears in C, does not appear in D after the call. Returns true if the elements of this class set are changed (that is, if an element is deleted), or False if it is called.

Boolean Retainall (COLLECTION<?>C): Preserves all elements in this class that have occurred in a specified class set, that is, deleting all elements in this class collection that are not contained in the specified class sets C. Returns true if the elements of this class set are changed (that is, if an element is deleted), or False if it is called.

int size (): Returns the number of elements in the set, but the size cannot exceed 0x7fffffff and returns 0X7FFFFFFF if it is exceeded.

Object[] ToArray (): Returns an array containing all the elements of this class set, the array type: object[], because the underlying is passed through the iterator () method, so the order of the elements in the array also depends on the type of the set. If the list is placed in the order of the list in the array, if the set is unordered.

<t>t[] ToArray (t[] a): The type of the returned array is passed in the form of generics, and the other is similar to the Object[]toarray () method.

Directly inherit the collection interface with the list<e>, queue<e>, Beancontext and set<e> four interfaces, of which we use the most is list<e> and set<e > These two interfaces, the following post will be detailed analysis, the other two interfaces with relatively few, generally to achieve the functions of the two interfaces are the developers themselves re-implementation, where no detailed analysis.

Description: Part of the content from the network! The following will be commonly used interfaces and classes for detailed analysis, I hope that with the common progress of netizens, the wrong place to welcome you to correct!

The test code is as follows:

[Java]View PlainCopyprint?
    1. Public class Collectiontest
    2. {
    3. public static void Main (string[] args)
    4. {
    5. collection<string> C = new arraylist<string> ();
    6. Collection<string> d = new arraylist<string> ();
    7. C.add ("1");
    8. C.add ("2");
    9. C.add ("1");
    10. C.add ("3");
    11. C.add ("1");
    12. C.add ("3");
    13. C.add ("3");
    14. C.add ("2");
    15. C.add ("3");
    16. C.remove ("3");
    17. D.add ("2");
    18. D.add ("1");
    19. D.add ("4");
    20. System.out.println (C.equals (d));
    21. System.out.println (c);
    22. System.out.println (d);
    23. System.out.println (C.hashcode ());
    24. System.out.println (D.hashcode ());
    25. System.out.println (C.removeall (d));
    26. System.out.println (c);
    27. System.out.println ("----------------------");
    28. object[] o = C.toarray ();
    29. String s = null;
    30. For (int i = 0; i < o.length; i++)
    31. {
    32. s = (String) o[i];
    33. System.out.println (s);
    34. }
    35. }
    36. }

Collection overall framework of Java Collection Summary

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.