1. Add "a", "a", "C", "C", "a" 5 elements to the set set and the list collection respectively, and observe whether the duplicate value "a" can be added successfully in the list collection and the set set.
1 Packageorg.hanqi.practise;2 ImportJava.util.*;3 Public classTest2 {4 5 Public Static voidMain (string[] args) {6 7set<string> s =NewHashset<string>();8S.add ("A");9S.add ("a");TenS.add ("C"); OneS.add ("C"); AS.add ("a"); - if(S.add ("a")) - { theSystem.out.println ("Add a successfully"); - } - Else - { +System.out.println ("Add a Failed"); - } + for(String t:s) A { atSystem.out.print ("" +t); - } - System.out.println (); -System.out.println ("s length =" +s.size ()); - -list<string> L =NewArraylist<string>(); inL.add ("A"); -L.add ("a"); toL.add ("C"); +L.add ("C"); -L.add ("a"); the for(String t:l) * { $System.out.print ("" +t);Panax Notoginseng } - System.out.println (); theSystem.out.println ("s length =" +l.size ()); + } A}
The result of the operation is:
Conclusion: The duplicate value "a" can be added successfully in the list collection, not in the set collection.
2. Create a Map collection, create an EMP object, and add the created EMP object to the collection (the ID of the EMP object as the key to the Map collection) and remove the object with ID 005 from the collection.
To create an EMP class:
1 Packageorg.hanqi.practise;2 3 Public classEMP {4 5 PrivateString ID;6 PrivateString name;7 PublicString getId () {8 returnID;9 }Ten Public voidsetId (String id) { One This. ID =ID; A } - PublicString GetName () { - returnname; the } - Public voidsetName (String name) { - This. Name =name; - } + PublicEMP (string ID, string name) { - Super(); + This. ID =ID; A This. Name =name; at } -}
To create the Maptest class:
1 Packageorg.hanqi.practise;2 3 ImportJava.util.*;4 5 Public classMaptest {6 7 Public Static voidMain (string[] args) {8 9Map<string,string>m =NewHashmap<string,string>();TenEmp e =NewEMP ("005", "Zhang San"); OneEMP e1 =NewEMP ("9527", "Huaan"); AEMP e2 =NewEMP ("4927", "Liu Jianming"); -EMP E3 =NewEMP ("27149", "Yan"); - M.put (E.getid (), E.getname ()); the M.put (E1.getid (), E1.getname ()); - M.put (E2.getid (), E2.getname ()); - M.put (E3.getid (), E3.getname ()); - for(String t:m.keyset ()) + { -System.out.println (t+ "" +M.get (t)); + } A atSystem.out.println ("The collection after removing the number 005 object is:"); -M.remove ("005"); -Set s =M.keyset (); -Iterator<string>it =s.iterator (); - while(It.hasnext ()) - { inString str =It.next (); -String name =m.get (str); toSystem.out.println (str+ "" +name); + } - } the}
The result of the operation is:
3. Lottery program
1 Packageorg.hanqi.practise;2 3 ImportJava.util.*;4 5 Public classTest3 {6 7 Public Static voidMain (string[] args) {8 9Random r =NewRandom ();Tenset<integer> s =NewHashset<integer>(); One while(S.size () < 10) A { - inti = R.nextint (20); - if(!s.contains (i)) the { - S.add (i); - } - } +System.out.println ("Randomly draw 10 numbers from 20:"); - for(Integer t:s) + { ASystem.out.print ("" +t); at } - } -}
The result of the operation is:
P235 Combat Practice (Collection Class 2) and the draw program