Java basic knowledge of JVM-—— collection exercises

Source: Internet
Author: User

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

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.