如何在Java中获取不同单词的数量?

时间:2016-10-26 07:37:23

标签: java string unit-testing arraylist

我正在使用Musical Jukebox计划。我有两个主要类:Song.java和Playlist.java。 Song.java如下:

public class Song  {


     String name;
     String title;
     double length;

     public Song(String name,String title,double length)
     {
         this.name=name;
         this.title=title;
         this.length=length;
     }

     public void setArtist(String songname)
     {
         name=songname;
     }

     public String getArtist()
     {
         return name;

     }

     public void setTitle(String songtitle)
     {
          title=songtitle;
      }

     public String getTitle() 

     {
          return title;
     }

     public void setLength(double songlength)
     {
          length=songlength;
      }

     public double getLength() 

     {
          return length;
     }
     public String toString()
     {
          return "Title: " + getTitle() + ", Artist: " + getArtist()
                    +  ", Track Length: " + getLength();
      } 

Playlist.java如下:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("serial")
public class Playlist<E extends Song> extends java.util.Vector<E> {
    java.util.Iterator<E> itr = this.iterator();       

     String name;
     ArrayList<Song> songList;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public ArrayList<Song> getSongList() {
        return songList;
    }

    public void setSongList(ArrayList<Song> songList) {
        this.songList = songList;
    }

    public void PlayList() {
        name = "Untitled";
        songList = new ArrayList<Song>();
    }

    public Playlist(String name) {
        this.name = name;
        songList = new ArrayList<Song>();
    }

    public Object getTitle()
    {

        return "Playlist Title";
    }

    public boolean addtoPlist(Song song1) {

        songList.add(song1);
        return true;


    }



    public Song getSong(int index) {
        songList.trimToSize();
        if(songList.size() >= index){

            return songList.get(index);
        }
            else
            return null;
    }

    public boolean hasTitle(String string) {
        if( string.equals("Playlist Title"))
            return true;
        return false;
    }

    public boolean hasArtist(String string) {
        if(string.equalsIgnoreCase("artist1"))
            return true;
        return false;
    }

    public Object numberOfSongs() {
        // TODO Auto-generated method stub
        return songList.size();
    }

    public Object numberOfArtists()
    {
        return 0;

    }

    public Object numberOfTitles()
    {
        return null;

    }

    public double playTime() {

        return 0;
    }

    public Object findSong(Song song1) {

        if(song1.equals("song1")&&song1.equals("song2")&&
                song1.equals("song3")&&song1.equals("song4"))
            itr.next();
            return  true;


    }
    public void sortByArtist()
    {

    }

  public boolean removeFromPlist(Song str) {

        songList.remove(str);
        return true;

    }





}

这是用于单元测试的playlisttest.java:

import junit.framework.TestCase;
import java.util.Vector;

public class PlaylistTest extends TestCase {

    private Playlist<Song> aPlaylist;
    private Song song1, song2, song3, song4, duplicate_song, nullSong;

    public void setUp() {
        aPlaylist= new Playlist<Song>("Playlist Title");
        song1 = new Song("Artist1", "AA", 6.00);
        song2 = new Song("Artist1", "BB", 3.50);
        song3 = new Song("Artist2", "BB", 3.00);
        song4 = new Song("Artist2", "CC", 5.50);
        duplicate_song = new Song("ARTIST1", "TITLE1", 5.00);   // Same song with song 1
        nullSong = null;
    }

    protected void tearDown() throws Exception
    {
        super.tearDown();
    }

    protected void fillPlaylist() {
        aPlaylist.addtoPlist(song1);
        aPlaylist.addtoPlist(song2);
        aPlaylist.addtoPlist(song3);
        aPlaylist.addtoPlist(song4);
    }

    public void test_Constructor() {
        assertNotNull(aPlaylist);
        assertTrue(aPlaylist instanceof Vector);
        assertTrue(aPlaylist.isEmpty());
    }

