Java List Learning

Source: Internet
Author: User

To learn the List<e> interface, first, I know it also has a parent interface collection<e>. And Collection<e> has a super interface iterable<t>.

Let's start with the Super interface iterable<t>:

Implementing this interface allows the object to be the target of the "foreach" statement. public interface iterable<t>

Method has only one:

Iterator ()//Returns an iterator that iterates over a set of elements of type T.

Then we look at Collection<e>:

Public interface Collection<e> extends iterable<e>

Explain:

1. The root interface in theCollection hierarchy .

2. Collection represents a set of objects, which are also called Collection elements .

3, Collection some allow repetition, some do not allow, some orderly, some disorderly.

4, the general Operation Collection no direct implementation, all through the specific sub-interface (such as set,list) to operate it.

All APIs:

Add (E Element) Make sure that this collection contains the specified element.
AddAll (collection<? extends e> c) Adds all the elements in the specified collection to this collection.
Clear () Removes all elements from the collection.
Contains (Object o) Returns True if the collection contains the specified element.
Containsall (collection<?> c) Returns True if collection contains all the elements of the specified collection.
Equals (Object o) Compares whether the specified object is equal to this collection.
Hashcode () Returns the hash code value for this collection.
IsEmpty () Returns True if this collection contains no elements.
Remove (Object o) Removes a single instance of the specified element from this collection, if one exists.
RemoveAll (collection<?> c) Removes all elements in this collection that are also contained in the specified collection.
Retainall (collection<?> c) Only those elements in this collection that are also contained in the specified collection are preserved.
Size () Returns the number of elements in this collection.
ToArray () Returns an array that contains all the elements in this collection.
ToArray (t[] a) Returns an array containing all the elements in this collection; The run-time type of the returned array is the same as the run-time type of the specified array.
Iterator () Returns an iterator that iterates over the elements of this collection. (Inherited method)

then look at List<e>:

Public interface List<e> extends collection<e>

Explain:

1, list<e>: Ordered collection (also known as sequences).

2. The element can be accessed based on the integer index of the element.

3. Unlike set, a list usually allows repeating elements.

4. List<e> inherits from Collection<e> and Iterator<t> but in some ways (Iterator,add,remove,equals, Hashcode) on the basis of the original method to add some other conventions.

5. list<e> extends some parent interfaces without methods.

All APIs:

Inherit all methods of collection Change the collection to a list, with the general meaning and usage unchanged. The change method is rewritten below.
Add (E Element) Adds the specified element to the tail of the list.
Add (int index,e Element) Inserts the specified element at the specified position in the list. Moves the element that is currently in that position (if any) and all subsequent elements to the right (plus 1 in its index).
AddAll (int index,collection<? extends e> c) Inserts all the elements in the specified collection into the specified position in the list.
Get (int index) Returns the element at the specified position in the list.
IndexOf (Object o) Returns the index of the specified element that first appears in this list, or 1 if the list does not contain the element.
LastIndexOf (Object o) Returns the index of the last occurrence of the specified element in this list, or 1 if the list does not contain this element.
Remove (int index) Removes the element from the specified position in the list.
Remove (Object o) Removes the first occurrence of the specified element (if one exists) from this list.
Set (int index, E Element) Replaces the element at the specified position in the list with the specified element.
sublist (int fromIndex, int toindex) Returns a partial view between the specified FromIndex(inclusive) and Toindex(not included) in the list.
ToArray () Returns an array that contains all the elements in the list in the appropriate order, from the first element to the last element.
ToArray (t[] a) With collection, and arranged in the proper order.
Iterator () Returns an iterator that iterates over the elements of a list in the appropriate order.
Listiterator () Returns the list iterator for this list element, in the appropriate order.
Listiterator (int index) Returns the list iterator (in the appropriate order) of the elements in the list, starting at the specified position in the list.



A single operation on the list:

 1 map<string, object> map1 = new hashmap<> (); 2 map1.put ("name", "Xiaoming"); 3 Map1.put ("Sex", "0"); 4 Map1.put ("Phone", "13566668888"); 5 map1.put ("email", "[email protected]"); 6 Map1.put ("Age", "18"); 7//List Declaration 8 List<string> list1 = new arraylist<> (); 9 list<object> list2 = new arraylist<> (); list<object> list3 = new Arraylist<> () ; one//Add Operation List1.add ("name"), List1.add ("email"), List1.add ("sex"); 1.add ("Age"); List1.add ("name"); List1: ["name", "email", "sex", "Age", "name"];17 List2.add (MAP1); List2: [{phone=13566668888, name=xiaoming,[email protected], age=18}]18//AddAll operation List3.addall (List1); LIST3: ["name", "email", "sex", "Age", "name"];20 list1.add (0, "phone"); List1: ["Phone", "name", "email", "sex", "Age", "name"];21 List2.addaLL (0, List1);         LIST2: [Phone, name, email, sex, age, name, {phone=13566668888, name=xiaoming, [email protected], age=18}]22             Get operation, equals operation, List1.get (1). Equals (List3.get (0))) {//NAME==NAME24//Set operation, indexof operation 25 List3.set (List1.indexof ("Phone"), "phone");  List1.indexof ("Phone") =0;LIST3: [Phone, email, sex, age, name]26//Remove Operation List2.remove (1);             LIST2: [Phone, email, sex, age, name, {phone=13566668888, name=xiaoming, [email protected], age=18}]28 List3.remove ("Sex"); LIST3: [Phone, email, age, name]29}30//IsEmpty operation (!list2.isempty ()) {//List2.isempty ( ) =FALSE32//RemoveAll operation List2.removeall (LIST2); List2: []34}35//contains operation (!list2.contains ("name")) {//List2.contains ("name") =false3 7//size operation list3size int = list3.size (); list3size=439//LAstindexof operation: + int list1lastindexofname = List1.lastindexof ("name"); list1lastindexofname=541//sublist Operation List2.addall (List1.sublist (List3size, List1lastindexofn AME)); List2: [age]43}44//Containsall operation (List1.containsall (LIST3)) {//List1.containsall (list 3) =true46//ToArray () map1.put ("Arr", List1.toarray ()); List1.toarray (): [Phone, name, email, sex, age, name]48//ToArray (t[] a)//ToArray (New Obje Ct[0]) is functionally the same as toarray (); List1.toarray (New object[0]): [Phone, name, email, sex, age, name]50 map1.put ("arr2", List1.toarray (new Object [0])); Do not understand this How to use, want to understand your own research ~ will and then come back to teach me Wuyi//Retainall operation List1.retainall (LIST3); List1: [Phone, name, email, age, name]53//Clear Operation List3.clear (); List1: []55//Hashcode Operation Map1.put ("Hashcode", List1.hashcode ()); List1.hashcode (): -91277858257} 

As for iterator (), I will not, interested students themselves study, seminar and then come back to teach me ~

Java List Learning

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.