JetBrains Academy: Iterator and Iterable - Kamil-Jankowski/Learning-JAVA GitHub Wiki
Hip-Hop:
You need to implement two methods using ListIterator
's.
1. The method iterateOverList
should iterate over the elements from the beginning to the end and add "Hop" after each "Hip".
2. The method printList
should print all elements of the list (on a new line).
Please, use ListIterator
's to solve this problem.
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void iterateOverList(ListIterator<String> iter) {
while (iter.hasNext()){
String word = iter.next();
if("Hip".equals(word)){
iter.add("Hop");
}
}
}
public static void printList(ListIterator<String> iter) {
while (iter.hasNext()){
System.out.println(iter.next());
}
}
/* Do not change code below */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<String> list = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
iterateOverList(list.listIterator());
printList(list.listIterator());
}
}
No "J":
Implement a method that does the following algorithm:
1. creates List<String>
from a given array of strings
2. using ListIterator
, removes all items not starting with "J" and items beginning with "J" replace with the same elements but without "J", (e.g., JFrame
-> Frame
)
3. prints all the remaining elements in the reverse order
import java.util.*;
public class Main {
public static void processIterator(String[] array) {
List<String> list = new ArrayList<>(Arrays.asList(array));
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()){
String word = iterator.next();
if(word.startsWith("J")){
iterator.set(word.substring(1));
} else {
iterator.remove();
}
}
while (iterator.hasPrevious()){
System.out.println(iterator.previous());
}
}
/* Do not change code below */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
processIterator(scanner.nextLine().split(" "));
}
}
Finding the max number:
Implement a method that returns the max number in a collection using the iterator. It's guaranteed the collection always has at least one element.
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static int findMaxByIterator(Iterator<Integer> iterator) {
int max = Integer.MIN_VALUE;
while(iterator.hasNext()){
int number = iterator.next();
max = number > max ? number : max;
}
return max;
}
/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final List<Integer> list = Arrays.stream(scanner.nextLine().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList());
System.out.println(findMaxByIterator(list.iterator()));
}
}
Reversed list:
Implement a method that takes an instance of ListIterator
and creates a new list in the reversed order. The given iterator is on the first element of a list. Use only the iterator to write a solution.
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static <T> List<T> createReversedListByIterator(ListIterator<T> iterator) {
List<T> list = new ArrayList<>();
while(iterator.hasNext()){
iterator.next();
}
while (iterator.hasPrevious()){
list.add(iterator.previous());
}
return list;
}
/* Do not change the code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final List<Integer> list = Arrays.stream(scanner.nextLine().split("\\s+"))
.map(Integer::parseInt)
.collect(Collectors.toList());
createReversedListByIterator(list.listIterator())
.forEach(e -> System.out.print(e + " "));
}
}
Remove elements of a collection:
Implement a method that takes an iterator from a collection and removes all elements from the collection greater than the given value. If an element is equal to the value, do not remove it.
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void removeElementsGreaterThanValue(Iterator<Long> iterator, Long val) {
while (iterator.hasNext()){
long number = iterator.next();
if (number > val){
iterator.remove();
}
}
}
/* Do not change code below */
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final List<Long> list = Arrays.stream(scanner.nextLine().split("\\s+"))
.map(Long::parseLong)
.collect(Collectors.toList());
final Long edge = Long.parseLong(scanner.nextLine());
removeElementsGreaterThanValue(list.iterator(), edge);
list.forEach(e -> System.out.print(e + " "));
}
}
Task:
Given a class named Range
. It represents a range from A (inclusive) to B (exclusive). The class implements the interface Iterable
, therefore, an instance of Range
can be used in the for-each loop, like below:
Range range = new Range(2, 6);
for (Long val : range) {
System.out.println(val);
}
must print:
2
3
4
5
Write a body of the overridden method iterator
so that the code works correctly.
class Range implements Iterable<Long> {
private long fromInclusive;
private long toExclusive;
public Range(long from, long to) {
this.fromInclusive = from;
this.toExclusive = to;
}
@Override
public Iterator<Long> iterator() {
return new RangeIterator();
}
private class RangeIterator implements Iterator<Long>{
private long cursor;
public RangeIterator(){
this.cursor = Range.this.fromInclusive;
}
@Override
public boolean hasNext(){
return this.cursor < Range.this.toExclusive;
}
@Override
public Long next(){
if (this.hasNext()){
long currentElement = cursor;
cursor++;
return currentElement;
}
throw new java.util.NoSuchElementException();
}
@Override
public void remove(){
throw new UnsupportedOperationException();
}
}
}