🖋️ 자바 개발할 때 유용한 내장 기능
자바 Stream API
Filter
List<String> names = Arrays.asList("John", "Jane", "Jack");
List<String> filteredNames = names.stream()
.filter(name -> name.startsWith("J"))
.collect(Collectors.toList());
Map
List<String> names = Arrays.asList("John", "Jane", "Jack");
List<String> upperCaseNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
Reduce
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.reduce(0, Integer::sum);
Optional
Optional<String> optional = Optional.of("Hello, World!");
optional.ifPresent(System.out::println);
String value = optional.orElse("Default Value");
CompletableFuture
CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(result -> result + ", World!")
.thenAccept(System.out::println);
Files
Path path = Paths.get("example.txt");
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
Files.write(path, Arrays.asList("Line 1", "Line 2"), StandardOpenOption.APPEND);
Collections
List<String> list = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));
Collections.sort(list);
list.forEach(System.out::println);
Collections.reverse(list);
list.forEach(System.out::println);
Locale
Locale locale = new Locale("en", "US");
System.out.println(locale.getDisplayLanguage());
System.out.println(locale.getDisplayCountry());
UUID
UUID uuid = UUID.randomUUID();
System.out.println(uuid.toString());
Pattern
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("12345");
if (matcher.matches()) {
System.out.println("Matches!");
}
Lambda Expressions
List<String> list = Arrays.asList("a", "b", "c");
list.forEach(item -> System.out.println(item));
list.stream()
.filter(item -> item.equals("b"))
.forEach(System.out::println);
Map Struct
@Mapper
public interface UserMapper {
UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);
@Mapping(source = "name", target = "userName")
UserDTO userToUserDTO(User user);
}
User user = new User("John", "Doe");
UserDTO userDTO = UserMapper.INSTANCE.userToUserDTO(user);
Lombok
@Data
@AllArgsConstructor
public class User {
private String firstName;
private String lastName;
}
User user = new User("John", "Doe");
System.out.println(user.getFirstName());