Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 Overview This assignment is about hash tables. Unlike the previous assignments

ID: 3831441 • Letter: 1

Question

1 Overview This assignment is about hash tables. Unlike the previous assignments, you do not have to imple- ment your own data structures. Instead, you are required to use the data structures provided by Java. Specifically, the map data structures provided by Java. These are very similar to the hash tables we have studied in class. You will be using these data structures to load data and provide it back in a form requested by the program user. 2 Tasks 2.1 Static Use of Maps in Java Create a class called FileToMapLoader. This class should be able to read a file, where each line has two comma-separated values. The first value is a person's name and the second is a phone number. You can use the sample file sample.csv provided with this assignment. This class should have the following constant: public static final String FILEPATH absolute path of file If should also have a nested static emum called Order, with three values: NAME, PHONE, and ORIGINAL. Finally, you must have a main method in this class which does the following: (i) Loads the data from the file into a map provided by Java (i.e., a class that implements the ap interface. util. Java (ii) Asks the user to specify one of the three orderings "name", "phone", or "original" (iii) Prints the data in the order specified by the user. The name and phone" options should print out the lines in ascending order of the names or phone numbers, respectively. The original" option should simply print the data in the same order as the input file So, this sounds just like another sorting assignment? Except for one little thing: you are NOT allowed to use any explicit sorting You can assume that the names are unique and every person has only one phone number. Also, you can assume that no two people have the same phone number. The programming for this is very simple, but you have to read up on the different types of maps provided by Java to print the correct output. For this, consult http://docs oracle com/javase http://docs. oracle com/javase The entire assignment hinges on your understanding of Java's maps and how to use them. In one of the three orderings, you will have to write a comparator.

Explanation / Answer

First prob:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class MapOrder {
   public static enum Order{NAME,PHONE,ORIGINAL}
   public static void main(String args[]){
       final String path="C:\Users\IBM_ADMIN\workspace\testnew\src\chegg\sample.csv";
       Map<String,String> hmap=new HashMap<String,String>();
       try{
       FileInputStream fis=new FileInputStream(path);
       Scanner sc=new Scanner(fis);
       while(sc.hasNext()){
           String line=sc.next();
           hmap.put(line.split(",")[0], line.split(",")[1]);
       }
       for(Map.Entry<String, String> entry:hmap.entrySet()){
           System.out.println(entry.getKey()+" ** "+entry.getValue());
       }
       System.out.println("Enter ordering type :");
       sc=new Scanner(System.in);
       String ord=sc.next();
       if(ord.equals("NAME")){
           hmap=sortByName(hmap);
       }else if(ord.equals("PHONE")){
           hmap=sortByPhone(hmap);
       }
       for(Map.Entry<String, String> entry:hmap.entrySet()){
           System.out.println(entry.getKey()+" ** "+entry.getValue());
       }
       }catch(FileNotFoundException fe){
           fe.printStackTrace();
       }
   }
   public static Map<String, String> sortByPhone( Map<String, String> map )
{
    List<Map.Entry<String, String>> list =
        new ArrayList<Map.Entry<String,String>>( (Collection<? extends Entry<String, String>>) map.entrySet() );
    Collections.sort( list, new Comparator<Map.Entry<String, String>>()
    {
       @Override
       public int compare(Entry<String, String> o1, Entry<String, String> o2) {
           // TODO Auto-generated method stub
           return o1.getValue().compareTo(o2.getValue());
          
       }
    } );
    Map<String, String> result = new LinkedHashMap<>();
    for (Map.Entry<String, String> entry : list)
    {
        result.put( entry.getKey(), entry.getValue() );
    }
    return result;
}
   public static Map<String, String> sortByName( Map<String, String> map )
{
    List<Map.Entry<String, String>> list =
        new ArrayList<Map.Entry<String,String>>( (Collection<? extends Entry<String, String>>) map.entrySet() );
    Collections.sort( list, new Comparator<Map.Entry<String, String>>()
    {
       @Override
       public int compare(Entry<String, String> o1, Entry<String, String> o2) {
           // TODO Auto-generated method stub
           return o1.getKey().compareTo(o2.getKey());
          
       }
    } );
    Map<String, String> result = new LinkedHashMap<>();
    for (Map.Entry<String, String> entry : list)
    {
        result.put( entry.getKey(), entry.getValue() );
    }
    return result;
}
}

Second Problem:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;

public class WordCount {
   public static void main(String args[]){
       final String path="C:\Users\IBM_ADMIN\workspace\testnew\src\chegg\input2.txt";
       Map<String,Integer> hmap=new HashMap<String,Integer>();
       try{
           FileInputStream fis=new FileInputStream(path);
           Scanner sc=new Scanner(fis);
           while(sc.hasNext()){
               String line=sc.next();
               String lines[]=line.split(" ");
               for(String s:lines){
                   s=s.replaceAll("\W", "");
                   if(hmap.containsKey(s)){
                       hmap.put(s, hmap.get(s)+1);
                   }else
                       hmap.put(s, 1);
               }
           }
       }catch(FileNotFoundException fe){
           fe.printStackTrace();
       }
       System.out.println("Word Count without ordering :");
       for(Map.Entry<String, Integer> entry: hmap.entrySet()){
           System.out.println(entry.getKey()+" "+entry.getValue());
       }
       hmap=sortByCount(hmap);
      
       System.out.println("Word Count After ordering :");
       for(Map.Entry<String, Integer> entry: hmap.entrySet()){
           System.out.println(entry.getKey()+" "+entry.getValue());
       }
   }
   public static Map<String, Integer> sortByCount( Map<String, Integer> map )
   {
        List<Map.Entry<String, Integer>> list =
            new ArrayList<Map.Entry<String,Integer>>( (Collection<? extends Entry<String, Integer>>) map.entrySet() );
        Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
        {
           @Override
           public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
               // TODO Auto-generated method stub
               return o2.getValue().compareTo(o1.getValue());
              
           }
        } );
        Map<String, Integer> result = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : list)
        {
            result.put( entry.getKey(), entry.getValue() );
        }
        return result;
   }
}