Common operation of List collection in Java _java

Source: Internet
Author: User
Tags stringbuffer

Directory:

1.list to add, get, delete elements;

Whether an element is contained in the 2.list;

In 3.list, the element values are changed (replaced) according to the index;

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

5. Judging according to the position of element index;

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

7. Compare all the elements in the two list;

8. Judge whether the list is empty;

9. Returns the Iterator collection object;

10. Convert the collection to a string;

11. Convert the collection to an array;

12. Set type conversion;

Note: The code in the content is related.

1.list to add, get, delete elements;

Add the method:. Add (e);

The Fetch method is:. Get (index);

The removal method is:. Remove (index);

Delete by index;. Remove (Object o);

Deleted according to element content;

List<string> person=new arraylist<> (); 
      Person.add ("Jackie");  The index is 0//.add (e) 
      Person.add ("Peter");  Index is 1 
      person.add ("Annie");  The index is 2 
      person.add ("Martin");  The index is 3 
      person.add ("Marry");  The index is 4 
       
      person.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 an element is contained in the 2.list;

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

List<string> fruits=new arraylist<> (); 
      Fruits.add ("Apple"); 
      Fruits.add ("banana"); 
      Fruits.add ("Peach"); 
      For loop traversal list for 
      (int i = 0; i < fruits.size (); i++) { 
        System.out.println (Fruits.get (i)); 
      } 
      String applestring= "Apple"; 
      True or False 
      System.out.println ("Fruits contains apple:" +fruits.contains (applestring)); 
       
      if (Fruits.contains (applestring)) { 
        System.out.println ("I Like eating apples"); 
      else { 
        System.out.println ("I'm not happy"); 
      }

In 3.list, the element values are changed (replaced) according to the index;

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

String a= "White Dragon Horse", b= "Sand Monk", c= "eight precepts", d= "Tang's Monk", e= "Wu empty"; 
      List<string> people=new arraylist<> (); 
      People.add (a); 
      People.add (b); 
      People.add (c); 
      People.set (0, D);  . Set (index, Element);   Place d Tang to index 0 in the list, replacing a white Dragon horse 
      people.add (1, e);  . Add (index, Element);   Put e Wukong in the list of the index of 1, the original location of the B-Sha Monk after moving a 
       
      //enhanced for loop traversal list for 
      (String str:people) { 
        System.out.println ( STR); 
      } 

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

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

List<string> names=new arraylist<> (); 
      Names.add ("Liu Bei");  Index of 0 
      names.add ("Guan Yu");  The index is 1 
      names.add ("Zhang Fei");  The index is 2 
      names.add ("Liu Bei");  The index is 3 
      names.add ("Zhang Fei");  The index is 4 
      System.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 according to the position of element index;

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?") "); 
} 

