This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist insights

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util
Class ArrayList<E>

1. This class is "unsynchronized" mean in the Java context?

Source: http://download.oracle.com/javase/6/docs/api.

2. "public int hashCode()

This method returns the hash code value for the object on which this method is invoked. This method returns the hash code value as an integer and is supported for the benefit of hashing based collection classes such as Hashtable, HashMap, HashSet etc. This method must be overridden in every class that overrides the equals method."

Why?
Does an ArrayList use hashCode() when processing?
How will my hashCode() algorithm affect the performance (memory use, efficiency of algorithms, CPU usage) of processing the ArrayList?

Source: http://www.technofundo.com/tech/java/equalhash.html.

3. I am not sure how to implement a hashCode() method for my Card class, which is extended by a Deck class.



 
Campbell Ritchie
Sheriff
Pie
Posts: 47250
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have already seen that Deck extends Class is incorrect use of inheritance. A Deck "is-not-a" Card.
There are several good references about the equals() method:
  • for example, if you google for Effective Java Joshua Bloch Methods inherited from Object, you might find a sample chapter from the 1st edition, which has lots about the equals() method it. you might find it here.
  • Angelika Langer's website. That actually appears on the Google search I told you about above
  • Odersky Spoon and Venners: you will find a link to their work on our FAQ.
  • Unsynchronized means that multiple threads can gain access to its methods simultaneously. Look in the documentation for ArrayList and the Java™ tutorials for more details.
    No, an array list doesn't use a hash code as a routine.
     
    Jon Camilleri
    Ranch Hand
    Posts: 664
    Chrome Eclipse IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:We have already seen that Deck extends Class is incorrect use of inheritance. A Deck "is-not-a" Card.
    There are several good references about the equals() method:
  • for example, if you google for Effective Java Joshua Bloch Methods inherited from Object, you might find a sample chapter from the 1st edition, which has lots about the equals() method it. you might find it here.
  • Angelika Langer's website. That actually appears on the Google search I told you about above
  • Odersky Spoon and Venners: you will find a link to their work on our FAQ.
  • Unsynchronized means that multiple threads can gain access to its methods simultaneously. Look in the documentation for ArrayList and the Java™ tutorials for more details.
    No, an array list doesn't use a hash code as a routine.


    Thanks I will surely go through the documentation since I am training; any idea how to represent a set of 52 cards as an object and avoid extending the Card class?
     
    Manjula Weerasinghe
    Greenhorn
    Posts: 25
    Java Oracle PHP
    • 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "Deck" is-not-a "Card" as Ritchie has pointed out. So it is not a good design to extend "Card" class for creating "Deck" class. We use extending of classes for representing "IS-A" relationship between two classes.
    Relationship between "Deck" and "Card" is, "Deck" has-a (list of)"Card".
    That means, "Deck" class has a reference to list of instances of "Card" class.
    In your "Deck" class, you have already implemented that relationship using


    As I see, "extends Card" part in your existing class is also not required for any functionality of your current code. No harm will be done if you removed that part. But I didn't read all of your code, there may be few other changes also needs to do if you remove "extends Card" part.
     
    Jon Camilleri
    Ranch Hand
    Posts: 664
    Chrome Eclipse IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Manjula Weerasinghe wrote:"Deck" is-not-a "Card" as Ritchie has pointed out. So it is not a good design to extend "Card" class for creating "Deck" class. We use extending of classes for representing "IS-A" relationship between two classes.
    Relationship between "Deck" and "Card" is, "Deck" has-a (list of)"Card".
    That means, "Deck" class has a reference to list of instances of "Card" class.
    In your "Deck" class, you have already implemented that relationship using


    As I see, "extends Card" part in your existing class is also not required for any functionality of your current code. No harm will be done if you removed that part. But I didn't read all of your code, there may be few other changes also needs to do if you remove "extends Card" part.


    Thank you, you are right, however the implication that I would have to factor out the code currently within the Deck class within the main() method (and will eventually be moved to some calling program when I get to the UI design stage) seems like spaghetti to me, what is your advice?



    I also have another little problem after having tried to rig in Apache logging services to the code.



     
    Stephan van Hulst
    Bartender
    Pie
    Posts: 4829
    34
    Chrome Netbeans IDE Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I didn't see this thread before I answered you in your other thread here: http://www.coderanch.com/t/535373/java/java/cannot-read-Collection-custom-class#2428780

    As has been proposed earlier in this thread, you should study the resources to find out more about the working of equals() and hashCode().

    Thank you, you are right, however the implication that I would have to factor out the code currently within the Deck class within the main() method (and will eventually be moved to some calling program when I get to the UI design stage) seems like spaghetti to me, what is your advice?

    What exactly strikes you as spaghetti? It seems like a perfectly reasonable implication.
     
    Jon Camilleri
    Ranch Hand
    Posts: 664
    Chrome Eclipse IDE
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:I didn't see this thread before I answered you in your other thread here: http://www.coderanch.com/t/535373/java/java/cannot-read-Collection-custom-class#2428780

    As has been proposed earlier in this thread, you should study the resources to find out more about the working of equals() and hashCode().

    Thank you, you are right, however the implication that I would have to factor out the code currently within the Deck class within the main() method (and will eventually be moved to some calling program when I get to the UI design stage) seems like spaghetti to me, what is your advice?

    What exactly strikes you as spaghetti? It seems like a perfectly reasonable implication.


    Okay thanks, so I just have to re-factor out the code from my Deck into the Poker class, which is basically my "mother" class for processing the game.
     
    Stephan van Hulst
    Bartender
    Pie
    Posts: 4829
    34
    Chrome Netbeans IDE Windows
    • 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sounds like a good idea to me. I would also put the Dealer code in the Poker class, and trash Dealer as well.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic