Finding Object in a List

1

I have the following objects

    public class Passo {
      private Long id;
      private Date dataAtend;
      private Time horaRealizada;
      private Long idProfissionalRealizador;
      private String Descricao;
      /*getter & Setters*/  
    }

    public class Atendimento
    {
      private Long id;
      private Date dataAtend;
      private Long idPaciente;
      private Long idProfissionalResponsavel;
      private List<Passo> passos;

      /*getter & Setters*/ 
      public void addPasso(Passo passo){
        passos.add(passo)
      }

    }  

And I have a list of appointments, where I have to find a service by your ID, in this list, and insert more Steps. Something like that

    ...{
      List<Atendimento> atd = new ArrayList<Atendimento>();
      int idAtendimento;
      Passo passo;
      /* Trecho de codigo*/
      int index = atd.indexOf(atd.element.id = idAtendimento);
      if (index > 0 )
      {
        Atendimento(atd.get(index)).addPasso(passo);    
      }  
      else
      {
        Atendimento atend =util.GetAtendimento(idAtendimento);
        atend.addPasso(passo);
        atd.add(atend);
      }
    }

I can not overwrite equals, since it is already overwritten by comparing all values ... = /

    
asked by anonymous 10.11.2015 / 18:41

2 answers

3

Does not it work here?

private List<Atendimento> lista = new ArrayList<>();

private Atendimento buscarPorId(long id) {
    for (Atendimento a : lista) {
        if (a.getId().longValue() == id) return a;
    }
    return null;
}

private void adicionarPasso(long idAtendimento, Passo passo) {
    Atendimento a = buscarPorId(idAtendimento, lista);
    if (a == null) {
         a = fazerAlgumaCoisaQueCrieOuObtenhaOAtendimentoComEsteId(idAtendimento);
         lista.add(a);
    }
    a.addPasso(passo);
}

Another idea is to use Map instead of List , to avoid having to scroll through the list to look for an element. It would look like this:

private Map<Long, Atendimento> map = new TreeMap<>();

private void adicionarPasso(long idAtendimento, Passo passo) {
    Atendimento a = map.get(idAtendimento);
    if (a == null) {
         a = fazerAlgumaCoisaQueCrieOuObtenhaOAtendimentoComEsteId(idAtendimento);
         map.put(idAtendimento, a);
    }
    a.addPasso(passo);
}

If you need to convert List to Map on some occasion, you can use this:

private static Map<Long, Atendente> mapear(List<Atendimento> lista) {
    Map<Long, Atendente> map = new TreeMap<>();
    for (Atendimento a : lista) {
         map.put(a.getId(), a);
    }
    return map;
}

If you need to convert Map to List :

List<Atendimento> lista = new ArrayList<>(map.values());

Or, if you prefer to convert without having to create a defensive copy, you can also use this:

Collection<Atendimento> lista = map.values();
    
11.11.2015 / 00:24
0

Take a look at on this material over Comparator e Comparable , from Caelum. With it you can set the id attribute of your object as a parameter for the comparison.

I hope the material is helpful (it was my reference when I needed this type of technique = D)

Embrace

    
10.11.2015 / 19:50