Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement Iterator Design Pattern in Java ?.

Program to demonstrate how to implement Iterator Design Pattern in Java.

1. Song.java
package com.hubberspot.designpattern.behavioral.iterator;

public class Song {

    private String songName;
    private String singer;

    public Song(String songName, String singer) {
        super();
        this.songName = songName;
        this.singer = singer;
    }    

    public String getSongName() {
        return songName;
    }
    public String getSinger() {
        return singer;
    }

}



2. SongCollection.java
package com.hubberspot.designpattern.behavioral.iterator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SongCollection {

    private List< Song > songList = new ArrayList< Song >();

    public void addSong(Song song) {
        songList.add(song);
    }

    public Iterator< Song > getSongIterator() {

        return new SongIterator();
    } 

    private class SongIterator implements Iterator< Song > {

        int currentSongIndex = 0;

        @Override
        public boolean hasNext() {
            if (currentSongIndex >= songList.size()) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        public Song next() {

            return songList.get(currentSongIndex++);

        }

        @Override
        public void remove() {

            songList.remove(--currentSongIndex);

        }

    }
}



3. SongIterationDemo.java
package com.hubberspot.designpattern.behavioral.iterator;

import java.util.Iterator;

public class SongIterationDemo {

    public static void main(String[] args) {

        Song s1 = new Song("Rock You !!!" , "Eric Martin");        
        Song s2 = new Song("Someone Gone !!!" , "Neuone Derek");        
        Song s3 = new Song("Remember Me !!!" , "Justin Diebler");        

        SongCollection songCollection = new SongCollection();
        songCollection.addSong(s1);
        songCollection.addSong(s2);
        songCollection.addSong(s3);

        System.out.println("Displaying Song Info : \n");
        Iterator< Song > songIterator = songCollection.getSongIterator();

        while(songIterator.hasNext()) {
            Song song = songIterator.next();
            System.out.println("Song Name : " + song.getSongName());
            System.out.println("Song Singer : " + song.getSinger());
            System.out.println();
        }

        System.out.println("Removing Song and Displaying :\n");

        songIterator.remove();

        songIterator = songCollection.getSongIterator();

        while(songIterator.hasNext()) {
            Song song = songIterator.next();
            System.out.println("Song Name : " + song.getSongName());
            System.out.println("Song Singer : " + song.getSinger());
            System.out.println();
        }
    }
}



Output of the program : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot