Created
July 15, 2019 06:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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