-
Notifications
You must be signed in to change notification settings - Fork 10
PR - Diogo - Pratica de Lambda #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.github.deividfrancis; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import com.github.deividfrancis.backup.Person; | ||
|
||
public class Carrinho { | ||
private static List<Produtos> produtoList = new ArrayList<Produtos>(); | ||
|
||
static { | ||
produtoList.add(new Produtos(744, "Redragon Kumara" , 243.35, "Tecnologia", 'S')); | ||
produtoList.add(new Produtos(850, "Redragon Cobra" , 190.20, "Tecnologia", 'S')); | ||
produtoList.add(new Produtos(022, "SSD M.2" , 102.02, "Tecnologia", 'S')); | ||
produtoList.add(new Produtos(848, "Monitor 24p 165hz" , 1153.0, "Tecnologia", 'N')); | ||
produtoList.add(new Produtos(254, "Oculos de ciclismo" , 104.02, "Esporte" , 'S')); | ||
produtoList.add(new Produtos(78, "Molinete de pesca" , 175.00, "Esporte" , 'N')); | ||
produtoList.add(new Produtos(415, "Tenis allstar" , 167.00, "Vestuario" , 'S')); | ||
produtoList.add(new Produtos(403, "Luva de motociclista" , 134.00, "Vestuario" , 'N')); | ||
produtoList.add(new Produtos(625, "Chave de fenda magnética", 55.00, "Equipamentos", 'S')); | ||
produtoList.add(new Produtos(573, "Tapete Geometrico", 114.00, "Lazer", 'S')); | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
// filtroTecnologia(); | ||
// filtroEstoqueValor200(); | ||
// filtroTemEstoque(); | ||
// filtroEsporte(); | ||
// filtroPrimeiraEquipamentos(); | ||
// agrupaAlfabetica(); | ||
// groupByCategoria(); | ||
// getMaiorValor(); | ||
// getListIds(); | ||
} | ||
|
||
|
||
private static void filtroTecnologia() { | ||
List<Produtos> produtoTempList = Carrinho.produtoList.stream() | ||
.filter(p -> "Tecnologia" == p.getCategoria()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tem certeza que esse método funcionou? a forma correta de comparar Objetos no java seria utilizar o método |
||
.collect(Collectors.toList()); | ||
|
||
System.out.println(produtoTempList); | ||
} | ||
|
||
private static void filtroEstoqueValor200() { | ||
List<Produtos> produtoTempList = produtoList.stream() | ||
.filter(p -> 'S' == p.getTemEstoque() && p.getValor() > 200.00) | ||
.collect(Collectors.toList()); | ||
|
||
System.out.println(produtoTempList); | ||
} | ||
|
||
private static void filtroTemEstoque() { | ||
List<Produtos> produtoTempList = produtoList.stream() | ||
.filter(p -> 'S' == p.getTemEstoque()) | ||
.collect(Collectors.toList()); | ||
|
||
produtoTempList.stream().forEach(p -> { | ||
System.out.println(p.getNome()); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DICA Interessante a forma que foi pensada para resolver essa atividade, talvez outra forma legal de fazer seria aproveitando o stream que fez no começo e adicionando mais uma condição ex: - List<Produtos> produtoTempList = produtoList.stream()
+ List<String> produtoNamesList = produtoList.stream()
.filter(p -> 'S' == p.getTemEstoque())
+ .map(Produtos::getName)
.collect(Collectors.toList()); |
||
} | ||
|
||
private static void filtroEsporte() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O objetivo era fazer a soma do atributo |
||
Long produtoTempList = produtoList.stream() | ||
.filter(p -> "Esporte" == p.getCategoria()) | ||
.collect(Collectors.counting()); | ||
|
||
System.out.println(produtoTempList); | ||
} | ||
|
||
private static void filtroPrimeiraEquipamentos() { | ||
Produtos produtoTempList = produtoList.stream() | ||
.filter(p -> p.getCategoria() == "Equipamentos") | ||
.findFirst() | ||
.orElse(null); | ||
|
||
System.out.println(produtoTempList); | ||
} | ||
|
||
public static void agrupaAlfabetica() { | ||
List<Produtos> produtoTempList = produtoList.stream() | ||
.sorted((x1 , x2) -> x1.getNome().compareTo(x2.getNome())) | ||
.toList(); | ||
|
||
System.out.println(produtoTempList); | ||
} | ||
|
||
public static void groupByCategoria() { | ||
Map<String, List<Produtos>> produtoMap = produtoList.stream().collect(Collectors.groupingBy(Produtos::getCategoria)); | ||
|
||
System.out.println(produtoMap); | ||
} | ||
|
||
private static void getMaiorValor() { | ||
Optional<Produtos> produtoTempList = produtoList.stream() | ||
.sorted((x1 , x2) -> x2.getValor().compareTo(x1.getValor())) | ||
.findFirst(); | ||
|
||
produtoTempList.stream().forEach(p -> { | ||
System.out.println(p.getCategoria()); | ||
}); | ||
} | ||
private static void getListIds() { | ||
List <Produtos> produtoTempList = produtoList.stream() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interessante sua forma de pensar, parabéns por resolver o problema mas tem uma limitação na lógica, imagine que esses ids são informados pelo usuário... seu código não ia suportar, uma forma seria: List<Integer> ids = Arrays.asList(850, 403, 625);
List <Produtos> produtoTempList = produtoList.stream()
.filter(p -> ids.contains(p.getId()))
.toList(); |
||
.filter(p -> 850 == p.getId() || 403 == p.getId() || 625 == p.getId()) | ||
.toList(); | ||
|
||
System.out.println(produtoTempList); | ||
} | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.github.deividfrancis; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Produtos { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DICA:
- Produtos
+ Produto
|
||
private Integer id; | ||
private String nome; | ||
private Double valor; | ||
private String categoria; | ||
private char temEstoque; | ||
|
||
public Produtos(Integer id, String name, Double valor, String categoria, char temEstoque) { | ||
this.id = id; | ||
this.nome = name; | ||
this.valor= valor; | ||
this.categoria = categoria; | ||
this.temEstoque = temEstoque; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
public String getNome() { | ||
return nome; | ||
} | ||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
public Double getValor() { | ||
return valor; | ||
} | ||
public void setValor(Double valor) { | ||
this.valor = valor; | ||
} | ||
public String getCategoria() { | ||
return categoria; | ||
} | ||
public void setCategoria(String categoria) { | ||
this.categoria = categoria; | ||
} | ||
public char getTemEstoque() { | ||
return temEstoque; | ||
} | ||
public void setTemEstoque(char temEstoque) { | ||
this.temEstoque = temEstoque; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Id=" + id + ", Nome=" + nome + ", Valor=R$" + valor + ", categoria=" + categoria + ", temEstoque=" + temEstoque +"\n\n"; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Não consigo vincular de forma simples qual método é a resposta de cada pergunta, fica como dica adicionar um comentário com a questão para ajudar quem vai corrigir kkk.