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

A java question. Write a program HashCodeSearch to search a file to see if you c

ID: 3836527 • Letter: A

Question

A java question. Write a program HashCodeSearch to search a file to see if you can find two words that have the same hash code. Read the file with a Scanner and use a data structure that maps the hashcode to a set of words with that hashcode. At the end, print out words that have the same hash code on the same line in lexicographical order. (That will be any set with a length greater than 1)

Finally print the number of duplicates divided by the number of words. It will be a really small number. That is the percentage of collisions.

Set the delimiter to use: scan.useDelimiter("[^A-Za-z0-9_]+");

The book to search is at this URL (http://www.gutenberg.org/ebooks/2600). Download the plain text and save it in your project as "war_and_peace.txt". The book is "War and Peace" - so there will be lots of words. (586,914).

You can just declare that the main method throws FileNotFoundException. No need for a try/catch

please show full code and your output,Thank you!

Explanation / Answer

As this is the simple one we have to knoe that the hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure.

Below i provided code for that it will clarify your doubts.

package com.java2novice.algos;

import java.util.HashMap;

public class MyHashcodeImpl {

    public static void main(String a[]){

         

        HashMap<Price, String> hm = new HashMap<Price, String>();

        hm.put(new Price("Banana", 20), "Banana");

        hm.put(new Price("Apple", 40), "Apple");

        hm.put(new Price("Orange", 30), "Orange");

        //creating new object to use as key to get value

        Price key = new Price("Banana", 20);

        System.out.println("Hashcode of the key: "+key.hashCode());

        System.out.println("Value from map: "+hm.get(key));

    }

}

class Price{

     

    private String item;

    private int price;

     

    public Price(String itm, int pr){

        this.item = itm;

        this.price = pr;

    }

     

    public int hashCode(){

        System.out.println("In hashcode");

        int hashcode = 0;

        hashcode = price*20;

        hashcode += item.hashCode();

        return hashcode;

    }

     

    public boolean equals(Object obj){

        System.out.println("In equals");

        if (obj instanceof Price) {

            Price pp = (Price) obj;

            return (pp.item.equals(this.item) && pp.price == this.price);

        } else {

            return false;

        }

    }

     

    public String getItem() {

        return item;

    }

    public void setItem(String item) {

        this.item = item;

    }

    public int getPrice() {

        return price;

    }

    public void setPrice(int price) {

        this.price = price;

    }

     

    public String toString(){

        return "item: "+item+" price: "+price;

    }

}

The sample output of this program is:

In hashcode

In hashcode

In hashcode

In hashcode

Hashcode of the key: 1982479637

In hashcode

In equals

Value from map: Banana

Hope this answers helps you.

package com.java2novice.algos;

import java.util.HashMap;

public class MyHashcodeImpl {

    public static void main(String a[]){

         

        HashMap<Price, String> hm = new HashMap<Price, String>();

        hm.put(new Price("Banana", 20), "Banana");

        hm.put(new Price("Apple", 40), "Apple");

        hm.put(new Price("Orange", 30), "Orange");

        //creating new object to use as key to get value

        Price key = new Price("Banana", 20);

        System.out.println("Hashcode of the key: "+key.hashCode());

        System.out.println("Value from map: "+hm.get(key));

    }

}

class Price{

     

    private String item;

    private int price;

     

    public Price(String itm, int pr){

        this.item = itm;

        this.price = pr;

    }

     

    public int hashCode(){

        System.out.println("In hashcode");

        int hashcode = 0;

        hashcode = price*20;

        hashcode += item.hashCode();

        return hashcode;

    }

     

    public boolean equals(Object obj){

        System.out.println("In equals");

        if (obj instanceof Price) {

            Price pp = (Price) obj;

            return (pp.item.equals(this.item) && pp.price == this.price);

        } else {

            return false;

        }

    }

     

    public String getItem() {

        return item;

    }

    public void setItem(String item) {

        this.item = item;

    }

    public int getPrice() {

        return price;

    }

    public void setPrice(int price) {

        this.price = price;

    }

     

    public String toString(){

        return "item: "+item+" price: "+price;

    }

}

The sample output of this program is:

In hashcode

In hashcode

In hashcode

In hashcode

Hashcode of the key: 1982479637

In hashcode

In equals

Value from map: Banana

Hope this answers helps you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote