title: Java TreeMap: Retrieving multiple values from a single key
body: <p>I'm trying to retrieve multiple values from a single key in a TreeMap. The idea is that each key will link to multiple values and should be searchable. Right now the trouble I'm running into is that when I am only able to get one value back from the key.</p>
  
  <p>The key is a String and the value is a custom object, called Song. Song contains multiple elements. The goal is to extract the lyrics, word by word, from each song and use each word as a key. The key then links to each Song (the value) that contains the key.</p>
  
  <p>I've searched StackOverFlow and the web in general for tips, and I've seen a few, but nothing that directly addresses my stumbling block. One idea I saw was to change the value into some kind of array or list. I may try that tomorrow when my brain is refreshed. </p>
  
  <p>Anyway, thanks in advance for any tips and advice. And yes, this is homework. No, I did not tag is because I was informed that the homework tag is no longer commonly in use.</p>
  
  <p>Code:</p>
  
  <pre><code>public class SearchByLyricsWords {
     private static Song[] songs;
  
     private static TreeMap&lt;String, Song&gt; lyricsTreeMap = new TreeMap&lt;String, Song&gt;();
  
     private static TreeSet&lt;String&gt; wordsToIgnoreTree = new TreeSet&lt;String&gt;();
     private static File wordsToIgnoreInput = new File("ignore.txt");
     private static String wordsToIgnoreString;
     private static String[] wordsToIgnoreArray;
  
     private Song[] searchResults;  // holds the results of the search
     private ArrayList&lt;Song&gt; searchList = new ArrayList&lt;Song&gt;();  
  
  public SearchByLyricsWords(SongCollection sc) throws FileNotFoundException {
  
    // Create a string out of the ignore.txt file
    Scanner scanInputFile = new Scanner(wordsToIgnoreInput);
    String ignoreToken = scanInputFile.next();
    ignoreToken.toLowerCase();
    wordsToIgnoreString = ignoreToken + " ";
  
    while (scanInputFile.hasNext()) {
       ignoreToken = scanInputFile.next();
       wordsToIgnoreString = wordsToIgnoreString + ignoreToken + " ";
    }
  
    // Split the string created from ignore.txt 
    wordsToIgnoreArray = wordsToIgnoreString.split("[^a-zA-Z]+");
  
    // Fill a TreeSet from the wordsToIgnoreArray
    for (int i = 0; i &lt; wordsToIgnoreArray.length; i++) {
       ignoreToken = wordsToIgnoreArray[i];
       wordsToIgnoreTree.add(ignoreToken);
    }
  
    // Fill TreeMap with lyrics words as the key, Song objects as the value
    songs = sc.getAllSongs();
  
    for (int j = 0; j &lt; songs.length; j++) {
       Song currentSong = songs[j];
       String lyrics = currentSong.getLyrics();         
       TreeSet&lt;String&gt; lyricsFound = new TreeSet&lt;String&gt;();
  
       String lyricsToken;
       String[] songLyricsArray;
       songLyricsArray = lyrics.split("[^a-zA-Z]+");
  
       for (int k = 0; k &lt; songLyricsArray.length; k++) {
          lyricsToken = songLyricsArray[k];
  
          if (lyricsToken.length() &lt;= 1) {
             continue;
          }
  
          lyricsFound.add(lyricsToken);
       }
  
       lyricsFound.removeAll(wordsToIgnoreTree);
  
       Iterator&lt;String&gt; iterator = lyricsFound.iterator();
  
       while(iterator.hasNext()) {
          String currentWord = (String)iterator.next();
          lyricsTreeMap.put(currentWord, currentSong);
       }
  
       //System.out.println(lyricsTreeMap); // testing only
    }
  }
  
  
  public Song[] search(String lyricsWords) {
  
    lyricsWords = lyricsWords.toLowerCase();
    TreeSet&lt;String&gt; searchTree = new TreeSet&lt;String&gt;();
    String searchToken;
    String[] lyricsWordsSearch = lyricsWords.split("[^a-zA-Z]+");
  
    for (int l = 0; l &lt; lyricsWordsSearch.length; l++) {
       searchToken = lyricsWordsSearch[l];
  
       if (searchToken.length() &lt;= 1) {
          continue;            
       }
       searchTree.add(searchToken);
    }
    searchTree.removeAll(wordsToIgnoreTree);
  
    Iterator&lt;String&gt; searchIterator = searchTree.iterator();
  
    while(searchIterator.hasNext()) {
       String currentSearchWord = (String)searchIterator.next();
       Collection&lt;Song&gt; lyricsTreeCollection = lyricsTreeMap.values();
  
       while (lyricsTreeMap.containsKey(currentSearchWord) == true) {
  
          Iterator collectionIterator = lyricsTreeCollection.iterator();
  
          while(collectionIterator.hasNext() &amp;&amp; collectionIterator.next() == currentSearchWord) {
  
             Song searchSong = lyricsTreeMap.get(currentSearchWord);
             searchList.add(searchSong);          
          }
       }           
    }
    searchResults = searchList.toArray(new Song[searchList.size()]);
  
    Arrays.sort(searchResults);
  
    return searchResults;
  }
  </code></pre>