Create Generic type in Arraylist to persist data

Asked

Viewed 1,325 times

3

I’m having trouble understanding the concept of Dao and how I could create a specific type to store my data, I’m initially using a String Arraylist and need to adapt it to an Object Product Arraylist, which would have for example code/description/quantity.

As I declare:

 private static ArrayList<String> Produto = new ArrayList<>();

As I add:

Produto.add(codigo + " | " + descricao + " | " + quantidade);

How I read:

listaDados = Produto.stream().map((string) -> string + "\n").reduce(listaDados, String::concat);

How would this look on a Product object instead of String?

  • You want to know how to read an arraylist of custom objects using correct stream?

  • @Diegof I need to understand how to create a generic type, in my example I use the ArrayList<String> would like to use ArrayList<Produto>.

  • Wouldn’t be the case to use Generics Type? http://www.tutorialspoint.com/java/java_generics.htm

  • @Nilsonuehara this would be like c template++?

  • I don’t know C++. I’ll post an answer with an example of use.

  • I posted an answer, but I would really like to understand your problem in order to improve it. Your question is on how to create a class Produto and an instance of it? Or are you using Java Generics?

Show 1 more comment

1 answer

2


I believe that’s what you want:

class Produto{
    private Integer codigo;
    private String descricao;
    private Integer quantidade;

    public Produto(Integer codigo, String descricao, Integer quantidade){
        this.codigo = codigo;
        this.descricao = descricao;
        this.quantidade = quantidade;
    }

    /*Getters setters*/
    @Override
    public String toString(){
         return this.codigo+" "+this.descricao+" "+this.quantidade;
    }
}

Now that you have your class Produto, you can create some objects of it and add to a ArrayList of Produto

Produto produtoCueca = new Produto(1, "Cueca", 5);
Produto produtoCalcinha = new Produto(2, "Calcinha", 5);

//Lista de produtos
ArrayList<Produto> listaProdutos = new ArrayList<Produto>();
listaProdutos.add(produtoCueca);
listaProdutos.add(produtoCalcinha);

Explanation of toString: In java, when you try to print an instance of a class, it calls the method toString of that class. But as you did not make the definition of this method, the toString is called from the parent class, in case Object, which causes the System.out.println do not stay in the way desired.

So if you want to customize how an instance of your class will be printed, you have to override the method toString.

Reference: when to use toString() method

  • Exactly this, I will try to implement the solution!

  • I made a change two seconds ago because I was using simple quotes for String (long time using python). So be careful not to use the code with single and double quotes on "Cueca" and "Calcinha"

  • Now the way of reading it is different right ? the normal would be Produto.get(i) if I give a print it comes like this: [estoqueDao.DaoProduto@e2d56bf, estoqueDao.DaoProduto@244038d0, estoqueDao.DaoProduto@5680a178]

  • According to the java documentation for Arraylist now is if you want to get the object Produto who is in position i, you must use listaProdutos.get(i)

  • here is a minimal example of the problem: https://ideone.com/cXiBq2 separated into 2 main and product files and only play in . java and run

  • I am without java on this machine, but what is the problem? It seems to me that your code is correct. With just one detail, does it make any sense to me that you change the product code to String, but why did it with quantity? It will cause you a huge headache if you want to calculate stock or make quantity reduction. Quantity I would keep as Integer

  • is only to test, I will not perform calculations for now, the problem is that when performing the reading instead of returning the data correctly as for example an array they return so: main.Produto@15db9742&#xA;main.Produto@6d06d69c

  • I’ll modify the code and explain it to you above

  • was this, I had even tried to do: listaProdutos.get(i).toString() but it would work the same way because it would be applied to the parent class. thanks!

  • Happy to help ;)

Show 5 more comments

Browser other questions tagged

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