Java: Find Id By Title () From Models

- 1 answer

This is my function, to find a id from list by matching titles

 public static int findIdByTitle(List<Integer> IDs, List<String> Titles ,String title){

    for (int i = 0; i < IDs.size(); i++){
        if (Titles.get(i).equals(title))
            return IDs.get(i);
    return 0;

This function search in model list and find id

But I can't use this because:

int id = findIdByTitle(Core.userSignIn.getBasicList().getGradeList().get(****?****).getID()

I must give it a number position : ( see the lists.get(****?****).getID() or getName()

I want using that's function for all models not just for this model give 2 lists and a matching word, use matching word to find position of that's in list, and give me a ID of this position

All of my model have ID , Title and some of them have ParentID

with help of Nitin , My problem has been resolved:

            List<String> Titles = new ArrayList<>();
            List<Integer> IDs = new ArrayList<>();
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                Titles = Core.userSignIn.getBasicList().getGradeList().stream().map(GradeList::getGradeName).collect(Collectors.toList());
                IDs = Core.userSignIn.getBasicList().getGradeList().stream().map(GradeList::getGradeID).collect(Collectors.toList());
            } else {
                Titles = new ArrayList<>(Core.userSignIn.getBasicList().getGradeList().size());
                IDs = new ArrayList<>(Core.userSignIn.getBasicList().getGradeList().size());
                for (GradeList gradeList : Core.userSignIn.getBasicList().getGradeList()) {


You first have to convert your list to List of Integer and List of String and then pass it to your method. You can use below code if you are using Java 8:

final List<Integer> testList1 = BasicList().getGradeList().stream().map(Test::getId).collect(Collectors.toList());
final List<String> testList2 = BasicList().getGradeList().stream().map(Test::getName).collect(Collectors.toList());

Replace your class name in place of Test in above code.

Use below code for below java 8 env:

List<String> strings = new ArrayList<>(BasicList().getGradeList().size());
for (Test test : BasicList().getGradeList()) {

Similar for Ids.