First make a lot of Developer using HashSet<Developer> . This set will remove duplicates itself if you override the hashCode method from the Developer class. For example, you can override the hashCode method as follows:
@Override public int hashCode() { return id; }
Then create a set:
HashSet<Developer> tests = new HashSet<>();
And add to it all the objects of the Developer class that you want to write to the file. After adding all the objects of the Developer class will be added to the set, except for duplicates.
However, be careful, after overriding the hashCode method, you may change other parts of the code in which you use this class.
The best solution would be to create additional methods that collect a list of all non-duplicate objects.
private List<Developer> getDeveloperList(List<Developer> totalDeveloperList){ if(totalDeveloperList == null || totalDeveloperList.isEmpty()){ return totalDeveloperList; } List<Developer> developerList = new ArrayList<>(); for(Developer developer : totalDeveloperList){ if(!hasDeveloper(developerList, developer)){ developerList.add(developer); } } return developerList; } private boolean hasDeveloper(List<Developer> developerList, Developer checkableDeveloper){ if(developerList == null || developerList.isEmpty()){ return false; } for(Developer developer : developerList){ if(developer.getId() == checkableDeveloper.getId()){ return true; } } return false; }