Java collection classes

Source: Internet
Author: User
Tags shuffle


I. Common collection classes
   1. Overview
We can use arrays to hold information, but sometimes the number of messages is indeterminate and the length of the array is uncertain, so there is a set, and his length is variable.
   2. Common collection Classes
There is a figure **********************************
two. List Interface
   1. Features:The deposited elements are ordered, allowing duplicate elements to be stored in the same order as they are taken out.
   2. Common methods:
Add (Index, object); Insert Element
Get (index value); Get element
Remove (index);
Set (Index, object); replace
  3. Class of implementation:
ArrayList:
Internally encapsulates a variable-length array object, ArrayList the collection, adding and removing elements creates a new array, accesses the element through an index, so he is typically used to query the data
Size () Gets the number of elements
LinkedList:
In order to facilitate the deletion and change to have him, the collection internally maintains a two-way loop list.
three. Set interface
  1. Features:The stored data is unordered and does not allow duplicate elements to appear.
   2. Common methods:Basic is the same as list
   3. Implemented classes:
     HashSet:
Use hash values to determine where objects are stored, with good access and query performance
Hashcode (); Gets the hash value of an object
   TreeSet:
Storing elements in a balanced sort binary tree way
four. Map Interface
is a two-column collection in which each element contains a key object key and a value object value, which can be accessed by the key value
   1. Common Methods
Put (Key,value);
Get (key);
ContainsKey (key); whether it is a key
KeySet (); Returns all key-value pairs of the set type
2. Implementation class
HASHMAP:
Key cannot be duplicated
TREEMAP:
Key cannot be duplicated
Five. Generics, collections, Arrays tool class, for Eeach Loop, Iterator
  generic type
   1. Meaning
Allow collections to access only certain types of objects
2. Format:

Collection type < parameterized type > variable name =new collection type < parameterized type > ();
For Eeach loops:
Also called Enhanced for loop
Format: for (element type variable in container: Container variable)
Often used to iterate through the elements in the container, cannot modify
Iterator 
is primarily used for iterative access that is traversed, so the iterator object is called an iterator
   Common methods:
Hasnext (); Determine if the collection has the next element
Next (); Remove the collection element
Collections Tool Class
    Description
Collections two interfaces are set and list, respectively
   Sort operations:
Reverse (List a); Reverses the order of elements in the list collection
Shuffle (List a); random sort, similar to shuffle
Sort (List a); natural sort
  Arrys Tool Class:
Sort (); Order numbers from big to small

2018-08-05

Java collection classes

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.