Java generic deep-Dive: Set utility examples of various Set generics deep-dive, applying anonymous internal classes and internal classes to generics

Source: Internet
Author: User
Tags lenovo

Java generic deep-Dive: Set utility examples of various Set generics deep-dive, applying anonymous internal classes and internal classes to generics

// Sets. javapackage org. rui. generics. set; import java. util. hashSet; import java. util. set;/*** a Set utility * @ author lenovo **/public class Sets {public static
 
  
Set
  
   
Union (Set
   
    
A, Set
    
     
B) {Set
     
      
Result = new HashSet
      
        (A); result. addAll (B); return result;} // keep the same public static
       
         Set
        
          Intersection (Set
         
           A, Set
          
            B) {Set
           
             Result = new HashSet
            
              (A); result. retainAll (B); return result;} // difference // remove the same public static
             
               Set
              
                Difference (Set
               
                 A, Set
                
                  B) {Set
                 
                   Result = new HashSet
                  
                    (A); result. removeAll (B); return result;} // all the elements except the intersection, except the public static
                   
                     Set
                    
                      Complement (Set
                     
                       A, Set
                      
                        B) {return difference (union (a, B), intersection (a, B);} public static void main (String [] args) {Set
                       
                         Result = new HashSet
                        
                          (); Result. add ("a"); result. add ("B"); Set
                         
                           Result2 = new HashSet
                          
                            (); Result2.add ("B"); Set
                           
                             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 usage example * @ author lenovo **/public class WatercolorSets {public static void main (String [] args) {Set
 
  
Set1 = EnumSet. range (Watercolors. LEMON_TYLLOW, Watercolors. ORANGE); Set
  
   
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 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
 
  
MethodSet (Class
  Type) {Set
  
   
Result = new TreeSet
   
    
(); For (Method m: type. getMethods () result. add (m. getName (); return result;} static void interfaces (Class
    Type) {System. out. println ("interfaces in:" + type. getSimpleName (); List
    
     
Result = new ArrayList
     
      
(); For (Class
      C: type. getInterfaces () result. add (c. getSimpleName (); System. out. println ("result:" + result);} static Set
      
        Object = methodSet (Object. class); static {object. add ("clone");} // differencestatic void difference (Class
       Superset, Class
       Subset) {System. out. println (superset. getSimpleName () + "extends:" + subset. getSimpleName (); Set
       
         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 -----------------------------"); difference (Set. class, Collections. class); System. out. println ("---- HashSet -----------------------------"); difference (HashSet. class, Set. class); System. out. println ("---- LinkedHashSet -----------------------------"); difference (LinkedHashSet. class, HashSet. class); System. out. println ("---- TreeSet -----------------------------"); difference (TreeSet. class, Set. class); System. out. println ("----- List ----------------------------"); difference (List. class, Collection. class); System. out. println ("------ ArrayList ---------------------------"); difference (ArrayList. class, List. class); System. out. println ("------ partition list ---------------------------"); difference (partition list. class, List. class); System. out. println ("------ Queue ---------------------------"); difference (Queue. class, Collection. class); System. out. println ("------ PriorityQueue -------------------------"); difference (PriorityQueue. class, Queue. class); System. out. println ("Map:" + methodSet (Map. class); System. out. println ("------ HashMap ---------------------------"); difference (HashMap. class, Map. class); System. out. println ("------ LinkedHashMap ---------------------------"); difference (LinkedHashMap. class, HashMap. class); System. out. println ("------ TreeMap ---------------------------"); difference (TreeMap. class, Map. class); // classification System. out. println ("------ SortedMap ---------------------------"); difference (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 extends: Setobject: [] interfaces in: HashSetresult: [Set, Cloneable, Serializable] ---- your HashSet extends: HashSetobject: [] interfaces in: LinkedHashSetresult: [Set, Cloneable, Serializable] ---- TreeSet inclutreeset extends: Setobject: [lower, last, higher, descendingIterator, subSet, pollLast, comparator, pollFirst, floor, headSet, ceiling, tailSet, first, descendingSet] interfaces in: TreeSetresult: [NavigableSet, Cloneable, Serializable] ----- List Functions List extends: Collectionobject: [get, set, listIterator, lastIndexOf, indexOf, subList] interfaces in: Listresult: [Collection] ------ ArrayList extends: Listobject: [trimToSize, attributes] interfaces in: attributes: [List, RandomAccess, Cloneable, serializable] ------ inclulist includedlist extends: Listobject: [offerFirst, offline, pop, peekLast, push, descendingIterator, poll, peek, removeFirst, pollLast, getFirst, offerLast, element, removeLast, offer, pollFirst, addLast, addFirst, peekFirst, getLast, Response] interfaces in: inclulistresult: [List, Deque, Cloneable, Serializable] ------ Queue incluqueue extends: Collectionobject: [element, offer, poll, peek] interfaces in: Queueresult: [Collection] ------ PriorityQueue extends: Queueobject: [comparator] interfaces in: serialize: [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 extends hashmap extends: HashMapobject: [] interfaces in: LinkedHashMapresult: [Map] ------ TreeMap extends: Mapobject: [pollLastEntry, firstKey, floorEntry, ceilingEntry, lowerEntry, lastEntry, subMap, tailMap, navigableKeySet, higherEntry, lowerKey, headMap, firstEntry, comparator, primary, lastKey, higherKey, floorKey, primary] interfaces in: treeMapresult: [NavigableMap, Cloneable, Serializable] ------ SortedMap extends: Mapobject: [tailMap, firstKey, headMap, comparator, lastKey, subMap] interfaces in: SortedMapresult: [Map] *
       
      
     
    
   
  
 

Certificate ---------------------------------------------------------------------------------------------------------------------------------------


// Generator. javapackage org. rui. generics. anonymity; public interface Generator
 
  
{// Returns the generic internal object T next ();}
 

// Generators. javapackage org. rui. generics. anonymity; import java. util. collection;/*** use the generator to easily fill in a collection * @ author lenovo **/public class Generators {public static
 
  
Collection
  
   
Fill (Collection
   
    
Coll, Generator
    
     
Gen, int n) {System. out. println ("gen. next ():" + gen. next (); for (int I = 0; I
     
      
// BankTeller. javapackage org. rui. generics. anonymity; import java. util. arrayList; import java. util. using list; import java. util. list; import java. util. queue; import java. util. random;/*** internal classes of anonymous internal classes are used for generic * generator to generate static classes, so they cannot be part of interfaces, * This is because it is impossible to generalize the two by using specific usages such as interfaces. * Despite this, they all work well in the fill () method ** @ author lenovo **/class Customer {private static long counter = 1; private final long id = counter ++; private Customer () {} public String toString () {return "Customer:" + id;} // a new Customer object public static Generator is created each time.
       
        
Generator () {return new Generator
        
         
() {Public Customer next () {return new Customer () ;}}/// cashier class Teller {private static long counter = 1; private final long id = counter ++; private Teller () {}public String toString () {return "Teller" + id ;} // Teller creates only one public generator object public static Generator
         
          
Generator = new Generator
          
           
() {Public Teller next () {return new Teller ();}};} //// // the cashier's 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 15 Queue for the customer object
           
            
Line = new queue list
            
              (); Generators. fill (line, Customer. generator (), 15); // four lists of cashier objects
             
               Tellers = new ArrayList
              
                (); Generators. fill (tellers, Teller. generator, 4); for (Customer c: line) serve (tellers. get (random. nextInt (tellers. size (), c) ;}/ * output: Teller3 serves Customer1Teller2 mer2teller3 serves Customer3Teller1 serves Customer4Teller1 serves Customer4Teller1 serves Customer5Teller3 serves Customer6Teller1 serves Customer7Teller2 serves custoserves Customer10Teller2 serves Customer10Teller2 serves Customer10Teller2 serves Customer11T Eller4 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.