A brief summary of basic Java collections

Source: Internet
Author: User

Last night I saw the idea of programming 4 of the object, so learn to see some of the memory back aspects to summarize.

Java containers are divided into two main categories, one is the implementation of the interface Collection class, there is a kind of implementation of the Map interface.

The main inherited Collction interfaces are the List, theSet interface, and the Queue. Collection inherits the Iterator interface, allowing the implementation class to iterate through the foreach .

The main inheritance of the List interface is ArrayList and linkedlist.

ArrayList 's advantage is random access,LinkedList Advantage is better insert Delete, in addition LinkedList also implemented the Queue interface, which allows LinkedList can be used as a data structure for a queue

List Method (parameter not written):

1, contains () to confirm whether there is an object in the list (and the Containsall () method)

2. Add () adds an object (and the AddAll () method)

3. Get () Gets an object based on the incoming location

4. Remove () removes an object from the list (as well as the RemoveAll () method)

5, IndexOf () returns the position of an object in the list

6. Sublist () intercepts a portion of the list collection and returns

7. Sort () sorts the list collection

8, Retainall () computes the intersection of the incoming collection collection, returns a Boolean, and the result of the intersection is saved in the list

9. Set () modifies an object at a location in the list collection

10. IsEmpty () Determine if the list collection is empty

11. Clear () clears the list collection

12, ToArray () generates an array of the specified type according to the parameters passed in, and the default is an array of type object.

13. Size () calculates the length of the list collection

Note: The actions that need to be compared above, for incoming objects, are the Equals method that uses the passed-in object parameter and the object in the collection to compare


Iterator iterates through a collection, only forwards (Hasnext (), Next ()), and Listiterator can have a bidirectional index, but listiterator can only be used for list

LinkedList implements the interface of list and implements the queue interface

1, GetFirst () element () is the first element in the return list, and if it is empty, throws an exception nosuchelementexception

2, Peek () returns the first element in the list, or null if it is empty

3, Removefirst () delete the first element in the list, if it is empty, throw an exception nosuchelementexception (the Remove () method of the null parameter is the same)

4, poll () delete the first element in the list, or null if it is empty

5, add () AddFirst () addlast () adds elements to the list header (tail)

6, Offer () Offerfirst () offerlast () Add elements to the list header (tail)

The difference between offer and add is the operation of a capacity-constrained queue, which returns a false after failure, which throws an exception

In addition to the queue, LinkedList can also be used as a stack stack (the stack here refers to a data structure, not a stack class in Java, LinkedList and Stack have no inheritance)

7. Push () Add an object to the top

8. Pop () removes a top object and returns the object

        Unlike the List collection, The set collection differs from the element that holds the duplicate, and the set interface is the same as the Collection interface field method, and the set The collection is implemented primarily with HashSet , TreeSet , linkedhashset . HashSet uses hashes, TreeSet uses the data structure of a red-black tree, and Linkedhashset is also hashed, It also maintains the position order with the linked list. So the difference between these three structures is that HashSet has the fastest query speed, sometimes orderly, but not guaranteed to be orderly. TreeSet is always in the sorted state. Linkedhashset preserves the insertion order of elements and also provides fast access.

        Collection The collection series also has a queue , and queue , in addition to the LinkedList implementation, has a The implementation of the Priorityqueue . The priorityqueue queue can eject elements based on a rule that pops the queue precedence. This rule needs to be defined by itself, such as the student class in the Priorityqueue collection, where the student class is defined to implement a compareble interface. In the CompareTo method of an int type return value object parameter, customize the rule to compare the passed-in object parameter (transformed first to student) and a field of the current student class. Returns a 1 description that pops up after the current student and returns 1 first (with a 0 experience)

Map Interface Implementation and set similar, three implementation classes HashMap,TreeMap,linkedhashmap, functional features and set similar. The map and Collection are parallel and do not inherit the Iterator interface, but map can convert the keys to set via the keyset () method traversal, or a Set object traversal through EntrySet () into entry

A brief summary of basic Java collections

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.