Java Collection Class collection, List

Source: Internet
Author: User
Tags iterable

Collection interface:

1. The collection can be understood as a dynamic array of objects, but the contents of the objects in the collection can be arbitrarily expanded.

This means that the operation in the collection is more convenient, easy to add or delete

2. Features of the collection:

High performance

Easy to expand and modify

This is also the main reason why you use collections instead of arrays

Common subclasses of 3.Collection

List

Set

Queue

These sub-categories are also more commonly used


Interface collection<e>

All Super Interfaces:

Iterable<e>

All known interfaces:

Beancontextbeancontextservices, Blockingdeque<e>, Blockingqueue<e>, Deque<e>, List<E>, Navigableset<e>, Queue<e>, Set<e>, sortedset<e>

    • All known implementation classes:

    • Abstractcollection, Abstractlist, Abstractqueue, Abstractsequentiallist, Abstractset, ArrayBlockingQueue, ArrayDeque , ArrayList, AttributeList, Beancontextservicessupport, Beancontextsupport, Concurrentlinkedqueue, Concurrentskiplistset, Copyonwritearraylist, Copyonwritearrayset, Delayqueue, Enumset, HashSet, JobStateReasons, Linkedblockingdeque, Linkedblockingqueue, Linkedhashset, LinkedList, Priorityblockingqueue, PriorityQueue, RoleList, Roleunresolvedlist, Stack, Synchronousqueue, TreeSet, Vector

public interface Collection<e>extends iterable<e>

The root interface in the collection hierarchy. Collection represents a set of objects, also known as collection elements. Some collection allow duplicate elements, while others do not. Some of the collection are orderly, while others are unordered. The JDK does not provide any implementations of this interface: it provides more specific sub-interfaces (such as set and list) implementations. This interface is often used to pass collection and operate these collection where it is required to be the most universal.

Package (bag) or multi-collection (Multiset) (unordered collection that may contain duplicate elements) should implement this interface directly.

All common colelction implementation classes (typically indirectly implemented collection through one of its subinterfaces) should provide two "standard" construction methods: one is void (parameterless) constructed to create an empty collection The other is a construction method with the collection type single parameter, which is used to create a new collection with the same elements as parameters. In fact, the latter allows the user to copy any collection to produce an equivalent collection of the desired implementation type. Although this Convention cannot be enforced (because an interface cannot contain a constructor method), all common collecion implementations in the Java Platform Library follow it.

The "destructive" method contained in this interface refers to those methods that can modify the collection of its operation, and specifies that these methods throw unsupportedoperationexception if this collection does not support the operation. If so, these methods may, but not necessarily, throw unsupportedoperationexception when the call to the collection is invalid. For example, if the Collection to be added is empty and cannot be modified, it is possible, but not necessarily, to throw an exception when calling the AddAll (Collection) method on the Collection.

Some collection implementations have limitations on the elements they may contain. For example, some implementations prohibit null elements, while some implementations have restrictions on the types of elements. Attempting to add an unqualified element throws an untested exception, usually NullPointerException or classcastexception. Attempting to query for an unqualified element might throw an exception, or simply return false; Some implementations will exhibit the previous behavior, while some implementations behave the latter. It is more common to attempt to perform an action on an unqualified element and the completion of the operation does not result in the insertion of unqualified elements into the collection, an exception may be thrown, or the operation succeeds, depending on the implementation itself. Such exceptions are marked as "optional" in the specification of this interface.

Each collection determines its own synchronization policy. In the absence of a strong guarantee of implementation, the invocation of a collection method that is being changed by another process may have an indeterminate behavior, which includes a direct call, passing collection to a method that might execute the call, and checking the collection with an existing iterator.

