import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class Scratch {
public static void main(String[] args) {
int[] ints = new int[]{13, 24, 24, 65, 3, 32, 27, 77, 88, 25, 74, 14};
batches(Arrays.stream(ints).boxed().toList(), 3)
.map(list -> list.stream().map(String::valueOf).collect(Collectors.joining(" ")))
.reduce((s1, s2) -> s1 + System.lineSeparator() + s2)
.ifPresent(System.out::println);
}
private static <T> Stream<List<T>> batches(List<T> source, int length) {
var size = source.size();
if (length <= 0 || size <= 0) {
return Stream.empty();
}
var fullChunks = (size - 1) / length;
return IntStream.range(0, fullChunks + 1).mapToObj(
n -> source.subList(n * length, n == fullChunks ? size : (n + 1) * length));
}
}
import java.util.ArrayList;
import java.util.List;
public class CatsDemo {
public static void main(String[] args) {
Cat васька = new Cat("Васька");
Cat мурзик = new Cat("мурзик");
ArrayList<Cat> cats = new ArrayList<>();
cats.add(васька);
cats.add(мурзик);
CatOwner catOwner = new CatOwner(cats);
}
}
class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CatOwner {
private List<Cat> cats;
public CatOwner(List<Cat> cats) {
this.cats = cats;
}
public List<Cat> getCats() {
return cats;
}
}
public String translate(String en) {
return Arrays.stream(en.split(" ")) // создаем стрим из массива. Массив - строка разбитая по пробелам
.map(dictionary::get) // Мапим каждый элемент из коллекции dictionary
.collect(Collectors.joining(" ")); // собираем стрим в одну строку, прибавляя пробел между элементами
}