aspose file tools*
The moose likes Beginning Java and the fly likes Arraylist insights Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arraylist insights" Watch "Arraylist insights" New topic
Author

Arraylist insights

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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.




Jon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
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

    Joined: Apr 25, 2008
    Posts: 660

    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

    Joined: Mar 15, 2010
    Posts: 25

    "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.


    OCPJP 6, OCE JEE 6 JSP and Servlet Developer
    Jon Camilleri
    Ranch Hand

    Joined: Apr 25, 2008
    Posts: 660

    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

    Joined: Sep 20, 2010
    Posts: 3611
        
      14

    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

    Joined: Apr 25, 2008
    Posts: 660

    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

    Joined: Sep 20, 2010
    Posts: 3611
        
      14

    Sounds like a good idea to me. I would also put the Dealer code in the Poker class, and trash Dealer as well.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Arraylist insights