Common operations for list collections in Java

Source: Internet
Author: User

Directory:

    1. List to add, get, delete elements;
    2. Whether the list contains an element;
    3. The value of the element is changed (replaced) by the index in the list;
    4. The index of the view (judging) element in the list;
    5. Judging by the index position of the element;
    6. Regenerate a new list (intercept collection) using the index position in the list;
    7. Compares all the elements in the two list;
    8. Determines whether the list is empty;
    9. Returns the Iterator collection object;
    10. Converts a collection to a string;
    11. Converts a collection into an array;
    12. collection type conversion;

Note: The code in the content is associative.

1.list Add, get, delete element;

The Add method is:. Add (E); Get method is:. Get (index); Delete method:. Remove (index); Delete by index; remove (Object o); Delete by element content;

List<string> person=new arraylist<> ();p Erson.add ("Jackie");//index is 0//.add (e) person.add ("Peter");// Index is 1person.add ("Annie");//Index is 2person.add ("Martin");//Index is 3person.add ("marry");//Index is 4person.remove (3);//.remove ( Index) Person.remove ("Marry"),//.remove (Object o) String per= "";p er=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 an element is included in the 2.list;

Method:. Contains (Object o); returns True or False

List<string> fruits=new arraylist<> () fruits.add ("Apple"); Fruits.add ("banana"); Fruits.add ("peach");// For loop traversal listfor (int i = 0; i < fruits.size (); i++) {System.out.println (Fruits.get (i));} String applestring= "Apple",//true or FalseSystem.out.println ("Fruits contains apple:" +fruits.contains (applestring)); if ( Fruits.contains (applestring)) {System.out.println ("I like to eat apples");} else {System.out.println ("I'm not Happy");}

The value of the element is changed (replaced) in 3.list according to the index;

Note. Set (index, Element); and. Add (index, Element); the difference;

String a= "White Dragon Horse", b= "Sand Monk", c= "eight commandments", d= "Tang priest", e= "Wukong"; List<string> people=new arraylist<> ();p Eople.add (a);p Eople.add (b);p Eople.add (c);p eople.set (0, d);//. Set (index, Element),//to place D Tang to the list index 0, replace a white Dragon horse People.add (1, e),//.add (index, Element),//To place e Wukong in the list index 1 position, The original position of the B-sand monk moves back one//enhanced for Loop traversal listfor (String str:people) {System.out.println (str);}

The index of the viewing (judging) element in 4.list; 

Note: The difference between indexOf (); and lastIndexOf ();

List<string> names=new arraylist<> () names.add ("Liu Bei");//Index is 0names.add ("Guan Yu");//Index is 1names.add ("Zhang Fei");// The index is 2names.add ("Liu Bei");//Index is 3names.add ("Zhang Fei");//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. Judging by the index position of the 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 Is There");} else {System.out.println ("Where is Liu Bei?") ");}

6. Regenerate a new list (intercept collection) using the index position in the list;

Methods:. Sublist (FromIndex, Toindex);. Size (); The method gets the number of elements in the list and

List<string> phone=new arraylist<> ();p hone.add ("Samsung");//Index for 0phone.add ("Apple");//Index for 1phone.add ("Hammer");// Index is 2phone.add ("Huawei");//Index is 3phone.add ("millet");//index is 4//the original list is traversed for (String Pho:phone) {System.out.println (pho);} Generate new Listphone=phone.sublist (1, 4);//.sublist (FromIndex, Toindex)//Use the object of index 1-4 to regenerate a list, but not the element with index 4, 4-1=3for ( int i = 0; I < phone.size (); i++) {//Phone.size () The method gets the number of elements in the list and the System.out.println ("The new list contains the element is" +phone.get (i));}

7. Compare all the elements in the two list;

The Equals method of two equal objects must be true, but two hashcode equal objects are not necessarily equal objects

1.
if (person.equals (fruits)) {System.out.println ("All elements in two list are the same");} else {System.out.println ("all elements in the two list are not the same");} 2.if (Person.hashcode () ==fruits.hashcode ()) {System.out.println ("We are the same");} else {System.out.println ("We are not the same");}

8. Determine if the list is empty;

NULL returns TRUE, non-null returns false

if (Person.isempty ()) {System.out.println ("Empty");} else {System.out.println ("not Empty");}

9. Returns the Iterator collection object;

System.out.println ("Return iterator Collection object:" +person.iterator ());

1+0. Converting a collection to a string;

String listring= ""; listring=person.tostring (); System.out.println ("Converts the collection to a string:" +listring);

