Java --- 25 --- Collection framework common methods, java --- 25 --- common

Source: Internet
Author: User

Java --- 25 --- Collection framework common methods, java --- 25 --- common

Collection class

Why does a collection class appear?

 

Object-oriented language (OSS) is used to display objects. To facilitate operations on multiple objects, OSS stores objects. A set of objects is the most common way to store objects.

 

What is the difference between containers of arrays and collection classes at the same time?

Although an array can also store objects, its length is not variable. The length of a set is variable. An array can store basic data types. A set can only store objects.

 

Collection class features:

A set is only used to store objects. The length of a set is variable. A set can store different types of objects.

Why are there so many containers?

Because each container stores data differently.

This storage method is called: Data Structure

 

 

Common Methods: (Collection method)

Add, delete, modify, and query.

Method Summary
 boolean add(E e)
Make sure that the collection contains the specified elements (optional ).
 boolean addAll(Collection<? extendsE> c)
Add all elements in the specified collection to this collection (optional ).
 void clear()
Removes all elements from the collection (optional ).
 boolean contains(Object o)
If the collection contains the specified elementTrue.
 boolean containsAll(Collection<?> c)
If the collection contains all elements in the specified collectionTrue.
 boolean equals(Object o)
Compare whether the collection is equal to the specified object.
 int hashCode()
Returns the hash value of this collection.
 boolean isEmpty()
If this collection does not contain elements, returnTrue.
 Iterator<E> iterator()
Returns the iterator that iterates on the elements of this collection.
 boolean remove(Object o)
Remove a single instance of the specified element from this collection. If yes (optional ).
 boolean removeAll(Collection<?> c)
Removes all elements in the collection that are also included in the specified collection (optional ).
 boolean retainAll(Collection<?> c)
Only the elements in the collection that are also included in the specified collection are retained (optional ).
 int size()
Returns the number of elements in the collection.
 Object[] toArray()
Returns an array containing all elements in the collection.
<T> T[]
toArray(T[] a)
Returns an array containing all elements in the collection. The runtime type of the returned array is the same as the runtime type of the specified array.

 

 

The parameter type of the add method is Object to receive any type of objects.

All objects are stored in the collection)



Import java. util. arrayList; public class Main {public static void main (String [] args) {// method_1 (); method_2 ();} public static void method_2 () {ArrayList al1 = new ArrayList (); ArrayList Al = new ArrayList (); al1.add ("abc01"); al1.add ("abc02"); al1.add ("abc03 "); al1.add ("abc04"); al2.add ("abc01"); al2.add ("abc02"); al2.add ("abc05"); al2.add ("abc06"); al1.removeAll (Al ); // remove the intersection between al1 and Al 2 and save it in al1 sop (al1); sop (Al);/** print the result: * [abc03, abc04] ** [abc01, abc02, abc05, abc06] ***/} public static void method_1 () {// create a Collection container and use the Collection interface subclass ArrayListArrayList a1 = new ArrayList (); // Add the a1.add ("abc01"); a1.add ("abc02"); a1.add ("abc03"); a1.add ("abc04"); // obtain the length of the number set System. out. println (a1.size (); // print the collection System. out. println (a1); // Delete the a1.remove ("abc03") element; System. out. println (a1);/** print result 4 [abc01, abc02, abc03, abc04] [abc01, abc02, abc04] * // a1.clear (); // clear the set // determine whether an element is in the SET System. out. println (a1.contains ("abc01"); // determines whether the set is empty. out. println (a1.isEmpty ();} public static void sop (Object obj) {System. out. println (obj );}}


================================== Iterator ========== ======================================

An iterator is actually a collection used to retrieve elements.

 

Extracts and operates on the elements.



Method Summary
 boolean hasNext()
If there are still elements that can be iterated, returnTrue.
 E next()
Returns the next element of the iteration.
 void remove()
Remove the last element returned by the iterator from the collection to which the iterator points (optional ).



Public class Main {public static void main (String [] args) {// TODO Auto-generated method stubArrayList al1 = new ArrayList (); al1.add ("abc01 "); al1.add ("abc02"); al1.add ("abc03"); al1.add ("abc04"); // Iterator it = al1.iterator (); // get Iterator, used to retrieve elements in the Set ///// sop (it. next (); // return the next element of the iteration. /// Sop (it. hasNext (); // returns true if there are still elements that can be iterated. // while (it. hasNext () // {// sop (it. next (); //} for (Iterator it = al1.iterator (); it. hasNext ();) {sop (it. next () ;}} public static void sop (Object obj) {System. out. println (obj );}}


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.