0
I am developing an application in which it aims to create a list that respects the condition of being of type = "unit". In the code below, I create the list and from a function I try to generate this list.
code of the class 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;
}
}
code of class 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));
}
}
Error message:
Exception in thread "main" java.lang.Classcastexception: java.util.stream.Referencepipeline$2 cannot be cast to java.util.List at teste1.Teste.filterFractionedMovements(Test.java:17) at teste1.Teste.main(Test.java:34)
however I only have to create the list with the objects course description/name has "unit"
– alexjosesilva
this filter does not work: filter(px -> px.getName(). equals("unit")
– alexjosesilva
It should work, the only problem I found in your code was the lack of the Collector after the filter. Otherwise, your code does exactly what you want.
– Júlio Neto
@alexjosesilva Se usar
filter
along withcollect
, as indicated, works yes: https://ideone.com/AcUvLr - Maybe you did something else that was not indicated in the question, I do not know...– hkotsubo
@hkotsubo it is possible to insert two conditions into the filter ? Type or(or) ??
– alexjosesilva
@alexjosesilva Inside the filter you can put as many conditions as you want: https://ideone.com/c6cbPN - Most cases I see have one or two conditions, but in fact the function can be as complex as you need.
– hkotsubo
@hkotsubo can provide two examples of conditions in the.filter stream
– alexjosesilva
@alexjosesilva In the link to the previous comment (https://ideone.com/c6cbPN) you have an example - you are on line 20
– hkotsubo
can use : . filter((px -> px.getNome(). equals("unit")) or (px -> px.getNome(). equals("home")))
– alexjosesilva
@alexjosesilva Java "or" is using
||
->filter((px -> px.getNome().equals("unidade")) || (px -> px.getNome().equals("casa")))
– hkotsubo