Escolar Documentos
Profissional Documentos
Cultura Documentos
Características:
Uma Stream não é uma estrutura de dados, portanto,
necessita de uma origem de dados (ex: Collections)
Pode ser processada uma única vez
Facilita a utilização da programação funcional e do
paralelismo no processamento
Elementos básicos de uma
Stream
Origem - de onde os dados vêm, pode ser uma
Collection ou um Array, por exemplo.
Saída:
CartItem[product=USB Mouse, productCategory=Peripherals, unitPrice=219.9, quantity=1]
CartItem[product=Bluetooth Mouse, productCategory=Peripherals, unitPrice=249.9, quantity=1]
CartItem[product=USB Keyboard, productCategory=Peripherals, unitPrice=89.9, quantity=1]
CartItem[product=Gaming Mousepad, productCategory=Peripherals, unitPrice=54, quantity=1]
CartItem[product=Smartphone, productCategory=Mobile, unitPrice=799.99, quantity=1]
CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
Exemplo de uso: map
Exemplo 2: transformar cada objeto em uma String
contendo o nome do produto
Saída:
USB Mouse
Bluetooth Mouse
USB Keyboard
Gaming Mousepad
Red Type-C USB Cable
Black Type-C USB Cable
Micro USB Cable
Micro USB Cable
Smartphone
Bluetooth Headset
Bluetooth Headset
Exemplo de uso: sorted
Exemplo 3: ordenar pelo nome do produto
Saída:
CartItem[product=Black Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
CartItem[product=Bluetooth Mouse, productCategory=Peripherals, unitPrice=249.9, quantity=1]
CartItem[product=Gaming Mousepad, productCategory=Peripherals, unitPrice=54, quantity=1]
CartItem[product=Micro USB Cable, productCategory=Accessories, unitPrice=4.99, quantity=2]
CartItem[product=Micro USB Cable, productCategory=Accessories, unitPrice=5.99, quantity=4]
CartItem[product=Red Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
CartItem[product=Smartphone, productCategory=Mobile, unitPrice=799.99, quantity=1]
CartItem[product=USB Keyboard, productCategory=Peripherals, unitPrice=89.9, quantity=1]
CartItem[product=USB Mouse, productCategory=Peripherals, unitPrice=219.9, quantity=1]
Exemplo de uso: distinct
Exemplo 4: remover os itens duplicados
Saída:
CartItem[product=USB Mouse, productCategory=Peripherals, unitPrice=219.9, quantity=1]
CartItem[product=Bluetooth Mouse, productCategory=Peripherals, unitPrice=249.9, quantity=1]
CartItem[product=USB Keyboard, productCategory=Peripherals, unitPrice=89.9, quantity=1]
CartItem[product=Gaming Mousepad, productCategory=Peripherals, unitPrice=54, quantity=1]
CartItem[product=Red Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
CartItem[product=Black Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
CartItem[product=Micro USB Cable, productCategory=Accessories, unitPrice=4.99, quantity=2]
CartItem[product=Smartphone, productCategory=Mobile, unitPrice=799.99, quantity=1]
CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
Exemplo de uso: peek
Exemplo 5: remover os itens duplicados (+operação
peek)
Saída:
Item sendo analisado: CartItem[product=USB Mouse, productCategory=Peripherals, unitPrice=219.9, quantity=1]
Item final: CartItem[product=USB Mouse, productCategory=Peripherals, unitPrice=219.9, quantity=1]
Item sendo analisado: CartItem[product=Bluetooth Mouse, productCategory=Peripherals, unitPrice=249.9, quantity=1]
Item final: CartItem[product=Bluetooth Mouse, productCategory=Peripherals, unitPrice=249.9, quantity=1]
Item sendo analisado: CartItem[product=USB Keyboard, productCategory=Peripherals, unitPrice=89.9, quantity=1]
Item final: CartItem[product=USB Keyboard, productCategory=Peripherals, unitPrice=89.9, quantity=1]
Item sendo analisado: CartItem[product=Gaming Mousepad, productCategory=Peripherals, unitPrice=54, quantity=1]
Item final: CartItem[product=Gaming Mousepad, productCategory=Peripherals, unitPrice=54, quantity=1]
Item sendo analisado: CartItem[product=Red Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
Item final: CartItem[product=Red Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
Item sendo analisado: CartItem[product=Black Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
Item final: CartItem[product=Black Type-C USB Cable, productCategory=Accessories, unitPrice=9.99, quantity=3]
Item sendo analisado: CartItem[product=Micro USB Cable, productCategory=Accessories, unitPrice=4.99, quantity=2]
Item final: CartItem[product=Micro USB Cable, productCategory=Accessories, unitPrice=4.99, quantity=2]
Item sendo analisado: CartItem[product=Micro USB Cable, productCategory=Accessories, unitPrice=5.99, quantity=4]
Item sendo analisado: CartItem[product=Smartphone, productCategory=Mobile, unitPrice=799.99, quantity=1]
Item final: CartItem[product=Smartphone, productCategory=Mobile, unitPrice=799.99, quantity=1]
Item sendo analisado: CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
Item final: CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
Item sendo analisado: CartItem[product=Bluetooth Headset, productCategory=Peripherals, unitPrice=60, quantity=1]
Exemplo de uso: count
Exemplo 6: retornar a quantidade de itens com
quantidade maior que 1
Resultado: 4
Exemplo de uso: max
Exemplo 7: buscar o item com o maior valor unitário
Resultado:
CartItem[product=Smartphone, productCategory=Mobile,
unitPrice=799.99, quantity=1]
Exemplo de uso: min
Exemplo 8: buscar o item com o menor valor unitário
Resultado:
CartItem[product=Micro USB Cable, productCategory=Accessories,
unitPrice=4.99, quantity=2]
Exemplo de uso: findAny
Exemplo 9: filtrar e retornar um item com valor unitário
maior que 100
Resultado (Optional):
CartItem[product=USB Mouse, productCategory=Peripherals,
unitPrice=219.9, quantity=1]
Exemplo de uso: findFirst
Exemplo 10: filtrar e retornar o primeiro item com valor
unitário maior que 100
Resultado (Optional):
CartItem[product=USB Mouse, productCategory=Peripherals,
unitPrice=219.9, quantity=1]
Exemplos de uso: allMatch,
noneMatch e anyMatch
Exemplo 11: retornar true caso todos, nenhum ou algum
item tenha preço unitário maior ou igual a 100
Resultados:
false, false, true
Exemplo de uso: reduce
Exemplo 12: retornar o subtotal de todos os itens
Resultado:
1627.57
Exemplo de uso: collect
Exemplo 13: transformar uma lista de CartItem em uma
lista de Strings
Resultado:
[USB Mouse, Bluetooth Mouse, USB Keyboard, Gaming Mousepad, Red
Type-C USB Cable, Black Type-C USB Cable, Micro USB Cable, Micro USB
Cable, Smartphone, Bluetooth Headset, Bluetooth Headset]
Exemplo de uso: collect
Exemplo 14: agrupar os itens por categoria e retornar
em um Map
Resultado:
{Accessories=[Red Type-C USB Cable, Black Type-C USB Cable, Micro
USB Cable, Micro USB Cable],
Peripherals=[USB Mouse, Bluetooth Mouse, USB Keyboard, Gaming
Mousepad, Bluetooth Headset, Bluetooth Headset],
Mobile=[Smartphone]}