[BAEL-6339] custom iterator with testcases (#14038)

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar 2023-05-18 02:02:04 +05:30 committed by GitHub
parent 06bf1155e8
commit da265d4f81
6 changed files with 328 additions and 4 deletions

View File

@ -1,10 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-5</artifactId>
<name>core-java-collections-5</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<parent>

View File

@ -0,0 +1,39 @@
package com.baeldung.customiterators;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class CustomMovieIterator implements Iterator<Movie> {
private int currentIndex;
private final List<Movie> list;
public CustomMovieIterator(List<Movie> list) {
this.list = list;
this.currentIndex = 0;
}
@Override
public boolean hasNext() {
while (currentIndex < list.size()) {
Movie currentMovie = list.get(currentIndex);
if (isMovieEligible(currentMovie)) {
return true;
}
currentIndex++;
}
return false;
}
@Override
public Movie next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return list.get(currentIndex++);
}
private boolean isMovieEligible(Movie movie) {
return movie.getRating() >= 8;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.customiterators;
public class Movie {
private String name;
private String director;
private float rating;
public Movie(String name, String director, float rating) {
this.name = name;
this.director = director;
this.rating = rating;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
}

View File

@ -0,0 +1,139 @@
package com.baeldung.customiterators;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* Please note that this class has been added for representation purposes of how a custom collection and its iterator would be.
* This does not have working code.
*/
public class MyList<E> implements List<E> {
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public boolean add(E e) {
return false;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public Iterator<E> iterator() {
return new MyListIterator();
}
private class MyListIterator implements Iterator<E> {
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
return null;
}
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] a) {
return null;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean addAll(Collection<? extends E> c) {
return false;
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public void clear() {
}
@Override
public E get(int index) {
return null;
}
@Override
public E set(int index, E element) {
return null;
}
@Override
public void add(int index, E element) {
}
@Override
public E remove(int index) {
return null;
}
@Override
public int indexOf(Object o) {
return 0;
}
@Override
public int lastIndexOf(Object o) {
return 0;
}
@Override
public ListIterator<E> listIterator() {
return null;
}
@Override
public ListIterator<E> listIterator(int index) {
return null;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
return null;
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.customiterators;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class PalindromIterator implements Iterator<String> {
private final List<String> list;
private int currentIndex;
public PalindromIterator(List<String> list) {
this.list = list;
this.currentIndex = 0;
}
@Override
public boolean hasNext() {
while (currentIndex < list.size()) {
String currString = list.get(currentIndex);
if (isPalindrome(currString)) {
return true;
}
currentIndex++;
}
return false;
}
@Override
public String next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return list.get(currentIndex++);
}
private boolean isPalindrome(String input) {
for (int i = 0; i < input.length() / 2; i++) {
if (input.charAt(i) != input.charAt(input.length() - i - 1)) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.customiterators;
import static org.junit.Assert.assertEquals;
import java.util.Iterator;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class IteratorsUnitTest {
@Test
public void givenListOfStrings_whenIteratedWithDefaultIterator() {
List<String> listOfStrings = List.of("hello", "world", "this", "is", "a", "test");
Iterator<String> iterator = listOfStrings.iterator();
Assert.assertTrue(iterator.hasNext());
assertEquals(iterator.next(), "hello");
}
@Test
public void givenListOfStrings_whenPalindromIterator_thenOnlyPalindromes() {
List<String> listOfStrings = List.of("oslo", "madam", "car", "deed", "wow", "test");
PalindromIterator palindromIterator = new PalindromIterator(listOfStrings);
int count = 0;
while (palindromIterator.hasNext()) {
palindromIterator.next();
count++;
}
assertEquals(count, 3);
}
@Test
public void givenMovieList_whenMovieIteratorUsed_thenOnlyHighRatedMovies() {
List<Movie> movies = getMovies();
CustomMovieIterator movieIterator = new CustomMovieIterator(movies);
int count = 0;
while (movieIterator.hasNext()) {
movieIterator.next();
count++;
}
assertEquals(4, movies.size());
assertEquals(2, count);
}
private List<Movie> getMovies() {
Movie movie1 = new Movie("The Dark Knight", "Nolan", 10);
Movie movie2 = new Movie("Avatar", "Cameron", 9);
Movie movie3 = new Movie("Tenet", "Nolan", 7);
Movie movie4 = new Movie("Extraction", "Hargrave", 5);
return List.of(movie1, movie2, movie3, movie4);
}
}