fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.reflect.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception {
  12.  
  13.  
  14. List<Data> allItems = new ArrayList<>();
  15. allItems.add(new Data("x", "y", "z"));
  16. allItems.add(new Data("x", "y", "a"));
  17. allItems.add(new Data("x", "y", "b"));
  18. allItems.add(new Data("x", "y", "c"));
  19. allItems.add(new Data("x", "d", "z"));
  20. allItems.add(new Data("e", "y", "z"));
  21. List<Data> filtered = new ArrayList<>();
  22. Map<String,String> propertyMap = new HashMap<>();
  23. propertyMap.put("getA", "x");
  24. propertyMap.put("getB", "y");
  25. for (Data d : allItems) {
  26. boolean allPass = true;
  27. for (Map.Entry<String,String> entry : propertyMap.entrySet()) {
  28. Method m = d.getClass().getMethod(entry.getKey());
  29. if (m == null || !entry.getValue().equals(m.invoke(d))){
  30. allPass = false;
  31. break;
  32. }
  33. }
  34. if (allPass) {
  35. filtered.add(d);
  36. }
  37. }
  38. for (Data d : filtered) {
  39. System.out.println(d.getA()+"-"+d.getB()+"-"+d.getC());
  40. }
  41.  
  42.  
  43. }
  44. }
  45.  
  46. class Data {
  47. String a;
  48. String b;
  49. String c;
  50. public String getA() {
  51. return a;
  52. }
  53. public String getB() {
  54. return b;
  55. }
  56. public String getC() {
  57. return c;
  58. }
  59. public Data(String a, String b, String c) {
  60. this.a = a;
  61. this.b = b;
  62. this.c = c;
  63. }
  64. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
x-y-z
x-y-a
x-y-b
x-y-c