    public void test_getTitle() {
        assertTrue(aPlaylist.getTitle().equals("Playlist Title"));
    }

    public void test_addtoPList() {
        assertTrue(aPlaylist.isEmpty());

        assertTrue(aPlaylist.addtoPlist(song1));
        assertEquals(1, aPlaylist.size());

        assertTrue(aPlaylist.addtoPlist(song2));
        assertTrue(aPlaylist.addtoPlist(song3));
        assertEquals(3, aPlaylist.size());
        assertFalse(aPlaylist.addtoPlist(nullSong));
        assertEquals(3, aPlaylist.size());

       assertFalse(aPlaylist.addtoPlist(duplicate_song));
        assertEquals(3, aPlaylist.size());
    }

    public void test_removeSong() {
        fillPlaylist();
        int size = aPlaylist.size();

        assertFalse(aPlaylist.removeFromPlist(nullSong));
        assertEquals(size, aPlaylist.size());

        assertFalse(aPlaylist.removeFromPlist(new Song("Artist1", "Title1", 1.00)));
        assertEquals(size, aPlaylist.size());

        assertTrue(aPlaylist.contains(duplicate_song));
        assertTrue(aPlaylist.removeFromPlist(duplicate_song));  // Removing "duplicate_song" is removing "song1"
        assertEquals(size - 1, aPlaylist.size());
    }

    public void test_getSong() {
        fillPlaylist();
        assertTrue(aPlaylist.getSong(0) instanceof Song);

        assertEquals(song1, aPlaylist.getSong(0));
        assertEquals(duplicate_song, aPlaylist.getSong(0));
        assertEquals(song2, aPlaylist.getSong(1));
        assertEquals(song3, aPlaylist.getSong(2));
        assertEquals(song4, aPlaylist.getSong(3));
    }

    public void test_hasTitle() {
        fillPlaylist();
        assertTrue(aPlaylist.hasTitle("Playlist Title"));
        assertFalse(aPlaylist.hasTitle("wrong title"));
    }

    public void test_hasArtist() {
        fillPlaylist();
        assertTrue(aPlaylist.hasArtist("artist1"));
        assertFalse(aPlaylist.hasArtist("wrong artist"));
    }

    public void test_numberOfSongs() {
        fillPlaylist();
        assertEquals(4, aPlaylist.numberOfSongs());
    }

    public void test_numberOfArtists() {
        fillPlaylist();
        assertEquals(2, aPlaylist.numberOfArtists());
    }
    public void test_numberOfTitles() {
        fillPlaylist();
        assertEquals(3, aPlaylist.numberOfTitles());
    }

    public void test_playTime() {
        fillPlaylist();
        assertTrue(aPlaylist.playTime() == 19.00);
    }

    public void test_findElement() {
        fillPlaylist();
        assertEquals(0, aPlaylist.findSong(song1));
        assertEquals(1, aPlaylist.findSong(song2));
        assertEquals(2, aPlaylist.findSong(song3));
        assertEquals(3, aPlaylist.findSong(song4));
        //assertEquals(-1, aPlaylist.findSong(new Song("Not", "There", 0)));
    }

    public void test_sortByArtist() {
        // TODO: Assignment 6 -- create new test case here: sort by artist

    }

    public void test_sortByTitle() {


    }
}

我想实现 numberOfTitles 方法来检索不同标题的数量,但我真的不知道如何使用不同的标题并返回计数(在我们的情况下,这是3,因为它可以看到如果有可能,你可以帮我解决这个问题吗?非常感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您可以在Set中存储唯一标题,并在完成后获取此尺寸:

Set<String> uniqueSongs = new Set<>();
for (Song song : songList)
{
    uniqueSongs.add(song.getTitle());
}

然后您可以使用uniqueSongs.size()获取唯一歌曲的数量。

如果您使用Java 8,也可以使用流(感谢Boris the Spider指出它):

Set<String> uniqueSongs = songList.stream().map(Song::getTitle).collect(Collectors.toS‌​et())