Вопрос

I'm validating my List of data's from file with the data's in the database to avoid duplicate insertion in database,if duplicate exists then i want the index of that record in file,am using the below code,

List<StudentMaster> studentMasterListFromDB = studentMasterDao.getStudentList();
List<StudentMasterVO> studentMasterListFromFile = getStudentMasterListFromFile();

for(int index=0;index<studentMasterListFromDB.size();index++){
    StudentMasterVO studentMasVO = new StudentMasterVO();
    StudentMaster studentMaster = studentMasterListFromDB.get(index);       
    BeanUtils.copyProperties(studentMasVO, studentMaster);      
    int indexOfexistingRec = studentMasterListFromFile.indexOf(studentMasVO);
    System.out.println("indexOfexistingRec :"+indexOfexistingRec);


}

But "indexOfexistingRec" value is giving -1,not the index of the existing record index.

Это было полезно?

Решение

You need to implement hashCode and equals methods in your StudentMaster and StudentMasterVO classes so that they will be properly compared for equality.

If you are using Eclipse (or any other IDE), then it can generate those methods for you, just choose which fields are to be taken into account when comparing.

See for example this article if you are not familiar with the concept: http://www.javaworld.com/community/node/1006

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top