6. Regenerate a new list (intercepting 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<> (); 
      Phone.add ("Samsung");  Index is 0 
      phone.add ("Apple");  Index 1 
      phone.add ("Hammer");  Index of 2 
      phone.add ("Huawei");  Index of 3 
      phone.add ("millet");  The index is 4 
      //The original list for traversal for 
      (String pho:phone) { 
        System.out.println (pho); 
      } 
      Generate a new list 
      phone=phone.sublist (1, 4);//.sublist (Fromindex, Toindex)   /////To regenerate a list with an object indexed by 1-4, but no elements indexed to 4 , 4-1=3 for 
      (int i = 0; i < phone.size (); i++) {//Phone.size () The method gets the number of elements in the list and 
        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 for two equal objects must be true, but two hashcode equal objects are not necessarily equal objects

1.<br>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 different"); 
} 
2.     
if (Person.hashcode () ==fruits.hashcode ()) { 
  System.out.println ("We are the same"); 
} else { 
  System.out.println ("We are not the same"); 
} 

8. Judge whether the list is empty;

Null returns TRUE, NOT 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. Converts a collection to a string;

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

11. Convert the collection to an array;

System.out.println ("Convert the collection 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. Designated type 
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) {//list Add, get, delete element List<stri 
      Ng> person=new arraylist<> ();  Person.add ("Jackie");  The index is 0//.add (e) person.add ("Peter");  Index is 1 person.add ("Annie");  The index is 2 person.add ("Martin");  The index is 3 person.add ("Marry");  The index is 4 person.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)); The. Get (Index)}//list always contains an element list<string> fruits=new arraylist<> () 
      ; 
      Fruits.add ("Apple"); 
      Fruits.add ("banana"); 
      Fruits.add ("Peach"); For loop traversal list for (int i = 0; I < fruitS.size (); 
      i++) {System.out.println (Fruits.get (i)); 
      String applestring= "Apple"; 
       
      True or False System.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 am not happy"); 
      //list element values in the index (replace) String a= "White Dragon Horse", b= "Sand Monk", c= "eight precepts", d= "Tang's Monk", e= "Wu empty"; 
      List<string> people=new arraylist<> (); 
      People.add (a); 
      People.add (b); 
      People.add (c);  People.set (0, D);  . Set (index, Element)//position D Tang to index 0 in list, replace a white Dragon horse People.add (1, e);   . Add (index, Element); Put e Wukong in the list of the index of 1, the original location of the B-Sha monk after moving A//enhanced for loop traversal list for (String str:people) {System.out.print 
      ln (str); 
      //list the index of the (judging) element in the list<string> names=new arraylist<> ();  Names.add ("Liu Bei");  Index of 0 names.add ("Guan Yu"); 
     Index is 1 Names.add ("Zhang Fei");  The index is 2 names.add ("Liu Bei");  The index is 3 names.add ("Zhang Fei"); 
      The index is 4 System.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")); 
      According to the element index position of the judge 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?") 
      "); 
      /////////Use the index position in list to regenerate a new list (intercept set) list<string> phone=new arraylist<> ();  Phone.add ("Samsung");  Index is 0 phone.add ("Apple");  Index 1 Phone.add ("Hammer");  Index of 2 Phone.add ("Huawei");  Index of 3 Phone.add ("millet"); 
      The index is 4//the original list for traversal for (String Pho:phone) {System.out.println (pho); ///Generate new list phone=phone.sublist (1, 4); . sublist (Fromindex, Toindex)//re-using index 1-4 objectsGenerates a list, but does not contain an element with an index of 4, 4-1=3 for (int i = 0; i < phone.size (); i++) {//Phone.size () This method gets the number of elements in the list and S 
      Ystem.out.println ("The new list contains elements that are" +phone.get (i)); The Equals method for all elements//two equal objects in the two list must be true, but two hashcode equal objects are not necessarily equal objects if (person.equals Frui 
      ts)) {System.out.println ("all elements of the two list are the same"); 
      }else {System.out.println ("all elements of the two list are different"); 
      } if (Person.hashcode () ==fruits.hashcode ()) {System.out.println ("We are the same"); 
      }else {System.out.println ("We are not the same"); ///Determine if list is empty//NULL returns TRUE, Non-empty returns False if (Person.isempty ()) {System.out.prin 
      TLN ("Empty"); 
      }else {System.out.println ("not Empty"); 
       
      //Returns the Iterator collection Object System.out.println ("Return iterator Collection object:" +person.iterator ()); 
      Converts the collection to string listring= ""; 
      Listring=person.tostring (); System.out.println ("Convert a collection to a string:" +listring); 
       
      Converts a collection to an array, the default type System.out.println ("Convert a collection to an array:" +person.toarray ()); 
      Converts the collection to the 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. Specifies the type list<stringbuffer> lst=new arraylist<> (); 
      for (string String:person) {Lst.add (StringBuffer (string)); 
  private static StringBuffer StringBuffer (String string) {return null; } 
 
 
  }

Above this Java in the list of common operation of the collection is a small set to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.