Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. package main;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7.  
  8. public class App {
  9.  
  10. public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException,
  11. IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  12. List<String> list = new ArrayList<>();
  13. list.add("a");
  14. list.add("1");
  15. list.add("b");
  16. list.add("2");
  17. list.add("c");
  18. list.add("3");
  19. list.add("d");
  20. list.add("4");
  21. list.add("e");
  22. list.add("5");
  23. list.add("f");
  24. Entity entity = (Entity) Entity.class.getConstructors()[0]
  25. .newInstance(list.stream().map(str -> canParse(str) ? Integer.parseInt(str) : str)
  26. .collect(Collectors.toList()).toArray(new Object[] {}));
  27. System.out.println(entity);
  28. }
  29.  
  30. public static boolean canParse(String string) {
  31. try {
  32. Integer.parseInt(string);
  33. return true;
  34. } catch (NumberFormatException nfe) {
  35. return false;
  36. }
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement