Common Operations on the List set in Java, javalist set

Source: Internet
Author: User

Common Operations on the List set in Java, javalist set

Directory:

 

Note: the code in the content is correlated.

1. Add, retrieve, and delete elements in the list;

The add method is :. add (e); The Obtaining method is :. get (index); the deletion method is :. remove (index); Delete by index ;. remove (Object o); Delete according to the element content;

List <String> person = new ArrayList <> (); person. add ("jackie"); // The index is 0 //. add (e) person. add ("peter"); // The index is 1person. add ("annie"); // The index is 2person. add ("martin"); // The index is 3person. add ("marry"); // The index is 4person. remove (3 );//. remove (index) person. remove ("marry ");//. remove (Object o) String per = ""; per = person. get (1); System. out. println (per );////. get (index) for (int I = 0; I <person. size (); I ++) {System. out. println (person. get (I ));//. get (index )}

 

2. Whether the list contains an element;

Method:. contains (Object o); returns true or false

List <String> fruits = new ArrayList <> (); fruits. add ("apple"); fruits. add ("banana"); fruits. add ("Peach"); // for Loop listfor (int I = 0; I <fruits. size (); I ++) {System. out. println (fruits. get (I);} String appleString = "apple"; // true or falseSystem. out. println ("Does fruits contain Apple:" + fruits. contains (appleString); if (fruits. contains (appleString) {System. out. println ("I like Apple");} else {System. out. println ("I'm not happy ");}

 

3. Change (replace) the element values in the list based on the index );

