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