The method takes a list of names. Returns a string of the form "1. Ivan, 3. Peter ...", only with names on odd indices, respectively.
public static String constructString1(List<String> nameList) {
return IntStream.range(0, nameList.size())
.boxed()
.map(x -> x * 2 + 1 + ". " + nameList.get(x))
.collect(Collectors.joining(", "));
//.collect(Collectors.toList())
//.toString();
}
The method map list of string to uppercase and then sort descending.
public static List<String> upperCaseAndSort(List<String> list) {
return list.stream()
.map(x -> x.toUpperCase())
.sorted()
.collect(Collectors.toList());
}
Given and collection = Arrays.asList ("1, 2, 0", "4, 5")
From the collection get all the numbers listed, separated by commas from all the elements
public static List<String> separate(List<String> list) {
String separator = ", ";
return list.stream()
.map(x -> Arrays.asList(x.split(separator)))
.flatMap(x -> x.stream())
.collect(Collectors.toList());
}
Using Stream.iterate, make an infinite stream of random numbers — not by calling Math.random but by directly implementing a linear congruential generator. In such a generator, you start with x[0] = seed and then produce x[n + 1] = 1 (a x[n] + c) % m, for appropriate values of a, c, and m. You should implement a method with parameters a, c, m, and seed that yields a Stream. Try out a = 25214903917, c = 11, and m = 2^48.
void generatorStream(long seed, long a, long c, long m) {
Stream.iterate(seed, x -> (a * x + c) % m);
}
Write a method public static Stream zip(Stream first, Stream second) that alternates elements from the stream first and second, stopping when one of them runs out of elements.
//пока так
public static <T> Stream<T> zipV1(Stream<T> first, Stream<T> second) {
Iterator<T> i1 = first.iterator();
Iterator<T> i2 = second.iterator();
Stream<T> resultStream = Stream.empty();
while (i1.hasNext() & i2.hasNext()){
resultStream = Stream.concat(resultStream, Stream.of(i1.next(), i2.next()));
}
return resultStream;
}
It should be possible to concurrently collect stream results in a single ArrayList, instead of merging multiple array lists, provided it has been constructed with the stream's size, since concurrent set operations at disjoint positions are threadsafe. How can you achieve this?
@Test
public static void main(String[] args) {
assertEquals("1. Ivan, 3. Anna, 5. Micle, 7. Miki, 9. Nazar, 11. Mykola",
constructString1(Arrays.asList("Ivan", "Anna", "Micle", "Miki", "Nazar", "Mykola")));
assertEquals(Arrays.asList("HELLO","IS","MY","NAME","WORLD"),
upperCaseAndSort(Arrays.asList("hELLo", "worlD", "mY", "Name", "Is")));
assertEquals(Arrays.asList("1", "2", "0", "4", "5"),
separate(Arrays.asList("1, 2, 0", "4, 5")));
assertEquals(Arrays.asList("1", "2", "3", "4", "5", "6"),
zipV1(Stream.of("1", "3", "5"), Stream.of("2", "4", "6")).collect(Collectors.toList()));
}