Java Collection Interface Examples of detailed _java

Source: Internet
Author: User
Tags data structures

1. Collection is a top-level interface to the collection class, and its direct inheritance interface has a list and set.

Collection
|--list: Elements are ordered and elements can be duplicated. Because the collection system is indexed.
|--arraylist: The underlying data structures use an array structure. Features: Query speed quickly. But the additions and deletions are slightly slower. Thread is out of sync.
|--linkedlist: A linked list data structure used at the bottom. Features: Adding and deleting speed quickly, query slightly slow. Thread is out of sync.
|--vector: The bottom is the array data structure. Thread synchronization. was replaced by ArrayList. Because the efficiency is low.
|--set: Elements are unordered and elements cannot be duplicated.

List: a unique method. All the methods that can operate the angular standard are the unique method of the system.

Increase:

 Add (index,element);
 AddAll (index,collection);

Delete:remove (index);

Change:set (index,element);

Check:

Get (Index):
 sublist (from,to);
 Listiterator ();
 int indexOf (obj): Gets the location of the specified element.
 listiterator listiterator ();

The iterator that is unique to the list collection. The Listiterator is a iterator sub-interface.

When iterating, the elements in the collection cannot be manipulated by the method of the collection object. Because a concurrentmodificationexception exception can occur.

Therefore, in the iterator, can only use the iterator to pass the operation element, but the iterator method is limited, can only judge the elements, take out, delete the operation,
If you want other actions such as add, modify, etc., you need to use its sub-interfaces, Listiterator. The interface can only be obtained through the Listiterator method of the list collection.

Instance code:

Import java.util.*;
 Class Listdemo {public static void sop (Object obj) {System.out.println (obj);

  public static void Method () {ArrayList Al = new ArrayList ();
  add Element Al.add ("Java01");
  Al.add ("Java02");
  
  Al.add ("java03");
  SOP ("Original set is:" +al);
  Adds an element at the specified location.

  Al.add (1, "java09");
  Deletes the element at the specified location.

  Al.remove (2);
  Modify the element.

  Al.set (2, "java007");
  Gets the element through a corner mark.

  SOP ("Get (1):" +al.get (1));

  SOP (AL);
  Gets all the elements.
  for (int x=0; x<al.size (); x + +) {System.out.println ("Al (" +x+ ") =" +al.get (x));

  Iterator it = Al.iterator ();
  while (It.hasnext ()) {SOP ("Next:" +it.next ());
  ///Get the location of the object through IndexOf.

  SOP ("index=" +al.indexof ("JAVA02"));

  List sub = al.sublist (1,3);
 SOP ("sub=" +sub);
  public static void Main (string[] args) {//Demo list iterator.

  ArrayList al = new ArrayList ();
  add Element Al.add ("Java01");
  Al.add ("Java02");

  Al.add ("java03");

  
  SOP (AL);

  
  Listiterator li = Al.listiterator (); SOP ("hasprevious ():" +li.hasprevIOUs ());

   while (Li.hasnext ()) {Object obj = Li.next ();
    if (Obj.equals ("JAVA02"))//li.add ("java009");


  Li.set ("java006");
  while (Li.hasprevious ()) {SOP ("Pre::" +li.previous ());
  }//sop ("Hasnext ():" +li.hasnext ());


  SOP ("hasprevious ():" +li.hasprevious ());



  SOP (AL);

  ////During the iteration, prepare to add or remove elements.

  Iterator it = Al.iterator ();

   while (It.hasnext ()) {Object obj = It.next ();
    if (Obj.equals ("JAVA02"))//al.add ("java008");

   It.remove ()//deletes the JAVA02 reference from the collection.


  SOP ("obj=" +obj);
  SOP (AL); */
  


 }
}

The above is the Java Collection Interface data collation, hope to help learn Java students.

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.