Advertisement
m1o2

m1o2 fxp elsf Java .class HashMap count classes

Jul 15th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.Socket;
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11.  
  12. public class Test{
  13.    
  14.     public static void main( String[] args ) throws IOException{
  15.        
  16.         testFoo2();
  17.     }
  18.    
  19.     public static void testFoo2(){
  20.        
  21.         List<Object> list = new LinkedList<>();
  22.        
  23.         list.add( new String("1") );
  24.         list.add( new String("2") );
  25.         list.add( new String("3") );
  26.         list.add( new String("4") );
  27.         list.add( new SimpleDateFormat("dd/MM/yy") );
  28.         list.add( new Socket() );
  29.         list.add( new Integer(777) );
  30.         list.add( new Integer(777) );
  31.         list.add( new Integer(777) );
  32.         list.add( new Thread() );
  33.        
  34.        
  35.         foo2( list.toArray() );
  36.     }
  37.    
  38.  
  39.    
  40.     public static void foo2( Object[] array ){
  41.        
  42.         Map<Class<?>, Integer > map = new HashMap<>();
  43.        
  44.         for( Object o : array ){
  45.            
  46.             Integer exists = map.get( o.getClass() );
  47.            
  48.             if( null == exists )
  49.                 exists = 0;
  50.            
  51.             map.put( o.getClass(), exists + 1 );
  52.         }
  53.        
  54.         for( Entry<Class<?>, Integer> entry : map.entrySet() )
  55.             System.out.println( "Class: " + entry.getKey().getSimpleName() + " appeared " + entry.getValue() + " times.");
  56.     }
  57.    
  58.     public static void testFoo1(){
  59.        
  60.         List<Class<?>> list = new ArrayList<>(10);
  61.        
  62.         list.add( String.class );
  63.         list.add( String.class );
  64.         list.add( String.class );
  65.         list.add( Integer.class );
  66.         list.add( Thread.class );
  67.         list.add( Thread.class );
  68.         list.add( Map.class );
  69.         list.add( System.class );
  70.         list.add( DateFormat.class );
  71.         list.add( DateFormat.class );
  72.        
  73.         foo1( list.toArray( new Class<?>[ list.size() ] ) );
  74.     }
  75.    
  76.     public static void foo1( Class<?>[] array ){
  77.        
  78.         Map<Class<?>, Integer > map = new HashMap<>();
  79.        
  80.         for( Class<?> c : array ){
  81.            
  82.             Integer exists = map.get( c );
  83.            
  84.             if( null == exists )
  85.                 exists = 0;
  86.            
  87.             map.put( c, exists + 1 );
  88.         }
  89.        
  90.         for( Entry<Class<?>, Integer> entry : map.entrySet() )
  91.             System.out.println( "Class: " + entry.getKey().getSimpleName() + " appeared " + entry.getValue() + " times.");
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement