Adicionar ou atualizar um Objeto ArrayList

21/03/2016

3

Boa tarde pessoal.

Estou tentando adicionar um objeto e num ArrayList do Objeto.
Ou ele adiciona com valores repetido ou não adiciona.


final ArrayList<Trackers> trackersList = new ArrayList<>();

Trackers trackers = new Trackers();
trackers.setSn(codRastreador);

if(trackersList.contains(trackers)){
      trackersList.remove(trackers);
      trackers.setAddress(ipClient);
      trackers.setPort(portClient);
      trackers.setTime(Calendar.getInstance());
      trackersList.add(trackers);
}
else{
      trackers.setAddress(ipClient);
      trackers.setPort(portClient);
      trackers.setTime(Calendar.getInstance());
      trackersList.add(trackers);
}


Já tentei dessa outra forma

for(Trackers track : trackersList){
       if(trackers.getSn().equals(track.getSn())){
               trackersList.remove(trackers);
               trackers.setAddress(ipClient);
               trackers.setPort(portClient);
               trackers.setTime(Calendar.getInstance());
               trackersList.add(trackers);
      }
      else{
               trackers.setAddress(ipClient);
               trackers.setPort(portClient);
               trackers.setTime(Calendar.getInstance());
               trackersList.add(trackers);
      }
}
Responder