NetRunner: Java Mastery Guide 1.0 Help

Streams API: Processamento de Dados Fluente

STREAMS API ├── Creation ├── Intermediate Ops ├── Terminal Ops └── Parallel Streams

Criando Streams

Métodos de Criação

// De Collection List<String> list = Arrays.asList("Neo", "Trinity"); Stream<String> stream = list.stream(); // Stream direto Stream<String> direct = Stream.of("Morpheus", "Tank"); // Stream infinito Stream<Integer> infinite = Stream.iterate(0, n -> n + 1);

Operações Intermediárias

Filter

List<String> hackers = Arrays.asList("Neo", "Trinity", "Morpheus"); hackers.stream() .filter(name -> name.length() > 3) .collect(Collectors.toList());

Map

List<Integer> lengths = hackers.stream() .map(String::length) .collect(Collectors.toList());

FlatMap

List<List<String>> nested = Arrays.asList( Arrays.asList("Neo", "Trinity"), Arrays.asList("Morpheus", "Tank") ); List<String> flat = nested.stream() .flatMap(List::stream) .collect(Collectors.toList());

Operações Terminais

Collect

// Para Lista List<String> list = stream.collect(Collectors.toList()); // Para Set Set<String> set = stream.collect(Collectors.toSet()); // Para Map Map<String, Integer> map = stream.collect( Collectors.toMap( s -> s, String::length ) );

Reduce

Optional<Integer> sum = Stream.of(1, 2, 3) .reduce((a, b) -> a + b); int total = Stream.of(1, 2, 3) .reduce(0, Integer::sum);

Streams Paralelos

Processamento Paralelo

// Stream paralelo de Collection List<String> result = hackers.parallelStream() .filter(name -> name.length() > 3) .collect(Collectors.toList()); // Converter para paralelo Stream<String> parallel = stream.parallel();

Exemplos Práticos

Análise de Dados

class HackerStats { public static Map<String, Long> analyzeSkills(List<Hacker> hackers) { return hackers.stream() .flatMap(h -> h.getSkills().stream()) .collect(Collectors.groupingBy( skill -> skill, Collectors.counting() )); } }

Pipeline Complexo

List<Hacker> eliteSquad = hackers.stream() .filter(h -> h.getLevel() > 50) .sorted(Comparator.comparing(Hacker::getSkillLevel).reversed()) .limit(5) .collect(Collectors.toList());

Boas Práticas

  1. Use Parallel com Cautela

// Nem sempre mais rápido! boolean anyMatch = hugeList.parallelStream() .anyMatch(predicate);
  1. Evite Estado Mutável

// Ruim List<String> results = new ArrayList<>(); stream.forEach(results::add); // Bom List<String> results = stream.collect(Collectors.toList());

Exercício

Implemente um sistema de análise de logs:

public class LogAnalyzer { public static Map<String, Long> analyzeErrors( List<LogEntry> logs ) { return logs.stream() .filter(log -> log.getLevel() == Level.ERROR) .collect(Collectors.groupingBy( LogEntry::getErrorCode, Collectors.counting() )); } }
26 June 2025