• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Hashtable

 
Ranch Hand
Posts: 814
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learnt that Hashtable is a common topic that comes out in the interview.

While learning from hyperskill, I run into this problem of not able to get the instance of hashtable to get after using the put for the data.



I hope someone can let me know why when I tried to use table.get(21), it gives me error

The error is :



TableEntry is a raw type. References to generic type TableEntry<T> should be parameterized.  



I am not sure why it means by this.  Can someone explains things to me ? Tks.


The method get(int) is undefined for the type HashTable<String>



But, it is already defined in this class HashTable



tks.
 
Marshal
Posts: 26750
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're trying to write your own implementation of Java's Hashtable class, then you should realize that as a map, a table entry has a key and a value. So for a start you should be implementing



As for your existing code, you can't call a get(int) method on your Hashtable<T> class simply because you didn't write one.
 
tangara goh
Ranch Hand
Posts: 814
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you're trying to write your own implementation of Java's Hashtable class, then you should realize that as a map, a table entry has a key and a value. So for a start you should be implementing



As for your existing code, you can't call a get(int) method on your Hashtable<T> class simply because you didn't write one.



Hi Paul,

But, the tutorial given did not have K, E inside as seen from the above.
It just have a genericl T inside the EntryTable and then it uses a



Does it mean that there is no way to use a get using the existing code that is written like that?
 
Marshal
Posts: 73760
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:. . . the tutorial given did not have K, E inside . . .

Please always tell us the source of any such information. That allows us to assess it for ourselves. I haven't read the whole of your code, but I think you are not creating a hash table of any sort.
I trust you know why line 32 is incorrect.
 
Ranch Hand
Posts: 165
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope the following explanation will help you to perceive HashTable properties.

1-TableEntry<T> is an API that defines operations of the HashTable class, in other words, API is a tool used by the HashTable class, furthermore, we should implement TableEntry<T> interface in the HashTable class in order to override its methods, as the following example shown



TableEntry<T> is a helper class or a tool used by the HashTable class, the helper class (TableEntry) facilitates reusability and maintenance, e.g., you want to add a new method named displayItems(),  you can add it in the TableEntry<T> class, and override it in the HashTable class, more specifically, you can call TableEntry a service class otherwise HashTable class is a client class




The error that you've faced while using the get() method, is thrown because the get() method is neither defined on TableEntry<T> interface nor HashTable class, that's why the error is raised at compile-time, you would define it in the TableEntry interface, thus, the IDE will ask you automatically to override it in the HashTable class.

NB: your implementation or usage of HashTable may thrown a key collision key (quadratic probing), and you should re-hash it again as you did the first time
 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Houssam El wrote:I hope the following explanation will help you to perceive HashTable properties. . . .

I am afraid it didn't help me. TG has had the misfortune to stumble on a “tutorial” which doesn't actually teach him how to write a hash table in the first place. I think the current problem is beyond redemption and the correct answer is to put the current solution out of its misery, and find a tutorial written by somebody who actually knows what they are doing.
 
tangara goh
Ranch Hand
Posts: 814
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Houssam El wrote:I hope the following explanation will help you to perceive HashTable properties.

1-TableEntry<T> is an API that defines operations of the HashTable class, in other words, API is a tool used by the HashTable class, furthermore, we should implement TableEntry<T> interface in the HashTable class in order to override its methods, as the following example shown



The error that you've faced while using the get() method, is thrown because the get() method is neither defined on TableEntry<T> interface nor HashTable class, that's why the error is raised at compile-time, you would define it in the TableEntry interface, thus, the IDE will ask you automatically to override it in the HashTable class.

NB: your implementation or usage of HashTable may thrown a key collision key (quadratic probing), and you should re-hash it again as you did the first time



Hi Houssam,

the example from Hyperskill actually uses TableEntry as a class, and not interface.

Can it not be done using TableEntry as a class ?