Note the differences between. set (index, element); and. add (index, element;

String a = "White Dragon Horse", B = "Sha Monk", c = "ba Jie", d = "Tang Seng", e = "Wukong "; list <String> people = new ArrayList <> (); people. add (a); people. add (B); people. add (c); people. set (0, d );//. set (index, element); // place d Tang Seng in the list to 0 and replace a White Dragon Horse people. add (1, e );//. add (index, element); // put e Wukong to the position where the index is 1 in the list, and the B Sha monk at the original position moves one after. // enhance the for loop traversal listfor (String str: people) {System. out. println (str );}

 

4. View (judge) the index of the element in the list; 

Note:. indexOf (); and lastIndexOf () are different;

List <String> names = new ArrayList <> (); names. add ("Liu Bei"); // The index is 0 names. add ("Guan Yu"); // The index is 1 names. add ("Zhang Fei"); // The index is 2names. add ("Liu Bei"); // The index is 3names. add ("Zhang Fei"); // The index is 4System. out. println (names. indexOf ("Liu Bei"); System. out. println (names. lastIndexOf ("Liu Bei"); System. out. println (names. indexOf ("Zhang Fei"); System. out. println (names. lastIndexOf ("Zhang Fei "));

 

5. Determine the index position of an element;

If (names. indexOf ("Liu Bei") = 0) {System. out. println ("Liu Bei here");} else if (names. lastIndexOf ("Liu Bei") = 3) {System. out. println ("Liu Bei there");} else {System. out. println ("Where is Liu Bei? ");}

 

6. Use the index position in the list to re-generate a new list (truncation set );

Method:. subList (fromIndex, toIndex);. size (); this method obtains the sum of the number of elements in the list.

List <String> phone = new ArrayList <> (); phone. add ("Samsung"); // The index is 0 phone. add ("apple"); // The index is 1 phone. add ("Hammer"); // The index is 2phone. add ("Huawei"); // The index is 3phone. add ("Xiaomi"); // The index is 4 // the original list is traversed for (String pho: phone) {System. out. println (pho);} // generate a new listphone = phone. subList (1, 4 );//. subList (fromIndex, toIndex) // use the object of Index 1-4 to generate a new list, but it does not contain the element with index 4, 4-1 = 3for (int I = 0; I <phone. size (); I ++) {// phone. size () This method returns the number of elements in the list and System. out. println ("the new list contains" + phone. get (I ));}

 

7. Compare all elements in the two lists;

// The equals method of two equal objects must be true, but the two objects with the same hashcode are not necessarily equal objects.

// 1.
If (person. equals (fruits) {System. out. println ("all elements in the two lists are the same");} else {System. out. println ("all elements in two lists are different");} // 2.if (person. hashCode () = fruits. hashCode () {System. out. println ("we are the same");} else {System. out. println ("we are different ");}

 

8. Check whether the list is empty;

// If it is null, true is returned. If it is not null, false is returned.

If (person. isEmpty () {System. out. println ("null");} else {System. out. println ("not empty ");}

 

9. Return the Iterator set object;

System. out. println ("Returned Iterator collection object:" + person. iterator ());

 

1 + 0. Convert the set to a string;

String liString = ""; liString = person. toString (); System. out. println ("convert the set to String:" + liString );

 

11. Convert the set into an array;

System. out. println ("convert the set to an array:" + person. toArray ());

 

12. Set type conversion;

// 1. default type List <Object> listsStrings = new ArrayList <> (); for (int I = 0; I <person. size (); I ++) {listsStrings. add (person. get (I);} // 2. list <StringBuffer> lst = new ArrayList <> (); for (String string: person) {lst. add (StringBuffer (string ));}

 

Complete code:

Package MyTest01; import java. util. arrayList; import java. util. list; public class ListTest01 {public static void main (String [] args) {// Add, retrieve, and delete the element list <String> person = new ArrayList <> (); person. add ("jackie"); // The index is 0 //. add (e) person. add ("peter"); // The index is 1person. add ("annie"); // The index is 2person. add ("martin"); // The index is 3person. add ("marry"); // The index is 4person. remove (3 );//. remove (index) person. remove ("marry ");//. remove (Object o) String per = ""; per = person. get (1); System. out. println (per );////. get (index) for (int I = 0; I <person. size (); I ++) {System. out. println (person. get (I ));//. get (index)} // whether the list contains a List of elements <String> fruits = new ArrayList <> (); fruits. add ("apple"); fruits. add ("banana"); fruits. add ("Peach"); // for Loop listfor (int I = 0; I <fruits. size (); I ++) {System. out. println (fruits. get (I);} String appleString = "apple"; // true or falseSystem. Out. println ("Does fruits contain Apple:" + fruits. contains (appleString); if (fruits. contains (appleString) {System. out. println ("I like Apple");} else {System. out. println ("I'm not happy");} // change (replace) the element value in the list based on the index String a = "White Dragon Horse", B = "Sha Monk ", c = "", d = "", e = "Wukong"; List <String> people = new ArrayList <> (); people. add (a); people. add (B); people. add (c); people. set (0, d );//. set (index, element) // place d Tang Seng in the list to 0 and replace a White Dragon Horse people. add (1, e );//. add (I Ndex, element); // put e Wukong to the position where the index is 1 in the list, and the B-sand monk at the original position moves one after. // enhance the for loop traversal listfor (String str: people) {System. out. println (str);} // The index list of the (judgement) element in the List <String> names = new ArrayList <> (); names. add ("Liu Bei"); // The index is 0 names. add ("Guan Yu"); // The index is 1 names. add ("Zhang Fei"); // The index is 2names. add ("Liu Bei"); // The index is 3names. add ("Zhang Fei"); // The index is 4System. out. println (names. indexOf ("Liu Bei"); System. out. println (names. lastIndexOf ("Liu Bei"); System. out. println (names. indexOf ("Zhang Fei ")); System. out. println (names. lastIndexOf ("Zhang Fei"); // determine if (names. indexOf ("Liu Bei") = 0) {System. out. println ("Liu Bei here");} else if (names. lastIndexOf ("Liu Bei") = 3) {System. out. println ("Liu Bei there");} else {System. out. println ("Where is Liu Bei? ");} // Use the index position in the list to regenerate a new list (truncated set) List <String> phone = new ArrayList <> (); phone. add ("Samsung"); // The index is 0 phone. add ("apple"); // The index is 1 phone. add ("Hammer"); // The index is 2phone. add ("Huawei"); // The index is 3phone. add ("Xiaomi"); // The index is 4 // the original list is traversed for (String pho: phone) {System. out. println (pho);} // generate a new listphone = phone. subList (1, 4 );//. subList (fromIndex, toIndex) // use the object of Index 1-4 to generate a new list, but it does not contain the element with index 4, 4-1 = 3for (int I = 0; I <phone. size (); I ++) {// phone. size () This method returns the number of elements in the list and System. out. println ("the new list contains" + phone. get (I);} // compare all the elements in the two lists. // The equals method of the two equal objects must be true, but the two objects with the same hashcode are not necessarily the same object if (person. equals (fruits) {System. out. println ("all elements in the two lists are the same");} else {System. out. println ("all elements in two lists are different");} if (person. hashCode () = fruits. hashCode () {System. out. println ("we are the same");} else {System. out. println ("we are different");} // determines whether the list is empty. // if it is null, true is returned. If it is not null, falseif (person. isEmpty () {System. out. println ("null");} else {System. out. println ("not empty");} // returns the Iterator set object System. out. println ("Returned Iterator set object:" + person. iterator (); // convert the set to String liString = ""; liString = person. toString (); System. out. println ("convert a set to a string:" + liString); // convert a set to an array. The default type is System. out. println ("convert a set to an array:" + person. toArray (); // convert the set to a specified type (friendly processing) // 1. default type List <Object> listsStrings = new ArrayList <> (); for (int I = 0; I <person. size (); I ++) {listsStrings. add (person. get (I);} // 2. list <StringBuffer> lst = new ArrayList <> (); for (String string: person) {lst. add (StringBuffer (string) ;}} private static StringBuffer (String string) {return null ;}}

 

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.