JAVA generic and common data structures and collections

Generic and common data structures and collections


 

Collection & iterator

  FIG set architecture

  Due to the different data structures (organization, how data is stored), it provides a different set of java for us

  Different sets of their functions are similar, keep up the extraction, the extracted common

 

Collection of commonly used functions

Boolean the Add (Object E): was added to the collection of elements
 void Clear (): Clear all the elements set
 Boolean the contains (Object O): determines whether the collection contains an element
 Boolean isEmpty (): determined set of elements is empty
 Boolean remove (Object O): to delete an element based on the content element
 int size (): Get the length of a set of 
Object [] toArray (): can be set into an array and the elements stored in the collection into an array

collections

Collection and Collections What is the difference?

    Collection is the most top-level collection system, including a common collection system

    Collections class is a tool, the method is for operating a Collection

static  void the swap (List List, int I, int J): The specified list of two index positions interchanged
 static  void   Sort (List <T> List): the list sorted according to the natural order of elements in the
 static  void shuffle ( list list): random permutation
 static  void reverse (list list): reverse
 static  void the fill (list list, Object obj): use the specified object to fill all of the elements specified list of
 static  void Copy (list dest, list src): is the coverage data source list to the target list, note: the target list is at least equal to the length of the source list length
 static  int   lookup index location specified in the list of the specified element binarySearch (list list, Object key) using the binary search method

 

List interface ( ordered, can be null, repeatable )

  Unique features:

void the Add (int index, E Element): add an element to index the index position 
E GET (int index): Gets the element according to index index 
E the Remove (int index): delete elements based index index 
E set (int index, E element ): the index position of the element index set element

  LinkedList unique features

LinkedList underlayer using a list structure, and therefore fast deletions, ArrayList relatively slow query 
void the addFirst (E E): add elements to the head of the list 
void addLast (E E): add elements to the end of the list 
 E getFirst (): Get element head of the chain, and does not remove elements 
 E getLast (): Get the element end of the chain, does not remove elements 
 E removeFirst (): returns the element head of the chain and remove elements strand head 
 E removeLast (): returns the element end of the chain and remove element end of the chain

 

List of common subclasses:

 *      ArrayList

 * The underlying structure is an array, query fast, slow additions and deletions

 *      LinkedList

 * Underlying structure is the list, the query is slow, fast additions and deletions

 


Set interface ( disorder can be null, not allowed to repeat, there is no index )

  Features Set collection: unordered, can be null, not allowed to repeat, there is no index

    Set is an interface because it is not directly instantiate an object

Set<String> set = new HashSet<String>();

    The interface is mainly inherited from the Collections interface, it has some common methods of Collection.

 

Set common subclasses:

  HashSet: the underlying data structure is a hash table

New elements have been added to the set of elements are compared one by one HashSet collection of 

first compare hash values (each element calls the hashCode () generates a hash value) 

If you already have a set of new elements and added elements hash values are different, the newly added elements into the collection 

the same as if a set of new elements have been added to the hash value of an element, this time also need to call equals (Object obj) Compare 

If equals (Object obj) method returns true, indicating that the same set of attribute values and new elements have been added in an element, then the newly added element is not stored in the collection 

if equals (Object obj) method returns false, indicating that the element with the newly added collection property values of existing elements are different, then the newly added elements into the collection

   TreeSet: the underlying data structure is a red-black tree

 

 


 

Collection summary

 

 

If you know Set, but do not know which Set, to use HashSet. 
                    Multi-query: ArrayList 
                    additions and more: LinkedList 
if you know the List, but do not know which List, to use ArrayList. 
     
If you know Collection collection, but do not know who to use, use ArrayList. 
If you know a collection, use ArrayList.

 

 

 


 

Iterator

Traversal collection:

       1.toArray (), can be converted into a set of arrays, the array can then traverse

       2.iterator (), can return an iterator object, we can iterate through the collection iterator object

    Private  static  void method2 () { 
        Collection C = new new the ArrayList (); 
        c.add ( "Hello" ); 
        c.add ( "World" ); 
        the Iterator IT = c.iterator (); 

        the while (it.hasNext ()) { 
            System.out.println (it.next ()); 
        } Object next (): returns the next element boolean hasNext (): determining whether all elements can be acquired
    

  Note: An iterator is dependent on the collection, the equivalent of a copy of the collection, when the iterator when in operation, and if you find a collection of different, an exception is thrown


foreach & Generics

Since the collection can store any type of objects, when we store different types of objects, type conversion is likely to occur at the time of abnormal conversion,

 * So java To solve this problem, provides us with a mechanism, called generics

 *

 * Generics: a broad range of types, the type of work ahead of clear data to compile time, learn the characteristics of the array

 * Generic benefits:

 * Avoid the problem of type conversion

 * You can reduce the yellow warning line

 * We can simplify writing code

 

the foreach : Enhanced for loop, collection or array is generally used to traverse

 * Format:

 * For (type variable elements: a collection of objects or arrays) {

 * Can be used as a variable;

 *      }

    Note: You can not modify the collection in the enhanced for loop, otherwise there will be concurrent modification exception.

 

 

Guess you like

Origin www.cnblogs.com/viperqy/p/11369748.html