Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Bitstrings vs Hashmap

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Bitstrings vs Hashmap

    Hi everyone,

    I have a bit of a problem that I've been trying to figure out for a while now, so any new input would be helpful and greatly appreciated.

    One of the things I'm working on is to determine the state of an object. For example, an object could be a fruit, colour red, with skin and sweet, while another object could have four legs, made of wood, and is located in your kitchen. The problem I have is that any given object can have any given pre-defined states, I won't know until runtime as to how many pre-defined states there are. It could be anywhere from 1-1000, let's say, and I could have more than 100k objects in memory. So speed and memory is a bit of an issue.

    Now, one of the ways to do it is with bitstrings manipulation. An "AND" will be able to tell me whether that particular flag is set or not. I guess my question is: would I be saving myself a lot of grief if i just made a hashmap with the value (say, {colour.red, 1}) and checked for that.

    Thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Bitstrings vs Hashmap

    If you are not going to know the number of pre-defined states at runtime a HashMap may be very useful. You may wish to use a HashSet rather than a Map - because it implements the Set interface (which extends Collection) it may give you more of an advantage when doing things that require a Collection (like comparisons - although HashSet uses a HashMap as its foundation, it directly implements the Set/Collection interface whereas the Map does not).

  3. The Following User Says Thank You to copeg For This Useful Post:

    April (February 2nd, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bitstrings vs Hashmap

    I'm going to have to read up on the difference between a HashSet and a HashMap ... thanks!

  5. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bitstrings vs Hashmap

    Here is a definition of HashSet vs HashMap:

    A HashMap provides fast access to a mapping from a unique key to a value. Keys are unordered, which makes it faster than the TreeMap, where the keys are ordered. A HashSet is fast access to unique values only (there are no keys because it is not a mapping, it's just the values). HashSet values are unordered, which makes it faster than the TreeSet, where the values are ordered. (Core Java - What is the difference between hashmap and hashset?)

    I guess in my case, I could just use HashSet because they're not in order, and the existance of those unique values already mean that the flag is set. Thanks!

Similar Threads

  1. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM