Java generics drill-down set useful tools various sets generics drill-down using demo sample, Anonymous inner class, inner class applied to generic discussion

Source: Internet
Author: User
Tags lenovo

Java generics drill-down set useful tools various sets generics drill-down using demo sample, Anonymous inner class, inner class applied to generic discussion

Sets.javapackage org.rui.generics.set;import Java.util.hashset;import java.util.set;/** * A set useful tool * @author Lenovo * */public class Sets {public static<t> set<t> Union (set<t> a,set<t> B) {set<t> result=new H Ashset<t> (a); Result.addall (b); return result;} Keep same public static <T> set<t> intersection (set<t> a,set<t> b) {set<t> result=new HashSet <T> (a); Result.retainall (b); return result;} Difference//Remove same public static <T> set<t> difference (set<t> a,set<t> b) {set<t> result=new Hashset<t> (a); Result.removeall (b); return result;} All over-Jewish elements except intersections public static <T> set<t> complement (set<t> a,set<t> b) {return Difference (union (A, B), intersection (A, b));} public static void Main (string[] args) {set<string> result=new hashset<string> (); Result.add ("a"); Result.add ("B"); Set<string> result2=new hashset<string> (); Result2.add ("B"); Set<string> Results=complemENT (RESULT,RESULT2); for (String s:results) {System.out.println (s);}}}  

Watercolors.javapackage org.rui.generics.set;//Watercolor Public enum Watercolors {ZINC,LEMON_TYLLOW,MEDIUM_YELLOW,DEEP_ Yellow,orange,brilliant_red}

Watercolorsets.javapackage org.rui.generics.set;import java.util.enumset;import java.util.Set;/** * EnumSet use Demo sample * @author Lenovo * */public class Watercolorsets {public static void main (string[] args) {set<watercolors> set1=enum Set.range (Watercolors.lemon_tyllow,watercolors.orange); Set<watercolors> Set2=enumset.range (Watercolors.zinc,watercolors.medium_yellow); System.out.println (SET1); System.out.println (Set2);//UNIONSYSTEM.OUT.PRINTLN ("Union 1 2:" +sets.union (Set1, Set2));// IntersectionSystem.out.println ("Intersection:" +sets.intersection (Set1, Set2));//difference Difference System.out.println ( "Difference1:" +sets.difference (Set1, Set2)); System.out.println ("Difference2:" +sets.difference (Set2, Set1));//complement complements System.out.println ("complement:" + Sets.complement (Set1, Set2));}}

Watercolors.javapackage org.rui.generics.set;import Java.lang.reflect.method;import java.util.*;/** * We can see the results of various relational operations from the output * * @author Lenovo * */public class Containermethoddifferences {static set<string> Methodset (Cl ass<?> type) {set<string> result =new treeset<string> (); for (Method m:type.getmethods ()) Result.add ( M.getname ()); return result;} static void Interfaces (Class<?> type) {System.out.println ("Interfaces in:" +type.getsimplename ()); List<string> result=new arraylist<string> (); for (class<?> c:type.getinterfaces ()) Result.add ( C.getsimplename ()); SYSTEM.OUT.PRINTLN ("Result:" +result);} Static set<string> Object=methodset (Object.class); Static{object.add ("clone");} differencestatic void Difference (class<?> superset,class<?> subset) {SYSTEM.OUT.PRINTLN ( Superset.getsimplename () + "extends:" +subset.getsimplename ()); Set<string> comp=sets.difference (Methodset (superset), Methodset (subset)); Comp.removeall (object); System.out.prinTLN ("object:" +comp); interfaces (superset);} Mianpublic static void Main (string[] args) {/*system.out.println ("collection:" +methodset (Collection.class)); Interfaces (Collections.class) */system.out.println ("----Set-----------------------------");d ifference ( Set.class,collections.class); SYSTEM.OUT.PRINTLN ("----HashSet-----------------------------");d ifference (Hashset.class,set.class); SYSTEM.OUT.PRINTLN ("----linkedhashset-----------------------------");d ifference (Linkedhashset.class, Hashset.class); SYSTEM.OUT.PRINTLN ("----TreeSet-----------------------------");d ifference (Treeset.class,set.class); SYSTEM.OUT.PRINTLN ("-----List----------------------------");d ifference (List.class,collection.class); SYSTEM.OUT.PRINTLN ("------ArrayList---------------------------");d ifference (Arraylist.class,list.class); SYSTEM.OUT.PRINTLN ("------linkedlist---------------------------");d ifference (Linkedlist.class,list.class); SYSTEM.OUT.PRINTLN ("------Queue---------------------------");d ifference (queue.class,collection. Class); SYSTEM.OUT.PRINTLN ("------priorityqueue---------------------------");d ifference (Priorityqueue.class, Queue.class); System.out.println ("Map:" +methodset (Map.class)); SYSTEM.OUT.PRINTLN ("------HashMap---------------------------");d ifference (Hashmap.class,map.class); SYSTEM.OUT.PRINTLN ("------linkedhashmap---------------------------");d ifference (Linkedhashmap.class, Hashmap.class); SYSTEM.OUT.PRINTLN ("------TreeMap---------------------------");d ifference (Treemap.class,map.class);// Category System.out.println ("------sortedmap---------------------------");d ifference (Sortedmap.class,map.class);}} /*output:----Set-----------------------------set Extends:collectionsobject:[toarray, iterator, remove, containsall , contains, add, size, clear, IsEmpty, Retainall, removeall]interfaces in:setresult:[collection]---- HashSet-----------------------------HashSet extends:setobject:[]interfaces in:hashsetresult:[set, cloneable, Serializable]----Linkedhashset-----------------------------linkedhashset EXTENDS:HASHSEtobject:[]interfaces In:linkedhashsetresult:[set, cloneable, Serializable]---- TreeSet-----------------------------TreeSet Extends:setobject:[lower, last, higher, descendingiterator, subset, Polllast, Comparator, pollfirst, floor, headSet, ceiling, Tailset, first, descendingset]interfaces in:treesetresult:[ Navigableset, cloneable, Serializable]-----list----------------------------list Extends:collectionobject:[get, set , Listiterator, LastIndexOf, IndexOf, sublist]interfaces in:listresult:[collection]------ ArrayList---------------------------ArrayList extends:listobject:[trimtosize, ensurecapacity]interfaces in: Arraylistresult:[list, Randomaccess, cloneable, Serializable]------ LinkedList---------------------------LinkedList Extends:listobject:[offerfirst, removefirstoccurrence, Pop, Peeklast, Push, Descendingiterator, poll, Peek, Removefirst, Polllast, GetFirst, offerlast, Element, Removelast, offer, PO Llfirst, AddLast, AddFirst, Peekfirst, GetLast, Removelastoccurrence]interfaces In:liNkedlistresult:[list, Deque, cloneable, Serializable]------Queue---------------------------Queue extends: Collectionobject:[element, offer, poll, Peek]interfaces In:queueresult:[collection]------ Priorityqueue---------------------------Priorityqueue extends:queueobject:[comparator]interfaces in: Priorityqueueresult:[serializable]map:[clear, ContainsKey, Containsvalue, EntrySet, equals, GET, HashCode, IsEmpty, KeySet, PUT, Putall, remove, size, values]------HashMap---------------------------HashMap extends:mapobject:[] Interfaces In:hashmapresult:[map, cloneable, Serializable]------ Linkedhashmap---------------------------linkedhashmap extends:hashmapobject:[]interfaces in:linkedhashmapresult: [Map]------TreeMap---------------------------TreeMap extends:mapobject:[polllastentry, Firstkey, Floorentry, Ceilingentry, Lowerentry, Lastentry, SubMap, Tailmap, Navigablekeyset, Higherentry, Lowerkey, Headmap, FirstEntry, Comparator, Descendingkeyset, Descendingmap, Pollfirstentry, Lastkey, Higherkey, Floorkey, Ceilingkey]interfaces In:treemapresult:[navigablemap, cloneable, Serializable]------ SortedMap---------------------------SortedMap Extends:mapobject:[tailmap, Firstkey, Headmap, Comparator, Lastkey, Submap]interfaces in:sortedmapresult:[map]*/

--------------------------------------------------------------------------------------------------------------- ------------------------


Generator.javapackage Org.rui.generics.anonymity;public interface Generator<t> {//returns the inner object of the generic type    T next ();

Generators.javapackage org.rui.generics.anonymity;import java.util.collection;/** * Easily populate a Collection with the generator * @ Author Lenovo * */public class Generators {public  static<t> collection<t> Fill (collection<t> coll ,generator<t> gen,int N)  {  System.out.println ("Gen.next ():" +gen.next ());  for (int i=0;i<n;i++)  {  coll.add (Gen.next ());  } return coll;}  }

Bankteller.javapackage Org.rui.generics.anonymity;import Java.util.arraylist;import Java.util.LinkedList;import Java.util.list;import java.util.queue;import java.util.random;/** * Anonymous inner class inner classes are used for generics * Generator are born into the static, Therefore, they cannot be used as part of the interface, because they cannot be generalized by a particular usage method such as interfaces. * Even so, they work very well in the fill () method * * @author Lenovo * */class customer{private Static long counter=1;private final long Id=cou nter++;p rivate Customer () {}public String toString () {return "Customer:" +ID;} Each time a new client object is created public static generator<customer> Generator () {return new generator<customer> () {public Customer Next () {return new Customer ();}};}} Cashier class Teller {private static long counter=1; private final long id=counter++; private Teller () {} public String t  Ostring () {return "Teller" +ID;} Teller just creates a public Generator object public static generator<teller> generator=new generator<teller> () {  Public Teller Next () {return new Teller ();} }; }//////////////Teller public class Bankteller {public static VOID Serve (Teller T,customer c) {System.out.println (t+ "serves" +c);} public static void Main (string[] args) {random random=new random (47);//Generate Customer Object 15 queue<customer> line=new Linkedli St<customer> (); Generators.fill (line, Customer.generator (), 15);//Teller object 4 x list<teller> tellers=new arraylist<teller> (); Generators.fill (Tellers, teller.generator, 4); for (Customer C:line) Serve (Tellers.get (Random.nextint (Tellers.size ()  )), c); }}/*output:teller3 serves Customer1teller2 serves Customer2teller3 serves Customer3teller1 serves Customer4Teller1 Serves Customer5teller3 serves Customer6teller1 serves Customer7teller2 serves Customer8teller3 serves Customer9Teller3 Serves Customer10teller2 serves Customer11teller4 serves Customer12teller2 serves Customer13Teller1 serves Customer14teller1 serves Customer15 * *///:~~~


Related Article

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.