JetBrains Academy: The utility class Collections - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Sorting sequences:
Implement a method for sorting a sequence of integer numbers in descending order. The method must modify the given sequence represented as a list.
Try to use standard methods for processing collections.
Do not output the elements of the list, just modify the collection.
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void sortInDescendingOrder(List<Integer> sequence) {
Collections.sort(sequence);
Collections.reverse(sequence);
// Collections.sort(sequence, Collections.reverseOrder());
}
/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final List<Integer> seq = Arrays.stream(scanner.nextLine().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList());
sortInDescendingOrder(seq);
seq.forEach(e -> System.out.print(e + " "));
}
}
Multiple swaps:
Write a program to sequentially swap elements by their indexes in a given list. Indexes of the elements start with 0 and always less than the size of the list.
Try to use standard methods for processing collections.
Input data format:
The first line contains elements of the list. The second line contains the number of swaps. Then follows the lines with descriptions of the swaps. Each line contains two numbers - indexes of swapped elements.
Output data format:
All elements of the modified list separated by spaces must be output in one line.
import java.util.*;
class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
List<String> list = new ArrayList<>();
Collections.addAll(list, scanner.nextLine().split("\\s+"));
int swaps = scanner.nextInt();
for(int i = 0; i < swaps; i++){
Collections.swap(list, scanner.nextInt(), scanner.nextInt());
}
list.forEach(elem -> System.out.printf("%s ", elem));
}
}
Rotating table:
Given a table of integer numbers. You should rotate rows of the table by the specified distance.
Try to use collections and standard methods for them.
Input data format:
The first line contains two numbers: a number of rows and a number of columns of the table.
The following lines describe rows of the table. In each row, all elements are separated by spaces.
The last line consists of one number - the distance for rotating.
Output data format:
Output the resulting table. Separate numbers by a single space in the output.
import java.util.*;
class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
List<List> table = new ArrayList<>();
int rows = scanner.nextInt();
int columns = scanner.nextInt();
for (int i = 0; i < rows; i++){
List<Integer> row = new ArrayList<>();
for (int j = 0; j < columns; j++){
row.add(scanner.nextInt());
}
table.add(row);
}
int rotations = scanner.nextInt();
Collections.rotate(table, rotations);
for (int i = 0; i < rows; i++){
List<Integer> row = table.get(i);
for (int j = 0; j < columns; j++){
System.out.printf("%d ", row.get(j));
}
System.out.println();
}
}
}
The first index of a sublist:
Write a program that read two sequences of numbers and output the starting position of the first occurrence and the last occurrence of the second list within the first list, or -1 if there is no such occurrence. Both numbers must be separated by a space.
import java.util.*;
class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
List<String> first = new ArrayList<>(Arrays.asList(scanner.nextLine().split("\\s+")));
List<String> second = new ArrayList<>(Arrays.asList(scanner.nextLine().split("\\s+")));
int firstOccurrence = Collections.indexOfSubList(first, second);
int lastOccurrence = Collections.lastIndexOfSubList(first, second);
System.out.printf("%d %d", firstOccurrence, lastOccurrence);
}
}
Frequency of characters:
Write a program that reads a sequence of characters and output the number of elements equal to the specified character.
Try to use the standard methods to write the program.
import java.util.*;
class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
List<String> letters = Arrays.asList(scanner.nextLine().split(" "));
int frequency = Collections.frequency(letters, scanner.next());
System.out.println(frequency);
}
}