I would like to find an object. There is only one object with the variable x=10. Is it possible? I hope you get what I try to explain... :)
if (any object of the class X .getValue() == 10)
...
Class X
 public int getValue(){
    return x;
    }
I would like to find an object. There is only one object with the variable x=10. Is it possible? I hope you get what I try to explain... :)
if (any object of the class X .getValue() == 10)
...
Class X
 public int getValue(){
    return x;
    }
 
    
    List<X> xList = new ArrayList<>();
public static X findXWithValue(List<X> xList, int value) {
 X xWithValue = null;
 for(int i = 0 ; i < xList.size()-1 ; i++) {
  if (value == xList[i].getValue()) {
   xWithValue = xList[i];
   break;
  }
 }
 return xWithValue;
}
Br,
Rakesh
 
    
    Something like:
public class ObjectFinder {
public static boolean checkObject(Object o, String methodName, int value) {
    return Stream.of(o.getClass().getDeclaredMethods())
            .filter(method -> method.getName().equals(methodName))
            .filter(m -> checkType(m, int.class))
            .map(m -> {
                try {
                    return (int) m.invoke(o);
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                    return 0;
                }
            }).anyMatch(v -> value == v);
}
private static boolean checkType(Method method, Class type) {
    return method.getReturnType() == type;
}
}
and you can test it in:
public static void main(String[] args) {
        System.out.println(checkObject(new X(), "valueOf", 2));
    }