11. Convert the set to an array;

System.out.println ("Convert collection to 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. Specified type list<stringbuffer> lst=new arraylist<> (); for (string String:person) {Lst.add (StringBuffer (String));}

Complete code is included:

Package Mytest01;import Java.util.arraylist;import Java.util.list;public class ListTest01 {public static void main ( String[] args) {//list Add, get, delete element list<string> person=new arraylist<> ();p Erson.add ("Jackie");//index is 0//. Add (E) Person.add ("Peter");//Index is 1person.add ("Annie");//Index is 2person.add ("Martin");//Index is 3person.add ("marry");// The index is 4person.remove (3),//.remove (index) person.remove ("Marry"),//.remove (Object o) String per= "";p er=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)}// The list always contains an element list<string> fruits=new arraylist<> () fruits.add ("Apple"); Fruits.add ("banana"); Fruits.add ("Peach") );//for loop traversal listfor (int i = 0; i < fruits.size (); i++) {System.out.println (Fruits.get (i));} String applestring= "Apple",//true or FalseSystem.out.println ("Fruits contains apple:" +fruits.contains (applestring)); if ( Fruits.contains (applestring)) {System.out.println ("I like to eat apples");} else {System.out.println ("I'm not Happy");} The list is based on the cableChange the element value (replace) String a= "White Dragon Horse", b= "Sand Monk", c= "eight commandments", d= "Tang priest", e= "Wukong"; List<string> people=new arraylist<> ();p Eople.add (a);p Eople.add (b);p Eople.add (c);p eople.set (0, d);//. Set (index, Element)//position D Tang to list index 0, replace a white Dragon horse People.add (1, e),//.add (index, Element),//Put e Wukong into the list index 1 position, The original position of the B-sand monk moves back one//enhanced for Loop traversal listfor (String str:people) {System.out.println (str);} The index of the view (judging) element in list list<string> names=new arraylist<> (); Names.add ("Liu Bei");//Index is 0names.add ("Guan Yu");// The index is 1names.add ("Zhang Fei");//Index is 2names.add ("Liu Bei");//Index is 3names.add ("Zhang Fei");//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"));//Depending on the index position of the element if (Names.indexof ("Liu Bei") ==0) {System.out.println (" Liu Bei is Here ");} else if (Names.lastindexof ("Liu Bei") ==3) {System.out.println ("Liu Bei Is There");} else {System.out.println ("Where is Liu Bei?") ");} Regenerate a new list (intercept collection) using the index position in the list list<string> phone=new arraylist<> ();p hone.add ("Samsung");//index is 0phone.add ( "Apple");//IndexIs 1phone.add ("Hammer");//Index is 2phone.add ("Huawei"),//Index is 3phone.add ("millet"),//index is 4//the original list to traverse for (String Pho:phone) { System.out.println (pho);} Generate new Listphone=phone.sublist (1, 4);//.sublist (FromIndex, Toindex)//Use the object of index 1-4 to regenerate a list, but not the element with index 4, 4-1=3for ( int i = 0; I < phone.size (); i++) {//Phone.size () The method gets the number of elements in the list and the System.out.println ("The new list contains the element is" +phone.get (i));} Compare the Equals method of all elements in two list//two equal objects must be true, but two hashcode equal objects are not necessarily equal objects if (person.equals (fruits)) {System.out.println ("All elements in the two list are the same");} else {System.out.println ("all elements in the two list are not the same");} if (Person.hashcode () ==fruits.hashcode ()) {System.out.println ("We are the same");} else {System.out.println ("We are not the same");} Returns true if the list is empty//empty, and returns Falseif (Person.isempty ()) {System.out.println ("empty") if not null;} else {System.out.println ("not Empty");} Returns the Iterator collection object System.out.println ("Returns iterator Collection object:" +person.iterator ());//Converts the collection to string listring= ""; listring= Person.tostring (); System.out.println ("Convert collection to String:" +listring);//Convert collection to an array, default type System.out.println ("Convert collection to array:" +person.toarray ());//// Turn the collectionChange to the specified type (friendly handling)//1. Default Type list<object> listsstrings=new arraylist<> (); for (int i = 0; i < person.size (); i++) { Listsstrings.add (Person.get (i)); 2. Specify the type list<stringbuffer> lst=new arraylist<> (); for (String String:person) {Lst.add (StringBuffer ( string));}} private static StringBuffer StringBuffer (String string) {return null;}}

Common operations for list collections in Java

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.