Java Notes--collection

Source: Internet
Author: User
Tags addall int size set set

1. The Java Collection class can be used to store multiple objects of unequal quantity, and can also be used to hold associative arrays that have a mapping relationship.

2. Java collection can be divided into collection and map two systems:

--collection:1) Set: An unordered, non-repeatable set of elements; 2) List: An orderly, repeatable set of elements

--map: A collection that has a mapping relationship of "key/value pairs".

3. Collection interface:

|----List interface:

|----ArrayList, linkedlist, Vector

|----SET Interface:

|----HashSet, linkedhashset, TreeSet

Map interface:

|----HashMap, LINKEDHASHMAP, TreeMap, HashTable (sub-category: Properties)

4. Common methods defined in the collection interface:

--add (object o);//Add object;

--addall (Collection col);//Add all the elements inside the col to the current set;

--clear ();//empties the current collection;

--contains (object o);//Determine whether the current combination contains an object o;

--containsall (Collection col);//Determines whether the current collection contains all objects in the set col;

--isempty ();//Determines whether the current collection is empty;

--remove (object o);//Remove Object o

--removeall (Collection col);//Remove the current set and Col-Public objects (differential set);

--size ();//Gets the current set length (number of elements);

--retainall (Collection col);//The public element of the current collection and Col, and returns to the current set (intersection);

--equals (Collection col);//Determines whether the current set and the object in the set Col are all equal;

--iterator ();//Returns the current set iterator;

5. New method in list relative to collection:

void Add (int index, Object o);//add element at the established index position

Boolean addall (int index, Collection col);//Add elements in col at the specified index position

Object get (int index);//Gets the element with the specified index

int indexOf (Object o);//return element o The first occurrence in the collection, or 1 if no o is present

int lastIndexOf (Object o);//Returns the element o the last occurrence in the collection, or 1 if no o is present

Object Remove (int index);//delete the element that specifies index

Object Set (int index, Object o);//setting specifies that the element of index is O

List sublist (int fromIndex, int toindex);//Returns the subset of the current collection from FromIndex to Toindex

6, HashSet When adding a custom type, to achieve its non-repeatability, you must override the Equals () and Hashcode () two methods, write only Equals () is not guaranteed.

--When adding an object to a set, first call the Hashcode () method of the class in which this object is located, and compute the hash value of this object, which determines where the object is stored in set. If there is no object store before this location, the object is stored directly at this location, if the object is already stored at this location, and then the two objects are compared by equals (). If the same, the latter object cannot be added.

Note: The Johasi value is the same, but Equals () is false, and two objects are stored in the same location, which is generally not recommended, and the Hashcode () method needs to be redesigned.

7, Linkedhashset: Use the list to maintain the order of additions, when traversing the collection, is in the order of the added (performance is slightly lower than hashset).

8, Treeset:1) The added elements must be of the same type;

2) can be traversed in the specified order of elements added to the collection, for example, int is from small to large;

3) There are two kinds of sort: natural sort, custom sort

4) The natural sort needs to implement the comparable interface, and the custom sequencing requires the implementation of the comparator interface.

5) When adding elements to TreeSet, the first comparison is performed by CompareTo (), and once you return 0, the program will think that the added element is the same as the existing element and cannot be added.

9. Map interface Method:

Object put (object key, object value);

Object remove (object key);

void Putall (map map);

void Clear ();

Object get (object key);

Boolean ContainsKey (Object key);

Boolean Containvalue (Object value);

int size ();

Boolean isEmpty ();

Boolean Equals ();

10. Map Traversal:

1) Traverse Key Set

Set set == set.iterator ();  while (It.hasnext ()) {    System.out.println (It.next ());}

2) traversing Valus set

Collection coll == coll.iterator ();  while (It.hasnext ()) {    System.out.println (It.next ());}

3) Traverse Key-value (entry) Set

Way one, by traversing keyset to get value

Set set == set.iterator ();  while (It.hasnext ()) {    = it.next ();     = Map.get (key);     + "--->" + value);}

Mode two, traverse entry set directly

Set Set1 = map.entryset ();  for (Object obj:set1) {    = (map.entry) obj;     + "--->" + entry.getvalue ());}

11. TreeMap: Sort by the specified attribute of the key that is added to the element in the map. Key must be of the same type.

12, Hashtable is a map implementation class, thread-safe, unlike HashMap, it does not allow NULL as key and value, rarely used.

13, properties: Commonly used to process property files, the keys and values are of type string.

 Public Static void throws FileNotFoundException, IOException {    new  Properties ();    Pros.load ( new FileInputStream ( "jdbc.properties"));             = Pros.getproperty ("user");    SYSTEM.OUT.PRINTLN (user);             = Pros.getproperty ("PSW");    System.out.println (PSW);}

14. Collections is a tool class that operates set, list, and map sets, provides a series of static methods for sorting, querying, and modifying collection elements, and also provides a method for synchronizing collection object settings and implementing synchronization control on collection objects.

Common methods: 1) reverse (list);//Reverses the elements in the list

2) Shuffle (list);//random Ordering of list collection elements

3) sort (list);//Sort list by natural sort of elements

4) Sort (list, Comparator);//Sort list by custom sort

5) Swap (list, int, int),//to swap the two elements in the specified list

6) Object Max (Collection);//returns the maximum value according to the natural order of the element

7) Object Max (Collection, Comparator);

8) Object min (Collection);//returns the minimum value according to the natural order of the element

9) Object min (Collection, Comparator);

int frequency (Collection, object);//Returns the number of occurrences of the specified object in the collection

a) void copy (list dest, list src);//copy elements from SRC to dest

A Boolean replaceall (List List, Object Oldval, Object newval);//Replace oldval in the specified list with newval

Java Notes--collection

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.