I think they are trying to use TableEntry to illustrate about HashTable..



 
Houssam El
Ranch Hand
Posts: 165
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am afraid it didn't help me. TG has had the misfortune to stumble on a “tutorial” which doesn't actually teach him how to write a hash table in the first place. I think the current problem is beyond redemption and the correct answer is to put the current solution out of its misery, and find a tutorial written by somebody who actually knows what they are doing.



I dunno how to answer you, Campbell, however, I want him to be aware of what he doing, I don't believe in video tutorials or brief explanations that teach many aspects in all programming languages, they skim much important information that can turn the learner frustrated.

I don't have a problem if he wants to contact me in private instant messaging and give him granularity details about HashTable data structure and its properties
 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:. . . Hyperskill actually uses TableEntry as a class, and not interface. . . .

That should make no difference. But the solution here seems to be miles out from what a hash table actually is. I don't think that will give you a correct implementation even if confine yourself to a HashTable<int, K>.

The hash table should not implement the entry interface; a hash table HAS entries; it IS NOT AN entry.
 
Houssam El
Ranch Hand
Posts: 165
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Houssam,

the example from Hyperskill actually uses TableEntry as a class, and not interface.

Can it not be done using TableEntry as a class ?

I think they are trying to use TableEntry to illustrate about HashTable..



The TableEntry class is either used to store items that you'll add using put() method or get() method to retrieve an item from TableEntry[] table.

first of all, you should be aware of what is the goal beyond Hashing, Hashing is used to minimize the length of an array, e.g, we have 100 items, and those items should be stored in a length of 0-9, thus, Hashing method intercepts in this case, and use an equation of hashing as the following example shown,  arrayIdx = key % arraySize, let's assume that a key of an item stored at index 55, arrayIdx = 55 % 99 = 55, the remainder is 55, and we should make another modulo operation, 55 % 10 = 5. I hope this brief explanation helps you to grasp what is goal beyond using hashing. and it is frequently used in dictionary to store words

 
Houssam El
Ranch Hand
Posts: 165
2
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Listen to me carefully, Tangara Goh,

You can't learn algorithms and data structures and grasp them well using the way that you are in now, the best way to perceive them is by reading books that put you on the scope to update your knowledge and flourish your skills, there were plenty of books explain that, and you would choose one, I suggest Data Structures and Algorithms by Robert Lafort, or Algorithms I by Robert Sedgewick
 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Houssam, and what's more, it sounded very authoritative!

I will just add, that while the entire shifting of Civilization from Reading Books to Watching TV has been a Big Downgrade intellectually, there is quite a range of quality even within the video presentations, and certainly blog posts / tutorial sites.

Now Houssam and Campbell will likely point out that yes, some of them are merely Bad, the majority are Terrible, and I can't really disagree.

But if you find the best sources, sometimes video can actually beneficially supplement your reading, hearing and seeing the better presentations can either make things "click" or help one to remember things better.

I have taken to watching some of the better videos AFTER I read the sections in the books, almost like the games where you have to look at two pictures and spot the differences.

I will wind up finding some major and some minor mistakes in what they present, and usually politely point them out for the benefit of the many, many people trying to use them as a primary information source.

That said, we have seen a number of very confused people bring in some fairly awful code that they found somewhere and need help fixing, as in this case, it is more often than not better to just throw it in the Recycling Bin, as it reinforces too many confused notions and bad design choices to count.  Of course, pointing them out is good practice for code reviews, but it is probably better for the original posters to abandon the Very Confused Solutions that are only further obscuring what they are supposed to be teaching.
 
Paul Clapham
Marshal
Posts: 26750
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tangara goh wrote:Does it mean that there is no way to use a get using the existing code that is written like that?



Well, yeah, if you write a put() method then it isn't a get() method and you can't use it to get an entry from the hash table. You have to write a get() method if you want to get an entry. I'm assuming the assignment you are working on expects you to do that.
 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . Houssam and Campbell will likely point out . . .

No, I shall leave it to you; you have done that so well already

. . . very confused people . . .

. . . who don't know the difference between good code and bad code and don't know whether they have fished out a prize salmon or something completely inedible.
 
Bring out your dead! Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic