• Post Reply Bookmark Topic Watch Topic
  • New Topic

Most suitable data type for category list  RSS feed

 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am building a new class which will represent product objects. Each object can belong to 1 or more categories. The category hierarchy will be flat.

Would the best way of representing what categories a product is a member of be with a simple ArrayList of strings? An enumerated type? Should I define the possible categories as a list of constants in the product class so I can check the correct categories are assigned on object creation?
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is straight from Bruce Eckel's book, very simple:


Keep in mind that interface fields are implicitly final and static, just replace Months with Categories.

Eckel also suggests that for extra type safety (which I frankly don't fully understand yet :P), you use something like I did below. It's one of the of the exercises I did yesterday:



I would be very interested in seeing what more experienced programmers have to say about this.

Oh, and an ArrayList<String> would be fine I think, but for less overhead I'd just use a String array.
 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, although I'm not sure exactly how your examples related to my problem, isn't that example more for creating a class with static methods?

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, although I'm not sure exactly how your examples related to my problem, isn't that example more for creating a class with static methods?


No, not exactly, though it may be overkill, depending on what you're trying to do. The class is used to store static data and there are some methods in there to access that data.

I thought about your product class, and you could do something like this:



You could make a constructor that initializes the object with the appropriate categories.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Before you go too far down this route, what Paul mentioned was correct a few versions of Java ago - the information is a bit dated. Now you should use Java enums which are a type-safe alternative to constants.


Go through the tutorial link and you will see why the enums are a much better alternative to constants and what you can use the for. You can, for example, get a Category using Category myCategory = Category.valueOf(name) if you get the category names in by String, or Category myCategory = Category.Bath if using pure code. You can safely compare enums using ==, and use them in switch statements. And you can use myCategory.name() to get a string representation of the Category if you need to write it out.

How you best approach your situation depends a lot on the data and how it will be used, though. You could have each Product have a Set<Category>, which would be my preference, assuming you are using a database to store categories and products and can use the DB to lookup products by category when needed.

Enums may not be the best solution - though - if the set of categories fluctuates (they get renamed, or new ones can be added fluidly).
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Steve.

I'm using an outdated Java (TIJ 3rd Ed.) book where I found this:

Because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, much as you would with an enum in C or C++





 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is also probably better to create constants in a class than an interface; there is then no temptation to implement it and have those constants part of the public interface of the class too. An enum is a special kind of class, so that follows the same principle.
 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Mrozik wrote:Thanks a lot Steve.

I'm using an outdated Java (TIJ 3rd Ed.) book where I found this:

Because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, much as you would with an enum in C or C++





I did think it seemed a little bit of unnatural way of doing things, was this then a work around to fake enum's before Java had that functionality? oh and thanks for the link im about to read it

Steve, I'm building a Play framework web service which is going to be returning the Product objects in JSON form to a Android app. The categories will possibly get renamed and new ones added. Would a Set<Category> be preferable then?
 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is also probably better to create constants in a class than an interface; there is then no temptation to implement it and have those constants part of the public interface of the class too. An enum is a special kind of class, so that follows the same principle.


I'm definitely missing something here, if you never implement an interface how can it be used in Paul's previous examples? Are those examples a massive hack which were used pre enums? Are you secretly turning the interface into a class with no methods? These are called marker/tag interfaces right? But I'm guessing Pauls examples arn't one of those?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Gibbs wrote:
I did think it seemed a little bit of unnatural way of doing things, was this then a work around to fake enum's before Java had that functionality?


Unfortunately, yes. I was commonly used, but generally frowned upon.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Gibbs wrote:
Campbell Ritchie wrote:It is also probably better to create constants in a class than an interface; there is then no temptation to implement it and have those constants part of the public interface of the class too. An enum is a special kind of class, so that follows the same principle.


I'm definitely missing something here, if you never implement an interface how can it be used in Paul's previous examples?


You could just use its constants, without ever implementing it.



However, the temptation would then be to have MyClass implement Color, to save some typing, by having MyClass inherit the interface's member variables


This is what is known in technical terms as "icky." Implementing an interface for code sharing is not what inheritance is about.

A better way to do it, pre-enums, was to have a non-instantiable final class.

 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff that really cleared it up for me. I'll be avoiding implementing that like the plague then.

Back to my original question, am I just over thinking this? Decided that I'll be reading the categories from a simple text file:

Cat1
Cat2
and so on.

Should I just bung them in a ArrayList and be done with it? Looked at Set and it doesn't seem to bring anything to the table.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James

Using a Set would remove duplication of categories i.e. duplicate categories specified in the text file.

By the way, does your web service use an XSD for its interface? You could use the schema to define the categories and also handle duplicates.
 
James Gibbs
Greenhorn
Posts: 29
Android Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:
Using a Set would remove duplication of categories i.e. duplicate categories specified in the text file.

By the way, does your web service use an XSD for its interface? You could use the schema to define the categories and also handle duplicates.


Ok that makes sense, I'll use a Set.

I was planning on using JSON, XSD is for XML right?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, XSD defines a definition for an XML payload.

I would go with the Set reading in values from a text file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!