Get List Help (Code Refactoring)

Asked

Viewed 271 times

4

I have a class with 2 attributes:

public class Classe(){
 private String conteudo;
 private String tipoConteudo;

 getters...setters..
}

So I have a Arraylist with 3 or more objects of this class, as I do in a row one get of a specific Object of a certain content??

For example, if I want the object that the type is blue, in several lines it would be:

Classe objetoFinal;

for(int i;i<List.size();i++){
  if(objetoFinal.tipoConteudo.equals("azul")
     objetoFinal = List.get(i);
}

So the question is: how to do this in a row (need to place on a JSF :D page)

  • 1

    You cannot make a method with your search code and then use it on the other page?

  • 1

    Why should it be on a line? What does this requirement have to do with JSF?

  • I would recommend a functional approach.

  • @Miguelcartagena probably because he is wanting to use this in an expression (e.g..: ${ ... }) that must return a value.

  • 1

    @mgibsonbr if that’s the case, Fábio’s solution is perfect.

3 answers

1

To perform this operation in only one line you must put the search code in a getter method in Managedbean, let’s see:

public class MyBean {
    public Object getObjetoFinal(){
        Object objetoFinal = null;

        for(int i;i<List.size();i++){
            if(objetoFinal.tipoConteudo.equals("azul")
                objetoFinal = List.get(i);
        }

        return objetoFinal;
     }
}

Then on a JSF page you put the expression in just one line, see:

    <h:outputText value = "#{myBean.objetoFinal.conteudo}" />

This way you will have an instance or null if no blue object is found.

  • 1

    It is not better to use for (Classe c : List) {...}?

  • Yes. The enhanced is an interesting shortcut in Java 6, but I preferred to use the code snippet of the question itself to make the answer simple and straightforward.

  • Right, but the original code is wrong. It should be if (List.get(i).tipoConteudo.equals("azul")). I only left that note because for traditional calling size with each iteration and then making a get(i) is less efficient and leaves the code more polluted. Otherwise your answer is correct.

1

In JSF, data can be recovered in a facelet (file .xhtml) or even in a JSP through simple method calls getter by Mr JSF.

No need to create Expression Languages elaborated, it is sufficient to delegate the necessary logic to the getter of Managed Bean.

One point to be considered is that there is no guarantee that a getter will be called only once. If the code executed in getter has some cost, for example reading in the database, one must "cachear" the value in a bean attribute.

Example:

private List<Classe> list;
public String getListClasses() {
    if (list == null) {
        list = dao.listaClassesDoBanco();
    }
    return list;
}

And in the case of an operation that searches for an attribute, depending on the cost, one could also create a map whose key is the value of the attribute.

Also, if the list is not changed on the screen it should be loaded only once. This can be done as in the above method or in an annotated method with @PostConstruct, which is called whenever the bean is recreated in its proper scope.

Example:

public class ManagedBean {

    private List<Classe> list = new ArrayList<Classe>();
    private Map<String, Classe> mapaClasse;

    @PostConstruct
    public void init() {
        list = dao.listaClassesDoBanco();
    }

    private Map<String, Classe> getMapaClasse() {
        if (mapaClasse == null) {
            mapaClasse = new HashMap<String, Classe>();
            for (Classe c : list) {
                mapaClasse.put(c.tipoConteudo, c);
            }
        }
        return mapaClasse;
    }

    public Classe getObjetoAzul() {
        return getMapaClasse().get("azul");
    }

    ...

}

The expression on your facelet looks like this:

#{managedBean.objetoFinal.objetoAzul}

You can also access the map value directly:

#{managedBean.mapaClasse.azul}

Or:

#{managedBean.mapaClasse['azul']}

If you are using EL 2.2, you can still do this:

public Classe getObjetoPorTipoConteudo(String tipoConteudo) {
    return getMapaClasse().get(tipoConteudo);
}

And use the expression:

#{managedBean.getObjetoPorTipoConteudo('azul')}

0

Sorry for the delay to answer, but I was able to solve the problem, but now I’ve changed the project code and I’m not using this piece of code anymore, but I used the idea of the first liobntt comment

  • post your answer in the form of an answer, as if you were answering a question from another person, because your answer as it stands fits more like a comment

Browser other questions tagged

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