Java class set framework simple comprehension of List, set, Iterator, map
1. Java class set framework body structure:
(1). Collection (SET): objects are not sorted by a particular time, and there are no duplicate objects.
(2). List: objects are sorted by index location and can have duplicate objects.
(3). Map: Each element contains a key object and a value object, the key cannot be duplicated, and the value can be repeated.
2. The collection interface provides the following methods:
3. The code is as follows:
ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set; Public class testdemo { Public Static void Main(string[] args) {Listdemo (); Setdemo (); Iteratordemo (); Mapdemo (); }/** * Map test: Key cannot be duplicated, value can be repeated, in the form of a key-value pair */ Private Static void Mapdemo() {map<string, string> Map =NewHashmap<string, string> (); Map.put ("1","a"); Map.put ("2","B"); Map.put ("3","C"); Map.put ("4","D"); Map.put ("5","a"); System.out.println ("---map-----"+ map); }/** * can have duplicate elements and are ordered * / Public Static void Listdemo() {//Create the objectArraylist<string> Listdeno =NewArraylist<string> ();/ * * Add the data * *Listdeno.add ("1"); Listdeno.add ("2"); Listdeno.add ("3"); Listdeno.add ("4");//can add repeating elementsListdeno.add ("1"); Listdeno.add ("4"); System.out.println ("---list---"+ Listdeno);//---list---[1, 2, 3, 4, 1, 4]}/** * cannot have duplicate elements and is unordered */ Public Static void Setdemo() {//Create the objectSet<string> Setdemo =NewHashset<string> ();/ * * Add the data * *Setdemo.add ("a"); Setdemo.add ("B"); Setdemo.add ("C"); Setdemo.add ("D");//can add repeating elementsSetdemo.add ("a"); Setdemo.add ("D"); System.out.println ("---setdemo---"+ Setdemo);//---setdemo---[A, B, C, d]} Public Static void Iteratordemo() {//Create the objectSet<string> Setdemo =NewHashset<string> ();/ * * Add the data * *Setdemo.add ("a"); Setdemo.add ("B"); Setdemo.add ("C"); Setdemo.add ("D");//can add repeating elementsSetdemo.add ("a"); Setdemo.add ("D"); System.out.println ("---setdemo---"+ Setdemo);//---setdemo---[A, B, C, d] //iterator used with collectionsiterator<string> Iterator = Setdemo.iterator (); while(Iterator.hasnext ()) {String element = (string) iterator.next (); System.out.println ("---element----"+ Element);//---element----a //---element----b //---element----c //---element----d} }}
Java class set framework simple comprehension of List, set, Iterator, map