Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.Socket;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- public class Test{
- public static void main( String[] args ) throws IOException{
- testFoo2();
- }
- public static void testFoo2(){
- List<Object> list = new LinkedList<>();
- list.add( new String("1") );
- list.add( new String("2") );
- list.add( new String("3") );
- list.add( new String("4") );
- list.add( new SimpleDateFormat("dd/MM/yy") );
- list.add( new Socket() );
- list.add( new Integer(777) );
- list.add( new Integer(777) );
- list.add( new Integer(777) );
- list.add( new Thread() );
- foo2( list.toArray() );
- }
- public static void foo2( Object[] array ){
- Map<Class<?>, Integer > map = new HashMap<>();
- for( Object o : array ){
- Integer exists = map.get( o.getClass() );
- if( null == exists )
- exists = 0;
- map.put( o.getClass(), exists + 1 );
- }
- for( Entry<Class<?>, Integer> entry : map.entrySet() )
- System.out.println( "Class: " + entry.getKey().getSimpleName() + " appeared " + entry.getValue() + " times.");
- }
- public static void testFoo1(){
- List<Class<?>> list = new ArrayList<>(10);
- list.add( String.class );
- list.add( String.class );
- list.add( String.class );
- list.add( Integer.class );
- list.add( Thread.class );
- list.add( Thread.class );
- list.add( Map.class );
- list.add( System.class );
- list.add( DateFormat.class );
- list.add( DateFormat.class );
- foo1( list.toArray( new Class<?>[ list.size() ] ) );
- }
- public static void foo1( Class<?>[] array ){
- Map<Class<?>, Integer > map = new HashMap<>();
- for( Class<?> c : array ){
- Integer exists = map.get( c );
- if( null == exists )
- exists = 0;
- map.put( c, exists + 1 );
- }
- for( Entry<Class<?>, Integer> entry : map.entrySet() )
- System.out.println( "Class: " + entry.getKey().getSimpleName() + " appeared " + entry.getValue() + " times.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement