Skip to content

Instantly share code, notes, and snippets.

@namila007
Created July 15, 2019 06:50
Show Gist options
  • Save namila007/80ffd737792ca842623977d6e7a6cf83 to your computer and use it in GitHub Desktop.
Save namila007/80ffd737792ca842623977d6e7a6cf83 to your computer and use it in GitHub Desktop.
Finding a max int value in given list of objects using its variable name
private <E> int findtheLastNoFromtheList( List<E> list, String getFieldName )
{
if ( list.isEmpty() )
{
return 0;
}
int maxCount = 0;
try
{
for ( E item : list )
{
try
{
Field field = item.getClass().getDeclaredField( getFieldName );
field.setAccessible( true );
Integer value = ( Integer ) field.get( item );
if ( value > maxCount )
{
maxCount = value;
}
}
catch ( IllegalAccessException e )
{
throw e;
}
catch ( NoSuchFieldException e )
{
throw e;
}
}
}
catch ( Exception e )
{
return list.size();
}
return maxCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment