Java learning summary chapter 1 Java set

Source: Internet
Author: User
I. Summary

1. All Java Collection classes are located in the Java. util package. Unlike Java arrays, Java collections cannot store basic data types, but can only store object references.

2. Set, list, and map are collectively called Java sets.

3. When you store objects in a collection class, to speed up the storage, you must overwrite the equals () method and hashcode () method in the class of the object. That is, if a class re-implements the equals () method, it should also re-implement the hashcode () method, and ensure that when two objects are equal, their hash code is also the same.

4. <? Extends Object> sets an upper limit for the model of the Collection class to accept the object type and its subtypes.

<? Super String> a lower limit is set for the model of the Collection class to accept the string type and its parent type.

5. You can use "for (type T: C) {…}" to traverse the set ){...}" Syntax. C indicates the set to be traversed. type indicates the type of elements in the set. t references the current element retrieved from the set each time.

6. there is also a runtime class for Java collections, that is, Java. util. collections, some of which are specifically used to manipulate list-type collections, and some can be used to manipulate all Collection types or map-type collections.

2. Collection and iterator Interfaces

1. Collection Interface Method

Method

Description

Boolean add (Object O)

Add an object reference to the collection

Void clear ()

Delete all objects in the set, that is, they no longer hold references to these objects.

Boolean contains (Object O)

Determines whether a specified object is referenced in the collection.

Iterator ()

Returns an iterator object that can be used to traverse elements in the set.

Boolean remove (Object O)

Deletes an object reference from a collection.

Int size ()

Returns the number of elements in a collection.

Object [] toarray ()

Returns an array containing all elements in the set.

Boolean isempty ()

Determines whether the set is empty.

Note: The set interface and list interface inherit the collection interface, while the map interface does not inherit the collection interface, so the set object and list object can call the above method, but the map object cannot call the above method.

2. The iterator interface declares the following methods:

A) hasnext (): determines whether the elements in the set have been traversed. If not, true is returned.

B) Next (): returns the next element.

C) Remove (): removes an element returned by the next () method from the set.

3. If the elements in the set are not sorted, The iterator traverses the elements in the set in any order, not necessarily consistent with the order of adding elements to the set.

4. after an iterator object is obtained by using the iterator () method of the collection set, if the current thread or other threads then modify the set through some methods of the collection set (except when the remove () method of the current iterator object is called to modify the Set ), the next () method for accessing this iterator object will cause Java. util. concurrentmodificationexception exception.

3. Set)

1. The set interface mainly has two implementation classes: hashset and treeset. The hashset class has a subclass of the linkedhashset class, which not only implements the hashAlgorithmAnd implements the Linked List data structure. The treeset class implements the sortedset interface and has the sorting function. In addition, there is an array-based set class copyonwritearrayset.

2. The methods of the set series classes do not support insertion or modification. Duplicate objects are not allowed in the Set set.

3. Set uses the equals () method of the object to compare whether two objects are equal, that is, to determine whether an object already exists in the set.

4. When an object is added to the set, hashset calls the hashcode () method of the object to obtain the hash code. Based on the hash code, the location where the object is stored in the set is further calculated.

5. treeset supports natural sorting and custom sorting. By default, treeset uses the natural sorting method.

6. treeset calls the compareto () method of the object to compare the size of the objects in the set and then sort them in ascending order. This sort method is called natural sorting. When using natural sorting, only objects of the same type can be added to the treeset, and the classes of these objects must implement the comparable interface.

7. Immutable classes are the most suitable for treeset sorting.

8. the treeset class supports custom sorting. You can first create a Java implementation. util. comparator <type> interface class, <type> specifies the type of the object to be compared, comparator has a compare (Type X, Type Y) method, used to compare the size of two objects. When the return value of compare (X, Y) is greater than 0, it indicates x> Y. If the return value is less than 0, it indicates x <Y. If the return value is equal to 0, it indicates X is equal to y. ExampleCodeAs follows:

ImportJava. util. comparator;

ImportJava. util. iterator;

ImportJava. util. Set;

ImportJava. util. treeset;

Public Class Customercomparator Implements Comparator{

Public IntCompare (CustomerX,CustomerY ){

If(X. getname (). compareto (Y. getname ()> 0)

Return-1;

If(X. getname (). compareto (Y. getname () <0)

Return1;

Return0;

}

Public Static VoidMain (string [] ARGs ){

Set <Customer> Set =NewTreeset <customer> (NewCustomercomparator ());

CustomerCustomer1 =New Customer("Tom", 15 );

CustomerCustomer2 =New Customer("Jack", 16 );

CustomerCustomer3 =New Customer("Mary", 26 );

Set. Add (customer1 );

Set. Add (customer2 );

Set. Add (customer3 );

Iterator <Customer> It = set. iterator ();

While(It. hasnext ()){

CustomerCustomer = it. Next ();

System.Out. Println (customer. getname () + "" + customer. getage ());

}

}

}

Iv. List)

1. Elements in the list are stored linearly, and repeated objects can be stored in the set. The main implementation classes of the List interface include arraylist (an array with variable length) and list (linked list ). In addition, the revoke list has the addfirst (), addlast (), getfirst (), getlast (), removefirst (), and removelast () methods separately, these methods enable the queue list to be used as a stack, queue, and bidirectional queue.

2. List can only sort objects in the set by index position. If you want to sort objects in the list by other specific methods, you can use the comparator interface and the sort method in the collections class.

3. The listiterator () method of list returns a listiterator object. The listiterator interface inherits the iterator interface and provides a method to manipulate the list:

Add ()

Insert an element to the list

Hasnext ()

Determine whether there is another element in the list

Hasprevious ()

Determines whether the list contains the previous element.

Next ()

Returns the next element in the list.

Previous ()

Returns the previous element in the list.

4. java. util. the aslist () method of the arrays class can encapsulate a Java array as a list object, which represents an array of fixed length, all operations on the list object are applied to the underlying Java array. Therefore, you cannot call the add () and remove () methods of this list object.

5. Map)

1. Map's entryset () method returns a set in which elements of the map. Entry type are stored. Each map. Entry object represents a pair of keys and values in map.

2. MAP has two commonly used implementations: hashmap and treemap. In addition, there are linkedhashmap classes based on linked lists and hash tables. When hashmap is used, the equals () method must be rewritten, And the hashcode () method must be rewritten. treemap implements the sortedmap interface to sort key objects and supports both natural and custom sorting. To sort keys in a custom manner, you can call its treemap (comparator) constructor. The comparator parameter specifies the specific sorting method.

3. Map's keyset () method returns the set of all key objects in the set.

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.