Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package me.mayuan.test;
  2.  
  3. import java.lang.annotation.Annotation;
  4. import java.lang.reflect.Field;
  5. import java.util.HashMap;
  6.  
  7. public class Everybody {
  8.    
  9.     @Person(NAME="tommy",AGE=26,SEX="boy")
  10.     public String tommy = "I am a gay boy";
  11.     @Person(NAME="army",AGE=14,SEX="girl")
  12.     public String army = "I like girls naked body";
  13.    
  14.    
  15.     public static HashMap<String, People> createMap(){
  16.         long startTime=System.nanoTime();
  17.         Everybody ea = new Everybody();
  18.         HashMap<String,People> people = new HashMap<String,People>();
  19.         Field[] fields = ea.getClass().getDeclaredFields();
  20.         for(Field f : fields) {
  21.             if(!f.isAccessible())f.setAccessible(true);
  22.             if(f.isAnnotationPresent(Person.class)) {
  23.                 Annotation an = f.getDeclaredAnnotation(Person.class);
  24.                 Person p = (Person)an;
  25.                 String name = p.NAME();
  26.                 int age = p.AGE();
  27.                 String sex = p.SEX();
  28.                 String msg = "";
  29.                 try {
  30.                     msg = (String)f.get(ea);
  31.                    
  32.                 } catch (IllegalArgumentException | IllegalAccessException e) {
  33.                     e.printStackTrace();
  34.                 }
  35.                 People peo = new People(name,age,sex, msg);
  36.                 people.put(f.getName(), peo);
  37.             }
  38.         }
  39.         long endTime=System.nanoTime();
  40.         System.out.println("Process Running Time:"+(endTime-startTime)+"ns");  
  41.         return people;
  42.     }
  43.    
  44.     public static HashMap<String, People> createMapByHand(){
  45.         long startTime=System.nanoTime();
  46.         HashMap<String,People> people = new HashMap<String,People>();
  47.         people.put("tommy", new People("tommy", 26, "boy", "I am a gay boy"));
  48.         people.put("army", new People("army", 14, "girl", "I like girls naked body"));
  49.         long endTime=System.nanoTime();
  50.         System.out.println("Process Running Time2:"+(endTime-startTime)+"ns");  
  51.         return people;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement