1. What are the differences between HashSet and HashMap?
Difference: The former is a single row the latter is a double row, that is, HashMap has a key has a value, hashset only key;
Contact: The bottom of the hashset is HashMap, you can refer to HashSet class source, the default construction method is:
Public HashSet () {
Map = new Hashmap<key,object>
}
is hashset only with the HashMap key, instead of his value, the former value can be arbitrarily assigned by the programmer, anyway
2. The collection in the thread concurrency library is inferior
HashMap and HashSet If the CPU fills up when thousands of threads are concurrent, a collection of new technology concurrent libraries is developed in the JAVA5 thread concurrency library, where the concurrentmap can be used. can also be used in the hashmap of the use of synchronous lock synchronized, in fact, Concurrentmap class is written in the original hashmap to rewrite all the methods, but the return value is the original method plus a synchronous lock
3. Synchronizing Collections
14The following is a case application of a class in a thread concurrent library collection, which is more closely associated with the instructions for changing the knowledge point2 PackageCom.java5.thread.newSkill;3 4 Importjava.util.ArrayList;5 Importjava.util.Collection;6 ImportJava.util.Iterator;7 Importjava.util.concurrent.CopyOnWriteArrayList;8 9 Public classCollectionmodifyexceptiontest {Ten One /** A * @paramargs - */ - Public Static voidMain (string[] args) { the - //Copyonwritearraylist is a class in the thread concurrency library collection to avoid concurrency exceptions in HashMap - /* - * Direct use of traditional ArrayList will appear a variety of thread concurrency anomaly, interested can try + */ -collection<user> users =NewCopyonwritearraylist<user>(); + //collection<user> users = new arraylist<user> (); AUsers.add (NewUser ("Yangkai", 21)); atUsers.add (NewUser ("Yang Yi", 20)); -Users.add (NewUser ("dashing", 22)); -Iterator itrusers =users.iterator (); - while(Itrusers.hasnext ()) { -User User =(User) Itrusers.next (); - if("Chic". Equals (User.getname ())) { in users.remove (user); -}Else { to System.out.println (user); + } - } the } * $ }Panax Notoginseng - Helper Class: User class the PackageCom.java5.thread.newSkill; + A Public classUser { the + PrivateString name; - Private intAge ; $User (String name,intAge ) { $ Super(); - This. Name =name; - This. Age =Age ; the } - Wuyi PublicString GetName () { the returnname; - } Wu - Public voidsetName (String name) { About This. Name =name; $ } - - Public intGetage () { - returnAge ; A } + the Public voidSetage (intAge ) { - This. Age =Age ; $ } the the @Override the Public inthashcode () { the Final intPrime = 31; - intresult = 1; inresult = Prime * result +Age ; theresult = Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); the returnresult; About } the @Override the Public Booleanequals (Object obj) { the if( This==obj) + return true; - if(obj = =NULL) the return false;Bayi if(GetClass ()! =Obj.getclass ()) the return false; theUser other =(User) obj; - if(Age! =other.age) - return false; the if(Name = =NULL) { the if(Other.name! =NULL) the return false; the}Else if(!name.equals (other.name)) - return false; the return true; the } the @Override94 PublicString toString () { the return"User [age=" + Age + ", name=" + name + "]"; the } the @Override98 protectedObject Clone ()throwsclonenotsupportedexception { About return Super. Clone (); - }101 102}
Thread advanced application-8-JAVA5 thread concurrent collection application and case analysis of collections tool class