You can use a dynamic proxy for each element:
class VeryBigPOJO {}
interface PojoWrapper {
VeryBigPOJO getPojo();
}
class MyHandler implements InvocationHandler {
private VeryBigPOJO ob;
public MyHandler( VeryBigPOJO ob ) {
this.ob = ob;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if( method.getName().equals( "getPojo") )
return ob;
if( method.getName().equals( "equals") )
if( method.getName().equals( "hashCode") )
}
}
Then wrap the objects in these dynamic proxies:
VeryBigPOJO pojo = ...;
PojoWrapper wrapper = Proxy.newProxyInstance( MyHandler.class.getClassLoader(), new Class[] { PojoWrapper.class}, new MyWrapper( pojo ) )
And now you can add your objects wrapperto this HashSet.
However, would it be easier to create a smaller, immutable object idfrom a large pojo? This would certainly be more readable.
source
share