Java-Collection, List simple use and method/(Set use-in ),

Source: Internet
Author: User

Java-Collection, List simple use and method/(Set use-in ),

1.1 The reference (address) of the element when the set only stores the element of the reference type)
1.2 new cyclic traversal set
Collection c = new ArrayList ();
C. add ("one ");
C. add ("two ");
C. add ("three ");
C. add ("four ");
/*
* The New loop is not a new syntax, And the jvm does not recognize the new loop.
* The New loop is recognized by the compiler. When the compiler finds that the new loop traversal set is used, the code is changed to the iterator traversal, therefore, you cannot add or delete elements in the process of traversing a set through a new loop.
*
*/
For (Object object: c ){
String str = (String) object;
System. out. println (str );
}
1.3 Set Operations
Boolean addAll (collection c) saves the elements in the given set to the current set. If the element of the current set changes, true is returned.
Boolean containsAll (Collection c) determines whether the current set contains all elements in the given set
Boolean removeAll (Collection c) deletes the same elements in the current Collection as the given one.
1.4 traverse set -- set provides a uniform way to traverse elements: iterator Mode
The Set provides a method for retrieving elements of the current set:
Java. util. Iterator
The interface of the Iteartor iterator () iterator specifies the method for traversing the set. The following mode is used: Question, fetch, and delete steps. Deleting an element is not a required operation. Different set implementation classes provide an iterator implementation class that can traverse itself. We don't need to remember their names as their Iterator looks at them.
Boolean hasNext () determines whether the set has any elements that can be traversed.
E next () retrieves the next element in the set that can be traversed.
When you use the iterator to traverse the set elements, you cannot add or delete elements through the set method. Otherwise, an exception occurs when the iterator traverses the set, however, the iterator remove can delete the elements retrieved using the next method.
1.5 New loop-the new loop, also known as the enhanced for loop and for each, is used to traverse the set or array-the new loop is a new feature launched after JDK1.5.
Use the new loop to traverse the set: the new loop is not a new syntax, And the JVM does not recognize the new loop. The new loop is recognized by the compiler. When the compiler finds that the new loop traversal set is used, the code is changed to the iterator traversal. Therefore, you cannot add or delete elements in the process of traversing a set through a new loop. If an element is deleted in the new cycle, an exception is thrown.
1.6 generic -- a feature introduced after 1.5. Generic is a feature that the compiler recognizes that the actual type of generic is an Object. When used, the compiler checks or automatically shapes it. When a value is assigned to a generic variable, the compiler checks whether the generic value meets the type requirements to obtain the code for automatic styling. If the generic type is not specified, the object is considered by default. Generic is used to specify the element type in the set.
1.7List _ get_set -- java. util. List
List is the sub-type interface of Collection. It is a replicaset and ordered. It provides a set of methods for operating elements according to subscript.
Common Implementation classes:
Java. util. ArrayList: array implementation, high query efficiency
Java. util. shortlist: linked list implementation. It is highly efficient to add and delete elements, especially add and delete elements at the beginning and end.
E get (int index) returns the element corresponding to the specified subscript
E set (int index, E e) replaces the elements at the specified position in the set with the given elements and returns the replaced elements.
1.8List _ add_remove: List provides a pair of overloaded add. The remove method can also return a deleted element through the subscript operation element deletion method.
1.9 obtain the List subset: List <E> subList (int startindex, int endindex) intercepts the elements at the specified position in the collection and returns the truncated elements; the subset obtained by the operation is equal to the original set of the operation.
1.10 convert a set to an array --- the Collection provides the method to convert the current set to an array
E [] array = c. toArray (new E [c. size ()]); converts a set to an array, transmits a generic array, and sets the length. If the length is not enough, an array exactly equal to the element is returned, if the parameter length is greater than the original array, the input length array is returned. The position of no element is null.
1.11 convert an array to a set --- List <String> list = Arrays. asList (array); converts an array to a set. The returned set is equal to the original array and cannot add or delete elements, if you want to add an element, you need to create an array to pass in and operate the returned array value.
List <String> list1 = new ArrayList <String> (list); this method can be used to add elements of the original array while creating a new array.

 

I am a beginner. If any updates are poor, I would like to thank you!

More highlights will be updated later!

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.