Estou desenvolvendo uma aplicação na qual tem como objetivo criar uma lista que respeita a condição de ser do tipo = "unidade". No código abaixo, crio a lista e a partir de uma função tento gerar essa lista.
codigo da classe ProductMovement
package teste1;
public class ProductMovement {
private int id;
private String nome;
public ProductMovement(int id, String nome) {
this.id = id;
this.nome = nome;
}
public ProductMovement() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
codigo da classe Teste1
package teste1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import teste1.ProductMovement;
public class Teste {
@SuppressWarnings("unchecked")
private static List<ProductMovement> filterFractionedMovements(List<ProductMovement> reservMovements) {
return (List<ProductMovement>) reservMovements.stream().filter(px -> px.getNome().equals("unidade"));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Creating a list
List<ProductMovement> pro = Arrays.asList(new ProductMovement(1,"unidade"),
new ProductMovement(2,"caixa"),
new ProductMovement(3,"unidade"),
new ProductMovement(4,"caixa"));
//Stream<ProductMovement> ProductMovements = pro.stream();
//Integer idTipoProduto = ProductMovements.filter(px -> px.getNome().equals("unidade")).mapToInt(px -> px.getId()).sum();
//System.out.println(idTipoProduto);
//lista somente
List<ProductMovement> lista = filterFractionedMovements(pro);
lista.forEach(element -> System.out.println(element));
}
}
Mensagem de erro:
Exception in thread "main" java.lang.ClassCastException: java.util.stream.ReferencePipeline$2 cannot be cast to java.util.List at teste1.Teste.filterFractionedMovements(Teste.java:17) at teste1.Teste.main(Teste.java:34)