1. Create a set set to hold the data entered by the user
1 Packagetest;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 Importjava.util.ArrayList;7 ImportJava.util.HashMap;8 ImportJava.util.HashSet;9 ImportJava.util.Iterator;Ten ImportJava.util.LinkedHashSet; One Importjava.util.List; A ImportJava.util.Map; - ImportJava.util.Set; - the classA { - Public intcount; - PublicAintcount) { - This. Count =count; + } - + Public Booleanequals (Object obj) { A if( This==obj) { at return true; - } - if(obj! =NULL&& Obj.getclass () = = A.class) { -A r =(A) obj; - returnR.count = = This. Count; - } in return false; - } to + Public inthashcode () { - return This. Count; the } * $ PublicString toString () {Panax Notoginseng return This. count+ ""; - } the + } A Public classtestcollection { the Public Static voidTestset ()throwsException { + //Set set = new Linkedhashset (); - //Set set = new TreeSet (); $Set set =NewHashSet (); $ intKeyin; - intnum = 0; - the while(Num++ < 2) { - //Scanner sc = new Scanner (system.in);Wuyi //keyin = Sc.nextint (); theBufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); -Keyin =Integer.parseint (Br.readline ()); WuSet.add (NewA (Keyin)); - } About System.out.println (set); $ } - - Public Static voidtestlist () { -List List =NewArrayList (); A for(inti=0; i<10; i++) { +List.add (NewA (i)); the } - System.out.println (list); $ } the the Public Static voidTestMap () { thestring[] str =NewString[] {"A", "B", "A", "B", "C", "A", "B", "C"}; themap<string, integer> map =NewHashMap (); -Set<string> set =NewLinkedhashset (); in for(inti = 0; i < str.length; i++) { the Set.add (Str[i]); the } About the the String key; theIterator it =set.iterator (); + while(It.hasnext ()) { - intval = 0; theKey =(String) It.next ();Bayi for(inti = 0; i < str.length; i++) { the if(Str[i] = = key) val++; the } - Map.put (Key, Val); - } the the System.out.println (map); the } the - Public Static voidMain (string[] args)throwsException { the //Testcollection.testset (); the //testcollection.testlist (); the Testcollection.testmap ();94 } the}
Java basic knowledge of JVM-—— collection exercises