title: Java collection
body: <p>I need to solve this assignment. I am wondering what collection is best to use in this case...Also, I am tempted to give up on collections and go for a class to hold the list. Please let me know how to handle this.</p>
  
  <p>></p>
  
  <pre><code>Read a file (name passed in as 1st argument to program)
  &gt;
  &gt;Each line of the file has a category, a space, and a sub-category (sub-category can have whitespace within it)
  &gt;
  &gt;Want to:
  &gt;
  &gt; - Only process the pair (category, sub-category) once
  &gt;   - If a pair appears twice, ignore the second one
  &gt; - Keep track of the order of the first occurrence of each pair
  &gt; - Keep track of the count for each category
  &gt; - Legal category values are: PERSON PLACE ANIMAL COMPUTER OTHER
  &gt;   - Illegal category values should be ignored
  &gt;
  &gt;Output is:
  &gt;
  &gt;     CATEGORY     COUNT
  &gt;     PERSON
  &gt;     PLACE
  &gt;     ANIMAL
  &gt;     COMPUTER
  &gt;     OTHER
  &gt;
  &gt;Ordered list of input
  &gt;
  &gt; - Category output should be in the order shown
  &gt; - If there are no items in the file for a specified category, it should be shown with a count of 0
  &gt;
  &gt;Please follow good software engineering practices.  In addition to a working program, unit tests should be produced.
  &gt;
  &gt;Example:
  &gt;input file:
  &gt;
  &gt;     PERSON Bob Jones
  &gt;     PLACE Washington
  &gt;     PERSON Mary
  &gt;     COMPUTER Mac
  &gt;     PERSON Bob Jones
  &gt;     OTHER Tree
  &gt;     ANIMAL Dog
  &gt;     PLACE Texas
  &gt;     FOOD Steak
  &gt;     ANIMAL Cat
  &gt;
  &gt;output:
  &gt;
  &gt;     CATEGORY     COUNT
  &gt;     PERSON       2
  &gt;     PLACE        2
  &gt;     ANIMAL       2
  &gt;     COMPUTER     1
  &gt;     OTHER        1
  &gt; 
  &gt;     PERSON Bob Jones
  &gt;     PLACE Washington
  &gt;     PERSON Mary
  &gt;     COMPUTER Mac
  &gt;     OTHER Tree
  &gt;     ANIMAL Dog
  &gt;     PLACE Texas
  &gt;     ANIMAL Cat
  &gt;
  </code></pre>
  
  <p>This is what I have so far.... However, the hashmap is getting overwritten which I believe is expected. Bob Jones read by the first line is replaced by Mary later. I am wondering if LinkedHashMap will be able to solve this problem or I need to go for a different collection. Thanks!</p>
  
  <pre><code>public void printOutput(String fileName){
    String category = null;
    String [] legalCategory ={"PERSON", "PLACE", "ANIMAL", "COMPUTER", "OTHER"};
    List&lt;String&gt; mapList = new ArrayList&lt;String&gt;();
    Map&lt;String, String&gt; categoryMap = new LinkedHashMap&lt;String, String&gt;();
  
  
    try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
        StringBuilder sb = new StringBuilder();
        String line = null;
  
        line = br.readLine();
        sb.append(line);
        sb.append(System.lineSeparator());
  
        while (line != null) {                              
            StringTokenizer st = new StringTokenizer(line);
                 while (st.hasMoreElements()) {
                        category = st.nextToken();
                        for (String element : legalCategory) {
                            if (category.equalsIgnoreCase(element)) {
                               categoryMap.put(category, line.substring(category.length(), line.length()));                                   
                               break;
                            }
                        }
  
                 }            
                 line = br.readLine();
        }
  
        String everything = sb.toString();
    }
    catch(Exception e){
           e.printStackTrace();
    }
  </code></pre>
  
  <p>===========================================================================</p>
  
  <p>Here is the full program. Mohamed's answer helped a lot!</p>
  
  <pre><code>public void printOutput(String fileName){
                String category = null;
                String [] legalCategory ={"PERSON", "PLACE", "ANIMAL", "COMPUTER", "OTHER"};
                List&lt;String&gt; mapList = new ArrayList&lt;String&gt;();
                Map&lt;String, String&gt; categoryMap = new LinkedHashMap&lt;String, String&gt;();
                List&lt;Category&gt; categoryList = new ArrayList&lt;Category&gt;();
                boolean keyFound = false;
  
                try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
                    StringBuilder sb = new StringBuilder();
                    String line = null;
  
                    line = br.readLine();
                    sb.append(line);
                    sb.append(System.lineSeparator());
  
                    while (line != null) {                       
                        StringTokenizer st = new StringTokenizer(line);
                        keyFound = false;
                        for(Category indCategory:categoryList){
                              if(indCategory.getCategoryKey().equalsIgnoreCase(line))
                                           keyFound=true;
                        }                    
                             while (st.hasMoreElements()) {
                                    category = st.nextToken();
                                    for (String element : legalCategory) {
                                        if (category.equalsIgnoreCase(element)) {
                                            if(!keyFound){                                              
                                                  categoryList.add(new Category(line, category, line.substring(category.length(), line.length())));
                                           //categoryMap.put(category, line.substring(category.length(), line.length()));                                   
                                                  break;
                                            }
                                        }
                                    }
  
                             }            
                             line = br.readLine();
                    }
  
                    String everything = sb.toString();
  
                    for(Category indCategory:categoryList){                  
                       System.out.println(indCategory.getCategoryItem() + "   " + indCategory.getCategorySubItem());              
                 }
  
  
                   int occurrences = 0;
                   System.out.println("Category" + "\t" +    "Count" ); 
                   for(String categoryItem:legalCategory){
                          occurrences = 0;
                          for(Category indCategory:categoryList){                   
                              //System.out.println(indCategory.getCategoryItem() + "   " + indCategory.getCategorySubItem());
                              if(categoryItem.equalsIgnoreCase(indCategory.getCategoryItem()))
                                     occurrences +=1;
                        }                    
                          System.out.println(categoryItem + "\t" +   occurrences );
                   }
  
                  // System.out.println("PERSON" + "\t" +    Collections.frequency(categoryList, "PERSON") + categoryList.size() );
  
                }
                catch(Exception e){
                       e.printStackTrace();
                }
         }
  
         public class Category{
  
                String categoryKey;
                String categoryItem;
                String categorySubItem;
                String occurrence;
  
                public Category(String categoryKey, String categoryItem, String categorySubItem){
                       this.categoryKey = categoryKey;
                       this.categoryItem = categoryItem;
                       this.categorySubItem = categorySubItem;
                }
                /**
                * @return the categoryKey
                */
                public String getCategoryKey() {
                       return categoryKey;
                }
                /**
                * @param categoryKey the categoryKey to set
                */
                public void setCategoryKey(String categoryKey) {
                       this.categoryKey = categoryKey;
                }
                /**
                * @return the categoryItem
                */
                public String getCategoryItem() {
                       return categoryItem;
                }
                /**
                * @param categoryItem the categoryItem to set
                */
                public void setCategoryItem(String categoryItem) {
                       this.categoryItem = categoryItem;
                }
                /**
                * @return the categorySubItem
                */
                public String getCategorySubItem() {
                       return categorySubItem;
                }
                /**
                * @param categorySubItem the categorySubItem to set
                */
                public void setCategorySubItem(String categorySubItem) {
                       this.categorySubItem = categorySubItem;
                }
  }
  </code></pre>