简体   繁体   中英

How to get specific object from list of objects by using unique id of that object in java?

I want specific object with all it's values by using it's unique id of object from object list.

I have tried but i am getting index -1 while running below code.

List<JobDataDetail> jobList = getJobList();
JobDataDetail object = jobList.get(jobList.indexOf(new JobDataDetail(jobReferenceId)));

from the class

public class JobDataDetail implements Serializable,Comparable<JobDataDetail> {

    public int jobSequence;
    public String jobReferenceId;
    public String jobAddress;
    public String jobScheduledDate;

    public JobDataDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

    public JobDataDetail(int jobSequence){
        super();
        this.jobSequence = jobSequence ;
    }
    public JobDataDetail(String jobReferenceId){
        super();
        this.jobReferenceId = jobReferenceId;
    }

    public int getJobSequence() {
        return jobSequence;
    }
    public void setJobSequence(int jobSequence) {
        this.jobSequence = jobSequence;
    }
    public String getJobReferenceId() {
        return jobReferenceId;
    }
    public void setJobReferenceId(String jobReferenceId) {
        this.jobReferenceId = jobReferenceId;
    }
    public String getJobAddress() {
        return jobAddress;
    }
    public void setJobAddress(String jobAddress) {
        this.jobAddress = jobAddress;
    }
    public String getJobScheduledDate() {
        return jobScheduledDate;
    }
    public void setJobScheduledDate(String jobScheduledDate) {
        this.jobScheduledDate = jobScheduledDate;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((jobReferenceId == null) ? 0 : jobReferenceId.hashCode());
        result = prime * result + jobSequence;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        JobDataDetail other = (JobDataDetail) obj;
        if (jobReferenceId == null) {
            if (other.jobReferenceId != null)
                return false;
        } else if (!jobReferenceId.equals(other.jobReferenceId))
            return false;
        if (jobSequence != other.jobSequence)
            return false;
        return true;
    }

    @Override
    public int compareTo(JobDataDetail another) {
        return this.getJobReferenceId().compareTo(another.getJobReferenceId());
    }

}

我已经从equals方法中删除了jobSequence条件检查,它正在工作。

List.indexOf() uses equals() method to compare objects.

In your case, you are assuming that two objects with same jobReferenceId are equals but your equals() method doesn't say so ( because of the jobSequence test at the end of your method ).


If you want to get an item from your list by one of its attribute, the easiest way would be using filter expression in Java 8:

JobDataDetail job = jobList.stream()
                           .filter(j -> j.getAttribute().equals(someValue))
                           .findFirst();

If Java 8 is not an option, I would go for a classic for loop iterating over the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM