Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set holding two dates

 
Amy Dickens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Is there a way of having a list that contains 2 date objects?
e.g.


or do i need to create a class that holds the two dates in one object?
e.g.


Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amy,
It depends on whether you want the Set to have one object or two.

If one object, you need the helper object in your second example. If the dates can be "lose" in the set, you can do:
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

For collections etc., I suggest you have a look at the Java Tutorials about collections.

Note there are several kinds of collection, Lists Sets and Maps being the most popular. You are confusing Lists and Sets in what you posted.

Yes, you can set up a List or a Set of Dates, but the two are different: Lists can contain duplicates of the same value and Lists are ordered (the first object added is at the beginning and the last at the end).
You can set up an object to hold two Dates, too. It all depends what you want to do with them. You can have a Set:You can have a List:Or you can have an object whihc has two dates in:Read the link I qouted and see how you get on with that.
CR
 
Amy Dickens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys very much for your time

I understand the difference between Set & List, and my program should not allow two objects of type SomeObject (as in the previous code) to have the same dates...

And so i guess i need to create SomeObject class

I really appreciate it
Thanks again
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amy Dickens:
Thanks guys very much . . . my program should not allow two objects . . .to have the same dates
You're welcome.

If you want to make sure there are two different dates, make the date object override the equals method, then put a test in the constructor of TwoDates which throws an Exception if the two are equal. You might even want to create a SameDateException.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic