The collection framework in Java

Source: Internet
Author: User
Tags iterable

public interface iterable<t>
public interface iterator<e>
The application of a typical iterator model.
Note that the iterator and enumerations mentioned in the note are a different point of the method name, and it is important to name it.

public interface collection<e>
Extends iterable<e>

More interesting.
Thread policy is determined by the implementation class.
Note that contains does not necessarily use equals, but rather gives the implementation class the freedom.
Many optional operations.
If you need to be particularly careful with inheriting the Equals method, the default convention is that list and set are never equal.

Java code:

  1. Query Operations
  2. int size ();
  3. Boolean isEmpty ();
  4. Boolean contains (Object O);
  5. Iterator<e> Iterator ();
  6. Object[] ToArray ();
  7. <T> t[] ToArray (t[] a);
  8. Modification Operations
  9. Boolean Add (e e);
  10. Boolean remove (Object O);
  11. Bulk Operations
  12. Boolean containsall (collection<?> c);
  13. Boolean AddAll (collection<?  extends e> c);
  14. Boolean RemoveAll (collection<?> c);
  15. Boolean retainall (collection<?> c);
  16. void Clear ();
  17. Comparison and hashing
  18. Boolean equals (Object O);
  19. int hashcode ();

2 Set  

  1. Public Boolean RemoveAll (collection<?> c) {
  2. Boolean modified = false;
  3. if (size () > C.size ()) {
  4. For (iterator<?> i = C.iterator (); I.hasnext ();)
  5. Modified |= Remove (I.next ());
  6. } Else {
  7. For (iterator<?> i = Iterator (); I.hasnext ();) {  
  8. if (C.contains (I.next ())) {
  9. I.remove ();
  10. modified = true;
  11. }
  12. }
  13. }
  14. return modified;
  15. }

3 List

  1. Positional Access Operations
  2. E get (int index);
  3. E set (int index, e element);
  4. void Add (int index, E element);
  5. E Remove (int index);
  6. Search Operations
  7. int indexOf (Object o);
  8. int lastIndexOf (Object o);
  9. List iterators
  10. Listiterator<e> Listiterator ();
  11. Listiterator<e> listiterator (int index);
  12. View
  13. list<e> sublist (int fromIndex, int toindex);

4 Map

  1. Public V Get (Object key) {
  2. iterator<entry<k,v>> i = EntrySet (). Iterator ();
  3. if (key==null) {
  4. While (I.hasnext ()) {
  5. Entry<k,v> e = I.next ();
  6. if (e.getkey () = =null)
  7. return E.getvalue ();
  8. }
  9. } Else {
  10. While (I.hasnext ()) {
  11. Entry<k,v> e = I.next ();
  12. if (Key.equals (E.getkey ()))
  13. return E.getvalue ();
  14. }
  15. }
  16. return null;
  17. }

Java, a generic term for the Java programming language and Java platform introduced by Sun Microsystems Company in May 1995 . Java-enabled HotJava browser (Java applet support) shows the charm of Java: Cross-platform, dynamic web, Internet computing. Since then, Java has been widely accepted and has driven the rapid development of the web, and common browsers now support Java applets. A collection framework is a uniform standard architecture that is defined for representing and manipulating collections . Any set frame contains three chunks of content: external interfaces, interface implementations, and algorithms for set operations.

A collection framework is a uniform standard architecture that is defined for representing and manipulating collections. Any set frame contains three chunks of content: external interfaces, interface implementations, and algorithms for set operations.

Java Collection Framework :

1. What is a framework: a collection of class libraries

2. Collection framework: A unified architecture for representing and manipulating the interfaces and classes that implement a collection

3. Collection: Container for storing data

A collection framework consists of two parts: a part is an interface, and a part is a class

4. Why interfaces occur: Because many of the class functions in the collection framework are similar, "use interfaces to standardize classes"

The list interface implements a number of classes:

Abstractlist, Abstractsequentiallist, ArrayList, AttributeList, Copyonwritearraylist, LinkedList, RoleList, Roleunresolvedlist, Stack, Vector in general, the main use is ArrayList, and LinkedList, the other class is not to say useless

ArrayList

ArrayList allows all elements to include NULL. ArrayList not synchronized

Understanding One: ArrayList uses a built-in array to store elements, the starting capacity of this array is 10. When the array needs to grow, the new capacity is obtained as follows: the new capacity = (old capacity *)/2+1, which means that each capacity will probably increase by 50%. This means that if you have a ArrayList object with a large number of elements, then eventually there will be a lot of wasted space, and this waste is caused by the way ArrayList works itself. If there is not enough space to hold the new element, the array will have to be reassigned so that new elements can be added.

Redistribution of the array will result in a dramatic decrease in performance. If we know how many elements a ArrayList will have, we can construct a method to specify the capacity. We can also remove wasted space after the ArrayList is allocated through the TrimToSize method.

Understanding two: ArrayList is implemented with an array, it is not a real linked list, at the time of initialization it sets an initial capacity of the array, when the space is not enough, it will rebuild a larger size of the array, and then copy the previous elements in

Whether it's one or two, no matter how he stores the elements. The only thing you can confirm is that he uses built-in arrays

LinkedList

The list interface is implemented as a link listing. Implements all optional list operations, and allows all elements (including null). In addition to implementing the List interface,

The LinkedList class also provides a uniform naming method for the get, remove, and insert elements at the beginning and end of the list. These operations allow the link list to be used as a stack, queue, or double-ended queue (deque). This class implements the queue interface to provide FIFO queue operations for Add, poll, and so on. Other stacks and double-ended queue operations make it easy to re-cast according to the standard list operation. Although they may run slightly faster than the equivalent list operation, they are included here primarily for ease of consideration.

The collection framework in Java

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.