Java Collection and Java Collection

Source: Internet
Author: User

Java Collection and Java Collection

1. Collection is the parent class of all sets. After JDK1.5, The Iterable super class is added (you don't need to know)

2. The learning set starts from the Collection, and all sets inherit their methods.

Set structure:

Mainly learning

Add () method

Add an element to a collection

Collection <String> list = new ArrayList <> (); // The parent class references a subclass object.
List. add ("");
List. add ("");
List. add ("B ");
List. add ("");

Bytes -----------------------------------------------------------------------------------------------------------

Remove () method

Remove elements from a set

List. remove ("");

Note: In the Collection interface, the remove () method can only pass fixed elements, but cannot be passed in. It is only a subclass that overrides the function of the remove () method.

Bytes -----------------------------------------------------------------------------------------------------------

Clear () method

Clear collection

List. clear ();

Bytes -----------------------------------------------------------------------------------------------------------

Size () method

Obtains the length of a set.

List. size ();

The length of the string is length (). The length of the array is the length attribute. Do not confuse it.

Bytes -----------------------------------------------------------------------------------------------------------

Contains () method

Contains the input value to check whether the set contains this element. The return value belongs to the Boolean type.

It is usually used to deduplicate elements in the ArrayList set.

Bytes -----------------------------------------------------------------------------------------------------------

ToArray () method

Convert a set into an array

Interger [] arr = list. toArray (new Integer [0]);

Convert array to set

List <Integer> list = Arrays. aslist (arr );

Bytes -----------------------------------------------------------------------------------------------------------

Collection <String> list = new ArrayList <> ();
List. add ("");
List. add ("");
List. add ("B ");
List. add ("");
System. out. println (list );
System. out. println (list. toString ());

List. remove ("");
System. out. println (list );

Boolean B = list. contains ("");
System. out. println (B );

String [] str = list. toArray (new String [0]);
System. out. println (Arrays. toString (str ));

List <String> list1 = Arrays. asList (str );

List. clear ();
System. out. println (list );

Bytes -----------------------------------------------------------------------------------------------------------

Traverse a set (delete an element)

Public static void main (String [] args ){
Fun2 ();

}
/**
* Normal for deletion
*/
Public static void fun (){
ArrayList <String> list = new ArrayList <> ();
List. add ("");
List. add ("");
List. add ("B ");
List. add ("");
For (int I = 0; I <list. size (); I ++ ){
If (list. get (I). equals ("")){
// Here I -- if the connected element is found, an index should be pushed forward to the element. Because the set deletes an element, the index of this element will be overwritten by the next element,

// Cause the next element to be uninterpreted

List. remove (I --);
}
}
System. out. println (list );
}

/**
* Enhanced for deletion
* The element enhancement for index with no record list cannot be deleted.
*/
Public static void fun1 (){
ArrayList <String> list = new ArrayList <> ();
List. add ("");
List. add ("");
List. add ("B ");
List. add ("");
For (String string: list ){
If (string. equals ("B ")){
List. remove ("B ");
}
}
System. out. println (list );
}
/**
* When deleting the iterator, you must use a value to record the value of iterator. next. If no record exists, the connected elements cannot be deleted.

* Because when you delete an element in a set, the index will be directed to the next index during execution.
*/
Public static void fun2 (){
ArrayList <String> list = new ArrayList <> ();
List. add ("");
List. add ("");
List. add ("B ");
List. add ("");
Iterator <String> iterator = list. iterator ();
While (iterator. hasNext ()){
String string = iterator. next ();
If (string. equals ("")){
Iterator. remove ();
}
}
System. out. println (list );
}
}

This is my first blog. I hope you can communicate with each other. Could you give me a question? Thank you!

Related Article

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.