Many of the methods in the collections Framework interface are defined according to the Equals method. For example, the canonical declaration of the CONTAINS (Object O) method: "When and only if this collection contains at least one satisfying (o==null? E==null:o.equals (e)), returns True when element E. "This specification should not be understood as it implies that calling the Collection.contains method with a non-null parameter o causes the O.equals (e) method to be called for any e element. You can arbitrarily perform optimizations on a variety of implementations, as long as you avoid calling equals, for example, by first comparing the hash codes of two elements. (The Object.hashcode () specification guarantees that two objects with unequal hash codes will not be equal). It is more common that implementations of various collections Framework interfaces are free to take advantage of the specified behavior of the underlying object method, regardless of whether the implementation program considers it appropriate.

This interface is a member of the Java collections Framework.




List Interface: Collection Sub-interface

The 1.List interface can hold any data, and the contents of the list interface can be duplicated

2.List Interface Common Subclass:

ArrayList--jdk1.2 After the launch, asynchronous processing, high performance, non-thread-safe

Vector--jdk1.0 launched, using synchronous processing, low performance, belonging to thread safety

3. Common operation:

Determines whether the collection is empty: Boolean IsEmpty ()

Finds whether the specified object exists: int indexOf (Object o)

Package Com.jikexueyuan.list;import Java.util.arraylist;import Java.util.list;public class ListDemo01 {public static void Main (string[] args) {list<string> lists = Null;lists = new arraylist<string> (); Lists.add ("A"); Lists.add ("B"); Lists.add ("A"); for (int i=0; i<lists.size (); i++) {System.out.println (Lists.get (i));} Lists.remove (0); System.out.println ("after delete:"); for (int i=0; i<lists.size (); i++) {System.out.println (Lists.get (i));} System.out.println ("Whether the collection is empty:" +lists.isempty ()); System.out.println ("B exists:" +lists.indexof ("B"));//indexof (Object O) return type: Int returns the index of the specified element that first appears in this list, or if//This list does not contain the element. Then return-1}}

Package Com.jikexueyuan.list;import Java.util.list;import Java.util.vector;public class ListDemo02 {public static void Main (string[] args) {list<string> lists = Null;lists = new vector<string> (); Lists.add ("A"); Lists.add ("B"); for (int i = 0; i < lists.size (); i++) {System.out.println (Lists.get (i));}}}




Set interface:

Duplicate elements cannot be added to the 1.Set interface, but can be sorted.

Common subclasses of the 2.Set interface

Hash Storage: HashSet

Ordered storage: TreeSet

Package Com.jikexueyuan.list;import Java.util.hashset;import Java.util.set;import Java.util.treeset;public class SetDemo01 {public static void main (string[] args) {set<string> s = null;s = new hashset<string> (); S.add ("B"); s . Add ("A"); S.add ("C"); S.add ("D"); S.add ("E"); S.add ("F"); System.out.println (s);//s = new treeset<string> ();//s.add ("B");//s.add ("A");//s.add ("C");//s.add ("D");// S.add ("E");//s.add ("F");//system.out.println (s);}}



Iterator interface:

1. Standard operation of the set output:

Standard practice, using the iterator interface

2. Operating principle:

Iterator is a specialized iterative output interface, the iterative output is to determine the elements of each one, to determine whether there is content, if there is content to remove the content

3. Precautions:

The remove operation cannot be performed through the collection while iterating the output

Package Com.jikexueyuan.list;import Java.util.arraylist;import Java.util.iterator;import java.util.List;public Class IteratorDemo01 {public static void main (string[] args) {list<string> lists = new arraylist<string> (); Li Sts.add ("A"), Lists.add ("B"), Lists.add ("C"), Lists.add ("D"); Lists.add ("E"); Lists.add ("F");iterator<string> iter = Lists.iterator (), while (Iter.hasnext ()) {String str = Iter.next (), if ("A". Equals (str)) {iter.remove ();} Else{system.out.println (str);}}}




Map interface

1. Save form:

Key----->value Save (Key-value pair method to save, find value by key)

Example: Xiao Ming: 13433321122 (Find the phone number by name)

2. Common sub-categories:

HASHMAP: unordered storage, key not allowed to repeat

Hashtable: unordered storage, key not allowed to repeat

Key is unique when storing data on a map interface.


Java Collection Class collection, List

Related Article

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.