Java Common Collection Learning summary

Source: Internet
Author: User

An array

Arrays can store a container of basic data types and objects that are fixed in length, so they are not suitable for use when the number of objects is unknown.

Arrays: A tool class for manipulating array objects, which are static methods.

Arrays.aslist: Converts an array to a List, which can be used as a parameter to other collection type constructors.

 privatestaticvoid  arraytolist () {                = {"Hello", "Hi"};            List<String> list =  arrays.aslist (arr);            SYSTEM.OUT.PRINTLN (list);        

Arrays.binarysearch: Quick Find in a sorted or one segment.
Arrays.copyof: Enlarging the array capacity without changing its contents
Arrays.copyofrange: You can copy an entire array or part of it.
Advanced version of Arrays.deepequals, Arrays.deephashcode:arrays.equals/hashcode, supports operation of sub-arrays.
Arrays.equals: If you want to compare two arrays for equality, you should call this method instead of the Equals method in the Array object (the Equals () method is not overridden in the array object, so this method compares references without comparing the contents).
Arrays.fill: Fills the entire array or part of it with a given value.
Arrays.hashcode: Used to calculate its hash value based on the contents of the array (the hashcode () of the array object is not available).
Arrays.sort: Sorts the entire array or part of an array. You can also use this method to sort an array of objects with a given comparer.
Arrays.tostring: Prints the contents of the array.

*************************************************************************************************************** ********************************************************************************************

Two List

List objects are ordered, repeatable,

List has a series of index-related methods, fast query speed , insert delete data slow.

    • ArrayList: The underlying use of the array structure, features: Fast query, adding and deleting operations are slow, and the thread is not synchronized.
      Common methods: add addAll remove indexOf subList , c
      ,,,, ontains , isEmpty ... ..
    • LinkedList

*************************************************************************************************************** ********************************************************************************************

Three Set

Set unordered and non-repeating
The objects in the set collection are not sorted in a particular way, simply by adding the object to the collection

  HashSet class (hash stored) the Java.util.HashSet class implements the Java.util.Set interface

Code

TreeSet (ordered storage) describes a variant of set -a collection of functions that can implement sorting

 public  class   testtreeset{ public  static  void   main (String [] args) {       TreeSet ts  =new   TreeSet ();       Ts.add ( orange " "Apple"  "banana"  "Grape"  =ts.iterator ();  while   (It.hasnext ()) {String fruit            =
Testtreeset

*************************************************************************************************************** ********************************************************************************************

Four Map

    • HashMap
      The implementation of a hash table-based MAP interface that stores key-value pairs, which are unique, allow null values and NULL keys, are non-synchronous (except for non-synchronous and allow null, the HashMap class is roughly the same as Hashtable), and synchronization can be usedMap m = Collections.synchronizedMap(new HashMap(...));
      Common methods:
      1. Add to. Put (Key,value): When the stored key is the same, the new value replaces the old value and returns the old value. Returns NULL if the key is not duplicated. void Putall (MAP);
      2. Delete. void Clear (): Empty value Remove (key): Deletes the specified key.
      3. Judge. Boolean IsEmpty (): Boolean ContainsKey (Key): Contains key Boolean containsvalue (value): Contains value
      4. Remove. int size (): Returns the length of value get (key): Gets the corresponding value by specifying the key. If NULL is returned, you can tell that the key does not exist. Of course there is a special case, that is, in the HashMap collection, the null key can be stored in the null value.
        Collection values (): Gets all the values in the Map collection.
      5. How to traverse a map:

        1. All keys in the map collection are taken out into the set set.

        Set<k> KeySet () returns the set set of all key objects and gets the value corresponding to the key by the Get method.

        2, values (), get all the values.

        Collection<v> values () cannot get to the key object

        3, Map.entry object recommended use focus

        Set<map.entry<k,v>> EntrySet () packages the key-value mappings in the Map collection into an object.

        The Map.entry object obtains its key and value through the Getkey,getvalue of the Map.entry object.

    • HashTable
      The bottom layer is a hash table data structure that cannot be stored in null, NULL, or thread synchronization.
      The Hashtable instance has two parameters that affect its performance: 初始容量 and加载因子

Java Common Collection Learning summary

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.