Condition to check if a value exists in the list containing objects,

I am trying to write an if condition to check the value exists in a list containing many objects. Here is my code:

List<TeacherInfo> teacherInfo=ServiceManager.getHelperService(TeacherManagementHelper.class, request, response).getTeacherInfoId();
if(teacherInfo.contains(inputParam))
{
    out2.println("<font color=red>");
    out2.println("Id Not Available");
    out2.println("</font>");
 }
 else
 {
    out2.println("<font color=green>");
    out2.println("Id Available");
    out2.println("</font>");        
 }

after executing the first sentence, the getTeacherInfoId()method successfully returns a list of objects, in those objects that I want to check, any object has the same value as inputParam. Is my code above? if wrong, please help me.

+4
source share
3 answers

contains(Object o)internally based on equalsbetween the objects of your list and your entry, as indicated in the document .

, inputParam - , , TeacherInfo, . , inputParam TeacherInfo.

Java 8, API contains():

List<TeacherInfo> teacherInfo=ServiceManager.getHelperService(TeacherManagementHelper.class, request, response).getTeacherInfoId();
if (teacherInfo.stream().anyMatch(ti -> ti.getId() == inputParam)) {
    // contains the id
} else {
    // does not contain the id
}

java- contains() TeacherInfo:

private static boolean containsTeacherId(List<TeacherInfo> teacherInfos, int id) {
    for (TeacherInfo ti : teacherInfos) {
        if (ti.getId() == inputParam) { // I used getId(), replace that by the accessor you actually need
            return true;
        }
    }
    return false;
}

:

List<TeacherInfo> teacherInfo=ServiceManager.getHelperService(TeacherManagementHelper.class, request, response).getTeacherInfoId();
if (containsTeacherId(teacherInfo, inputParam)) {
    // contains the id
} else {
    // does not contain the id
}

. , , getTeacherIds(), DB.

+16

, . "teacherInfo", () hashvalue() .

0

You will need to iterate through the teacherInfo list and compare each element of this list with inputParam.

Below is a small demo code that may help you.

I created anInfo tester similar to your teacherInfo and a parameter similar to your input parameter. Hope this helps.

Tester.java

/**
 * 
 */
package com.demo;

/**
 * @author Parul
 *
 */
public class Tester {

    private int id;
    private String name;
    /**
     * @return the id
     */
    public int getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    public Tester(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public Tester() {

    }

}

Demo.java

/**
 * 
 */
package com.demo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Parul
 *
 */
public class Demo {

    public static void main(String [] args){

        List<Tester> testerInfo=new ArrayList<Tester>();
        testerInfo.add(new Tester(1,"Java"));
        testerInfo.add(new Tester(2,"C++"));
        testerInfo.add(new Tester(3,"Python"));
        testerInfo.add(new Tester(4,"C"));
        Tester tester=null;
        int param=2;
        for(int i=0;i<testerInfo.size();i++){
            tester=testerInfo.get(i);
            if(tester.getId()==param){
                System.out.println("param found: "+tester.getName());
                break;
            }

        }


    }


}

OUTPUT
param found: C++
0
source

Source: https://habr.com/ru/post/1538358/


All Articles