NetRunner: Java Mastery Guide 1.0 Help

Expressões Lambda: Programação Funcional em Java

LAMBDA CONCEPTS ├── Syntax ├── Functional Interfaces ├── Method References └── Common Use Cases

Sintaxe Básica

Forma Padrão

// Sintaxe: (parâmetros) -> { corpo } Runnable r = () -> { System.out.println("Executando"); }; // Versão simplificada Comparator<String> c = (s1, s2) -> s1.length() - s2.length();

Interfaces Funcionais Comuns

Function<T,R>

Function<String, Integer> length = str -> str.length(); Integer size = length.apply("NetRunner");

Predicate

Predicate<String> isEmpty = str -> str.trim().isEmpty(); boolean result = isEmpty.test(" ");

Consumer

Consumer<String> print = msg -> System.out.println(msg); print.accept("Hello, Runner!");

Supplier

Supplier<LocalDateTime> now = () -> LocalDateTime.now(); LocalDateTime time = now.get();

Method References

Tipos de References

// Referência a método estático Function<String, Integer> parser = Integer::parseInt; // Referência a método de instância String str = "NetRunner"; Supplier<Integer> length = str::length; // Referência a construtor Supplier<ArrayList<String>> constructor = ArrayList::new;

Composição de Funções

Combining Functions

Function<String, String> upper = String::toUpperCase; Function<String, String> trim = String::trim; Function<String, String> combined = upper.andThen(trim);

Exemplo Prático

Stream com Lambda

List<String> hackers = Arrays.asList("Neo", "Trinity", "Morpheus"); hackers.stream() .filter(name -> name.length() > 3) .map(String::toUpperCase) .forEach(System.out::println);

Boas Práticas

  1. Mantenha Lambdas Simples

// Ruim button.setOnAction(e -> { // 20 linhas de código aqui }); // Bom button.setOnAction(this::handleClick);
  1. Use Method References

// Menos legível list.forEach(item -> System.out.println(item)); // Mais legível list.forEach(System.out::println);

Exercício

Implemente um sistema de filtro usando lambdas:

public class HackerFilter { public static List<Hacker> filterBySkill( List<Hacker> hackers, Predicate<Hacker> criteria ) { return hackers.stream() .filter(criteria) .collect(Collectors.toList()); } } // Uso List<Hacker> eliteHackers = filterBySkill( hackerList, h -> h.getSkillLevel() > 9000 );
26 June 2025