Java review--collection Array helper class

Source: Internet
Author: User
Tags mutex

1. Collection Help Class

Basically, the key is to understand that collections is a collection of helper classes, not arrays of helper classes;

1) Generate a read-only collection instance object

A list of collections.singletonlist--used to generate a single, read-only element

collections.singletonmap--is used to generate a read-only map of single key and value

collections.singleton--set used to generate a read-only single element

collections.unmodifiablelist--used to generate a read-only list

collections.unmodifiablemap--is used to generate a map of read-only key and value

collections.unmodifiableset--used to generate read-only set

If the lookup performs a first off non-read-only operation, an exception occurs: unsupportedoperationexception

2) Check the type of the collection element

Checkedcollection,checkedlist,checkedmap,checkedset,checkedsortedmap,checkedsortedset

If the check finds that the element type mismatch is a classcastexceptions exception, although the functionality of generics is provided in Java5, the generic feature is able to constrain the type of elements in the collection during the code compilation phase, but sometimes the set of declarations may be raw (primitive type, That is, no generic collection is used, the type constraints of the compile phase do not work, and at this point the checked collection can play the role of the element type in the constraint set.

3) Synchronizing the collection elements

The Synchronizedxxxxx series method returns a Synchronized collection class (Synchronizedmap, synchronizedlist). The internal implementations of these collection classes implement synchronization of these collection operations through a mutex (mutex). is to turn an asynchronous set into

4) Set Operation

Includes sorting, finding, replacing, shifting, counting, filling, etc.

Fill-replaces all elements in the specified list with the specified element.

frequency-returns the number of elements in the specified collection that are equal to the specified object.

Indexofsublist-returns the starting position of the specified target list for the first occurrence in the specified source list, or 1 if no such list appears.

lastindexofsublist-returns the location of the last specified target list in the specified source list, or 1 if no such list appears.

max--returns the largest element of a given collection, based on the natural order of the elements.

min--returns the smallest element of a given collection based on the natural order of the element.

replaceall--replaces all occurrences of a specified value in a list with another value.

reverse--the elements in the list in reverse order

shuffle--the elements in the list are arranged

sort--to sort elements in a list

swap--the position of a two specified subscript element in the collection in the interchange list.

rotate--Loop movement. It's hard to understand the way the loop moves, and the following example will give you a sense of what this means.

binarysearch--uses a binary search algorithm to search the specified list for the specified object.

5) Other operations

addall--adds all the specified elements to the specified collection.

copy--copy all elements from one list to another list.

disjoint--returns True if there are no identical elements in the two specified collection.

ncopies--returns an immutable list of n replicas of a specified object.


2. Array Helper Class

Basically, the key is to understand that Arrays is an array helper class, not a collection of helper classes;

1) assigning Values to arrays

: Through the Fill method.


2) Sorting the array

: By the Sort method, in ascending order.


3) Comparing arrays

: Compares the values of elements in an array by the Equals method.


4) Finding array elements

: The BinarySearch method can be used to perform binary lookups of sorted arrays.


5) Convert the array to a collection

: The elements of the array are converted to the elements of the collection by means of the Aslist method.


6) Convert the array in string form

: By the ToString method, the array call to ToString returns the class @ address, now the print element


7) Copying elements of an array

: The array that is responsible for specifying the elements through CopyOf, Copyofrange


Java review--collection Array helper class

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.