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 6 of 6

Thread: Data storage question - which type would be best?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Data storage question - which type would be best?

    Hey everyone,
    I am relatively new to Java and have gotten to a point in a piece of software I am working on (for fun) that I am not sure which path I should take.

    Most of the software is complete but here is my question: I have a decent amount of text that needs to be stored and accessed but rarely modified. Each individual 'unit' would consist of a title, paragraph, and tags to define it's category - for it to be searched and returned.

    I considered using enums but I think that would get kind of unwieldy because of the amount of these I would have - and having to declare them all at compile time. Second, I considered using a text which I think would be good for 'holding' the data - using a comma/newline separated format but I wasn't sure what I would 'read' it into per se. I.E., user selects 'residential' category, program searches tags, if finds a residential tag it stores the title, desc, in what for display? I thought enums would be good for this but I know they are constants so I didn't think you could 'read' data into them like this and I wasn't sure if writing a class that would instantiate an individual object for each data unit would be efficient? After the title and desc are displayed that is about all I need with the data and whatever object/data type is holding it could be released.

    Thanks for any help in advance, sorry if it is difficult to follow.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Data storage question - which type would be best?

    One of javas Collections
    Make a class to store each object and the related values and a few methods

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

    mmazur (August 24th, 2012)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Data storage question - which type would be best?

    Before thinking about efficiency try to scope out the details of what it is you need. Organize it into manageable chunks to make the overall problem easier to examine.

    To help get you started:

    1. You have data which is to be searched but rarely modified. What does rarely modified mean (only at construction, a few times relative to use, etc.)? How are you modifying the data? How are you planning on searching the data (what fields, any special characteristics like exact match, partial match, greater than/less than, etc.)?

    2. You have several units, each consisting of a title, paragraph, and tags. Are any of these guaranteed to be unique? What special characteristics (if any) are there about each of these items? What's the source of the data? Is it external (a data file, over the internet, etc.)?

    3. What's the magnitude of your problem set? Do you have a few hundred units? A few thousand? Several billion?

    4. What type of system(s) do you expect your software to run on? A desktop computer? Mobile platform? Super computer? What is more important: speed or memory? How much more important, if at all, is it?

    Once you have this scoped out it will be much easier for you to examine what approach should be taken. The greater you can detail your requirements the easier it will be later on when it comes time to design/implement your solution.

    Some general thoughts:

    It will probably make the most sense to create objects which can hold a unit.

    As jps suggested take a look at the collections already in the Java library. Understand what they are and aren't good at. You may have to make sacrifices in one area to gain benefits in another area.

    Ask if you have any further questions.

    Note that item 4 shouldn't really be given that much thought unless it really matters. In the vast majority of my programs this ends up being answered as I want to write a program for a PC/laptop and speed/memory don't matter too much within reason.
    Last edited by helloworld922; August 24th, 2012 at 03:45 PM.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    curmudgeon (August 24th, 2012)

  6. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Data storage question - which type would be best?

    Quote Originally Posted by helloworld922 View Post
    Before thinking about efficiency try to scope out the details of what it is you need. Organize it into manageable chunks to make the overall problem easier to examine.

    To help get you started:

    1. You have data which is to be searched but rarely modified. What does rarely modified mean (only at construction, a few times relative to use, etc.)? How are you modifying the data? How are you planning on searching the data (what fields, any special characteristics like exact match, partial match, greater than/less than, etc.)?

    2. You have several units, each consisting of a title, paragraph, and tags. Are any of these guaranteed to be unique? What special characteristics (if any) are there about each of these items? What's the source of the data? Is it external (a data file, over the internet, etc.)?

    3. What's the magnitude of your problem set? Do you have a few hundred units? A few thousand? Several billion?

    4. What type of system(s) do you expect your software to run on? A desktop computer? Mobile platform? Super computer? What is more important: speed or memory? How much more important, if at all, is it?

    Once you have this scoped out it will be much easier for you to examine what approach should be taken. The greater you can detail your requirements the easier it will be later on when it comes time to design/implement your solution.

    Some general thoughts:

    It will probably make the most sense to create objects which can hold a unit.

    As jps suggested take a look at the collections already in the Java library. Understand what they are and aren't good at. You may have to make sacrifices in one area to gain benefits in another area.

    Ask if you have any further questions.

    Note that item 4 shouldn't really be given that much thought unless it really matters. In the vast majority of my programs this ends up being answered as I want to write a program for a PC/laptop and speed/memory don't matter too much within reason.
    I have these answers already but that is actually what led me to my question. Thank you for the information though, I think the methodology you outlined is important for any programmer - not just new ones messing around for fun like me. I am looking over Collections now and I think - assuming I can get them to work as I need - that they will do nicely to solve this part of the problem at hand!

  7. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Data storage question - which type would be best?

    I have a new question!

    Below is an example of the data I am storing in a text file - I think this may help with my question below.

    house, residential                                                                      // Tags to categorize entry
    Evans: Columbia County -- Johnson Estate -- Upstairs Hallway          // This is the 'title' of the place
    The hallway in this house could use some work. The overall decor      // Description from viewer
         but there are some scratches along the floor board, small dents   // of property
         in the plaster, and could use a bit of paint on some of the
         doors. Most of the ceiling is fine though.
     
    Oak Room <OR>  Study <ST> Nursery <N> Downstairs <DS>             // List of connecting property rooms

    There could be as many as a few hundred of these separate entries. Once they were noted in the text file though they would not be changed often - if ever.
    The problem I am approaching now is how I should go about formatting the text file itself. I have worked out most of my issues with reading, searching,
    and printing text but it has been basic and single line items to become a little more acclimated with using the in and output functions of Java.

    So now I am at what is the best way to format the text file itself? As you see it above is the original or 'natural' format. I was thinking
    about using XML but I am not sure how that would work with the symbols in the text. I considered doing it line-by-line but
    the descriptions and possibly connecting rooms can vary in the amount of lines.

    Thank you everyone for your help so far! I am moving right a long in my Java ability, but, I am doing this strictly for fun and for myself. I am
    /not/ asking - by any stretch - for someone to write this for me because then I'd have no fun and learn nothing; but, please do not be
    afraid to use small snippets to demonstrate what you mean.

  8. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Data storage question - which type would be best?

    If you don't have a particular formatting method you want your data to be you may want to consider using Java's Serializable interface. It provides a convenient mechanism for saving Java objects in a format that can be easily written and read.

    There's a good tutorial on how to use it from Sun Oracle.

    I can't remember if Serializable produces a human-readable output but I seem to remember that it does. In any-case, if it doesn't I believe you should still be able to use the Serializable along with the Externalizable interfaces to read/write an xml-like output.

    Special characters in xml/html are handled the same way. This is done by using an ampersand (&) code. For example:

    <xml>
    <unit name="&lt3">
    </unit>
    </xml>

    There are various special names given for specific characters. Here's a link to one site which has a list of these: html chars. You can also use the unicode number to produce any character you want.

    <xml>
    <unit name="Hansel &#0038 Gretel">
    </unit>
    </xml>

    Java has a built-in mechanisms to make parsing through XML/HTML easier than writing your own interpreter. Try googling "Java read xml" or some other variant for methods on how to do this.

  9. The Following User Says Thank You to helloworld922 For This Useful Post:

    mmazur (August 25th, 2012)

Similar Threads

  1. My first rpg with a custom data type
    By Kylelem62 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2012, 08:28 AM
  2. Replies: 2
    Last Post: February 17th, 2012, 02:13 AM
  3. Primitive data type
    By stuart harper in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2011, 12:14 PM
  4. About heap data type
    By PaddyWangTheGG in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 17th, 2010, 03:02 PM
  5. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM