Welcome to visit the blog Base Code List interface
Import Java.util.Iterator;
Import java.util.LinkedList;
Import java.util.List;
public class Collectiondemo {public
static void Main (string[] args) {
String a= "A", b= "B", c= "C", d= "D", e= "E";
list<string> List = new linkedlist<string> ();
List.add (a);
List.add (e);
List.add (d);
iterator<string> firstiterator = List.iterator ();
System.out.println ("The elements in the set before the modification are:");
while (Firstiterator.hasnext ()) {
System.out.print (Firstiterator.next () + " ");
}
List.set (1,b);
List.set (2,c);
Iterator<string> it = List.iterator ();
System.out.println ();
System.out.println ("The elements in the modified set are:");
while (It.hasnext ()) {
System.out.print (It.next () + " ");
}
}
}
List Index
Import java.util.ArrayList;
Import java.util.LinkedList;
Import java.util.List;
public class Collectiondemo {public
static void Main (string[] args) {
String a= "A", b= "B", c= "C", d= "D", apple= "a Pple ";
list<string> list = new arraylist<string> ();
List.add (a);
List.add (apple);
List.add (b);
List.add (apple);
List.add (c);
List.add (apple);
List.add (d);
SYSTEM.OUT.PRINTLN (list);
System.out.println ("Apple's first occurrence of index position is:" + list.indexof (apple));
System.out.println ("Apple's last occurrence index position is:" + list.lastindexof (apple));
System.out.println ("B" the index position to appear for the first time is: "+ list.indexof (b));
System.out.println ("B Last occurrence of index position is:" + list.lastindexof (b));
}
Set interface
Using collections to remove duplicates
Import java.util.ArrayList;
Import Java.util.HashSet;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Set;
public class Collectiondemo {public
static void Main (string[] args) {
list<string> List = new arraylist< String> ();
List.add ("Apple");
List.add ("pear");
List.add ("banana");
List.add ("Apple");
set<string> set = new Hashset<string> ();
Set.addall (list);
Iterator<string> it = Set.iterator ();
System.out.println ("The elements in the collection are:");
while (It.hasnext ()) {
System.out.print (It.next () + " ");
}
}
}
Map interface
Import Java.util.HashMap;
Import Java.util.Map;
public class Collectiondemo {public
static void Main (string[] args) {
map<string, string> Map = new HashMap <string, string> ();
Map.put ("1", "Apple");
Map.put ("2", "pear");
Map.put ("3", "orange");
for (int i=1; i<=map.size (); i++) {
System.out.println ("+i+" element is: "+map.get (" "+i+"));}}
Implementation class for List interface
Import java.util.ArrayList;
Import java.util.List;
public class Test {public
static void Main (string[] args) {
list<string> List = new arraylist<string> ();
int i = (int) (Math.random () * (List.size ()-1));
List.add ("a");
List.add ("B");
List.add ("C");
SYSTEM.OUT.PRINTLN ("Randomly acquiring elements in an array:" + list.get (i));
List.remove (2);
System.out.println (the element in the array is: "After the element that has the index is ' 2" is removed from the array);
for (int j=0; j<list.size (); j + +) {
System.out.print (List.get (j) + "");}}}
Implementation classes for SET interfaces
Import Java.util.HashSet;
Import Java.util.Iterator;
Import Java.util.Set; public class Collectiondemo {public static void main (string[] args) {set<people> hashset = new HashSet
<People> ();
Hashset.add (New people ("Chen classmate", 201101));
Hashset.add (New people ("Wang classmate", 201102));
Hashset.add (New people ("classmate Li", 201103));
Iterator<people> it = Hashset.iterator ();
System.out.println ("The elements in the collection are:");
while (It.hasnext ()) {People person = it.next ();
System.out.println (Person.getname () + "" + Person.getid_card ());
}} class People {private String name;
Private long Id_card;
Public people (String name, long id_card) {this.name = name;
This.id_card = Id_card;
Public long Getid_card () {return id_card;
} public void Setid_card (long id_card) {this.id_card = Id_card;
Public String GetName () {return name;
} public void SetName (String name) {this.name = name; }
}
Instance-Traversal of ArrayList using a for loop
Import java.util.ArrayList;
Import java.util.List;
public class Test {public
static void Main (string[] args) {
list<integer> List = new ARRAYLIST<INTEGER&G t; ();
for (int i=0; i<10; i++) {
list.add (i);
}
System.out.println ("Elements in the list:" + lists);
System.out.println ("Odd ordinal element in the list:");
for (int i=1; i<list.size (); I +=2) {
System.out.print (list.get (i) + " ");
}
}
The creation and use of iterators
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.ListIterator; public class Test {public static void main (string[] args) {arraylist<integer> array = new ARRAYLIST<
;integer> ();
add element Collections.addall (array, 1,2,3,4,5,6) to the collection using the tool method in the Collections class AddAll ();
System.out.println ("Elements in the collection:" + array);
The Listiterator object listiterator<integer> iterator = Array.listiterator () is obtained by using the method without parameters.
For the initial position, decide whether to have the next element boolean hasnext = Iterator.hasnext ();
System.out.println ("Whether the collection has the next element:" + Hasnext);
For the initial position, determine if there is a previous element Boolean hasprevious = Iterator.hasprevious ();
System.out.println ("Whether the collection has the previous element:" + hasprevious);
int next = Iterator.next ();
System.out.println ("Get the next element of the collection:" + next);
int nextindex = Iterator.nextindex ();
System.out.println ("Gets the index of the next element of the collection:" + Nextindex);
int previous = Iterator.previous (); System.out.prinTLN ("Get the first element of a set:" + previous);
int previousindex = Iterator.previousindex ();
System.out.println ("Gets the index of the previous element of the collection:" + Previousindex);
Iterator.add (7);
System.out.println ("Add the set after element 7 to the set:" + array);
Iterator.next ();
Iterator.set (12);
System.out.println ("The next element to be obtained is modified to the set after 12:" + array);
Iterator.remove ();
System.out.println ("will get the next element removed after the collection:" + array); }
}
Instance-iterator traversal ArrayList
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
public class Test {public
static void Main (string[] args) {
list<integer> List = new ARRAYLIST<INTEGER&G t; ();
for (int i=0; i<10; i++) {
list.add (i);
}
System.out.println ("All elements in the list:");
for (iterator<integer> it = List.iterator (); It.hasnext ();) {
System.out.print (It.next () + "");}}
Instance-Listiterator traversal ArrayList
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class Test {public static void main (string[] args) {list<integer> List = new Arraylist<integ
Er> ();
for (int i=0; i<10; i++) {list.add (i);
} System.out.println ("All elements in the list are:" + list);
SYSTEM.OUT.PRINTLN ("the element in reverse output list:");
Listiterator<integer> li = List.listiterator ();
For (Li=list.listiterator (); Li.hasnext ();) li.next ();
for (; li.hasprevious ();)
System.out.print (li.previous () + ""); }
}