Javase Basic Note Nine

Source: Internet
Author: User

Seventh Chapter Set


1. Storage of objects: ① Array (base data type & Reference data type) ② collection (reference data type)

The drawbacks of storing data in arrays: 1. Once created, its length is immutable 2. The number of objects that a real array holds is not known

2. Collection frame:

Collection interface:

|----List interface: Stores an ordered, repeatable element equivalent to a "dynamic" array

New method: Delete remove (int index) modify set (int index,object obj) get get (int index)

Insert Add (int index,object obj)

the class in which the element (object) is added into the list collection must override the Equals () method

|----ArrayList (Main implementation Class)

|----LinkedList (for frequent INSERT, delete operations)

|----Vector (Old implementation class, thread safest, efficiency less than ArrayList)


|----SET interface: Stores unordered, non-repeatable elements

Disorder: Not equal to random, where elements are stored in the underlying location is unordered

Non-repeatable: When the same element is added to the set, the latter is not added

The methods commonly used in set are defined under the collection interface.

The class that contains the elements that are added into the set set must override the Equals () and Hashcold methods, and the Equals () and Hashcold methods remain consistent

|----HashSet (Main implementation Class)

|----Linkedhashset: is a subclass of HashSet, when traversing the collection elements, is implemented in the order of addition: frequent traversal, less add, more efficient insert operation, it is recommended to select it

|----Threeset: can be sorted according to the specified attributes of the elements added to the collection

The elements that require threeset to be added must be of the same type

Natural sort: ① the class implements comparable interface that requires the element to be added into the Threeset

② overrides Comparableto (Object obj), which in this method indicates which property of the element to sort by

③ add elements to the Threeset, if you do not implement this interface, run the exception

Custom sort: ① Create an implementation class object that implements the comparator interface, overriding the comparator compare (object O1,object O2) method in the Implementation class

② in this compare () method to sort by which property of the class The element is located in

③ the object of the implementation class that implements the comparator interface is passed as a parameter to the constructor of TreeSet

④ add elements to the TreeSet, if you do not implement this interface, run the exception

The CompareTo or compare () method that requires rewriting is consistent with the Equals () and Hashcold methods


Method: 1. Add (Object obj), add all (Collection coll),size (), Clear () IsEmpty ();

2.remove (object obj), RemoveAll (Collection coll), Retainall (Collection coll), equals (Object obj), contains ( Object obj), Containsall (Collection coll);

3.iterator (), ToArray ();

Map Interface : Store "key-value" pairs of data, equivalent to high-school function y=f (x) key is not repeatable, use set to store value can be repeated, using collection to store, a key-value to form a entry ( Map.entry), entry use set to store

add \ Modify put (object Key,object value) Delete Remove (object key) Get Get (object key) size () /KeySet () VALUES () EntrySet ()

|----HASHMAP: Main implementation class

|----Linkedhashmap:hashmap, which can be traversed in the order in which they were added to the map

|----TREEMAP: must be sorted according to the specified attribute of the class where key is located, requiring that key be of the same type. Natural/Custom sequencing for key considerations

|----Hashtable: Old implementation class, thread-safe, non-additive null key, null value, not recommended

Subclass: Properties: Commonly used to process property files


Iterator interface: Used to traverse the collection collection elements

Collections Tool Class: Tool class using collection and map, mostly static



This article from the "Ah Cheng Blog" blog, reproduced please contact the author!

Javase Basic Note nine

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.