Locating Object in a List

Asked

Viewed 1,616 times

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 Calls, where I have to locate a call by your ID, on 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 cannot overwrite the equals, because it is already overwritten comparing all values... =/

  • Public Class Passo - You know that the public and the class should be in lower case, right?

  • Correct, Victor. =)

  • Simply make a for who searches the list for an element that contains the desired id and returns that element or null case not found does not solve for you?

  • Dude, I don’t understand your example. The chunk of code you gave doesn’t compile or filling what is missing in an imaginative way, because in the line int index = atd.indexOf(atd.element.id = idAtendimento); the idAtendimento is an uninitialized variable. In addition, the assignment within the indexOf seems to me very suspicious and confused. Also, Atendimento(atd.get(index)) also does not compile because Atendimento is a class, not a method. Also, you don’t know what it is util.GetAtendimento.

2 answers

3


That doesn’t 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 one Map instead of a List, to avoid having to scroll through the list to find an element. This would be:

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 the List for Map at some point, 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 the Map for a List:

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

Or else, if you prefer to convert without creating defensive copy, you can also use it like this:

Collection<Atendimento> lista = map.values();

0

Take a look in this material about Comparator and Comparable, of Caelum. With it you can define the attribute id of your object as a parameter for comparison.

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

Hug

  • the problem is that I don’t want to sort, but get the class. but at the end of this article has a link to another that seems more interesting. I think he’s more useful to me

  • Just create a method that will use the compareTo and when equal, return the selected object in comparison. I believe that this way does not bring you so much work in the implementation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.