Collection operation classes in Java (not yet continued)

Source: Internet
Author: User

Disclaimer:

The superficial understanding of interns, such as the discovery of errors, but also hope that Daniel more guidance

nonsense

In fact, I write the background operation of Java, I will encounter a statement every time:list<xxxxx> List = new arraylist<xxxxx> ();

But I just know that the list class is a variable-length class of object instances to store, and I even think the list object can understand the array, but it's a lot different from what we normally understand in Java, for example, his length can grow automatically as needed, for example, Instantiating a list class is not the same as when we declare an array!

Today's internship life is a bit boring, I opened the eclipse, look at colleagues write code, inadvertently saw this sentence, so decided to learn this kind of operation--java in the Container Class (collection Class)

Body

Steal a picture first, I won't tell you that I stole it in the Baidu Encyclopedia.


How, as we can see, there are two main classes of collection operations in Java: Collection collection and map mapping

Collection

Collection is a top-level interface, which is an abstraction of the Java collection, based on which two sub-interfaces are derived, namely: List and set

List
list interface, the characteristics of the ordered, stored objects can be null, and allow repetition, which is very different from the set, set is not allowed to repeat, the list is similar to the array, the access to the list object can be accessed by a similar array subscript, However, it is accessed through the Get (int index) method, and its actual length is similar to that of an array, which can return its actual length through size (), in addition to the iterator () method with collection interface prerequisites. The list also provides a listiterator () method that returns a Listiterator interface, which, compared to the standard iterator interface, listiterator a number of add () methods, allowing adding, deleting, setting elements, can also traverse forward or backward. List Common methods: add(E e) : 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
addAll(Collection<? extendsE> c): Adds all the elements in the specified collection to the end of this list, in order to specify the collection iterator to return Back to the order of these elements
addAll(int index,Collection<? extends E> c) : inserts all elements from the specified collection into the specified position in the list list queries are more efficient than arrays, but insertions and deletions are less efficient because insertions and deletions cause changes in the location of other elements clear(): Remove all elements from the list
contains(Object o) : Returns true if the list contains the specified element
containsAll(Collection<?> c) : Returns true if the list contains all the elements of the specified collection
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.
iterator() : Returns an iterator that iterates over the elements of a list in the appropriate order
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.
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
remove(int index) : Removes an element from the specified position in the list
remove(Object o) : Removes the first occurrence of the specified element from this list
removeAll(Collection<?> c) : Removes all of its elements contained in the specified collection from the list
retainAll(Collection<?> c): Only the elements contained in the specified collection are preserved in the list
set(int index,E element) : Replaces the element at the specified position in the list with the specified element
size() : Returns the number of elements in the list
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

Detailed method information or refer to the API. The main implementations of the list interface are: ArrayList, LinkedList, vector, and Stack
ArrayList by name you can tell what this is, grey is often similar to an array

ArrayList implements a variable-size array. It allows all elements, including null. ArrayList is not synchronized, only vectors are synchronized (thread safe), each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements, and the default value seems to be 10. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method (set length minimum) before inserting to increase the capacity of the ArrayList to improve insertion efficiency. like LinkedList, ArrayList is also unsynchronized (unsynchronized)

Case:
Package Test;import Java.util.arraylist;import Java.util.iterator;import java.util.list;public class Test {public static int FLG = 0;public int self = 0;public Test () {++flg;self = FLG; System.out.println ("already initialized:" + FLG + "instances");} public static void Main (string[] args) {//TODO auto-generated method stub/* * Join paradigm Test means that the test type is stored here, and you can access the object's methods after you have obtained a specific value  and attributes such as the following: List.get (i). FLG * If you do not add a normal form, you cannot access the Self property or another method */list<test> list = new arraylist<test> (); for (int i = 0; I < 5; i++) {List.add (New Test ()); System.out.println (List.get (i). self);} List.add (New Test ()); System.out.println (List.get (List.size ()-1). self); List.addall (list); System.out.println (List.size ()); List.add (0, NULL); System.out.println (list.get (0));//Use iterators to traverse iterator<test> myiterator = List.listiterator (); Test Test;while (Myiterator.hasnext ()) {test = Myiterator.next (); if (test! = null) System.out.println (test.self); ELSESYSTEM.OUT.PRINTLN (test);}}}


LinkedList

The LinkedList implements a list interface that allows null elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the LinkedList. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).

Note LinkedList does not have a synchronization method. If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:

List List = Collections.synchronizedlist (new LinkedList (...));

Case: To be supplemented

Vector vectors are similar to ArrayList, but do synchronize, that is, he is thread-safe, in other words, if the vector creates an iterator, and the other thread changes the state of the vector when the iterator is in use, an exception is generated. Case: To be supplemented
Stack stack this thing, in the data structure learned, before very familiar, but now I am unfamiliar, probably know this has a feature: LIFO LIFO, in Java stack is based on the vector, in addition to the basic Method push () and Pop () method, The Java stack also provides a peek () method to get the value of the top element of the stack, the empty method to determine whether it is null, the search method to find the position of an element in the stack, etc. case: to be supplemented
Set

Set is a non-repeatable collection interface, set retrieval efficiency is relatively low, compared with the list, insert and delete more efficient, will not cause the change of other elements position, set is unordered, set and list the biggest difference is that set does not allow repetition

We have to remember that this guy can't repeat .

The reason that set does not allow duplication is that he is based on a map, and the key in the map is not allowed to be duplicated, only the key in the map is used in the set


Set common methods, please refer to the Java language API for more information

add(E e): Add this element if the specified element is not already present in the set

addAll(Collection<? extendsE> c): If all elements in collection are not specified in the set, add them to this set

clear(): Removes all elements from this set

contains(Object o): Returns true if the set contains the specified element

containsAll(Collection<?> c): Returns true if this set contains all the elements of the specified collection

equals(Object o): Compares the equality of the specified object to this set

isEmpty(): Returns true if the set does not contain an element

iterator(): Returns an iterator that iterates over the elements in this set

remove(Object o): If the specified element exists in the set, it is removed

removeAll(Collection<?> c): Removes the elements in the set that are contained in the specified collection

retainAll(Collection<?> c): Preserves only those elements in the set that are contained in the specified collection

size(): Returns the number of elements in a set

toArray(): Returns an array containing all the elements in the set

Compared to the egg, the set has no get method to get the element object, only through the iterator to access the data

Our common set implementation classes are: TreeSet, HashSet, and Linkedhashset

TreeSet TreeSet Incredibly orderly, the default is the natural sequence, the sort can be used by the use of, or you can use the custom, when creating TreeSet, pass a custom collation object, and since it is orderly, there is based on TREEMAP implementation, Then the element object in the TreeSet must implement the comparable interface, but if it is a string object, hehe, no need, the string itself has implemented the comparable interface. case: to be supplemented HashSet

As with TreeSet, data can only be accessed through iterators, HashSet is based on HASHMAP implementations, but is unordered, common methods are similar to set interfaces, remember that access must pass through iterators, the order of iterations is not guaranteed to be the same as the order of insertion, This is exactly the opposite of Linkedhashset.

Case: To be supplemented

Linkedhashset

Linkedhashset is a list structure that is consistent with the above, traversed by iterators, but with the same order of traversal and insertion.

Case: To be supplemented

Map

August 26, 2014 23:41:50

Tomorrow in the query map of the Information Bar, wrote this, found that there are many things to continue to learn, such as iterators, such as generics, Oh, little rookie, not fly, I can only slowly climb



Collection operation classes in Java (not yet continued)

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.