• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using .retainAll to compare the contents of sets.  RSS feed

 
Alan O' Reilly
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I am wondering if anyone could help. I am trying to re factor the below code, so that the winning lotto numbers can be checked against more than one lotto line e.g. if the user decided to buy two lotto tickets.



Below is my attempt at solving the problem. But it only checks the winning numbers against the first index of 'myTicket'. I appreciate any help anyone can give, I'm new to data structures and am slowly getting to grips with using them.

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You need to break down the problem into little parts. And you need to break the program into little parts, too.
Each method should do one thing and if you do two things you should have two methods. The main method is intended for starting an application off, so that is all it should do. You should have methods for all the set operations, remembering there is
  • equals for =
  • size for cardinality
  • containsAll for ⊆
  • removeAll for \
  • addAll for ∪ and
  • retainAll for ∩
  • So, what is the set operation you are trying to implement? Is it
    ticketNumbers ∩ drawNumbers?
    Or is it
    cardinality(ticketNumbers ∩ drawNumbers)?

    To get your program to work, you should have a Ticket class and a Draw class. Your ticket will have a set of numbers all elements of the set 1‍…‍49 with cardinality = 6 (usually) and your draw will have a similar set. You might do well to restrict the draw, maybe to positive numbers ≤ 10, initially, because the 6 out of 49 draw yields 13983816 different combinations.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7992
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's because you're comparing two sets of sets, not two sets of numbers. retainAll() checks whether two elements are equal, and two sets are equal if their entire content is equal. So since only the first line exactly matches the winning numbers, only the first line is counted.

    What you want to do, is retain all numbers that are in the combined lines of the ticket. You you should take the union of all numbers of the ticket, and then do an intersection with the winning numbers.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alan O' Reilly wrote:Hey, I am wondering if anyone could help. I am trying to re factor the below code, so that the winning lotto numbers can be checked against more than one lotto line e.g. if the user decided to buy two lotto tickets.

    I don't want to overload you, along with all of Campbell's excellent advice; but there's two other things you might want to consider.

  • Lists are much easier to compare for content, if they're both in the same order.
  • Choosing x distinct random items from a set of n (in your case: 5 distinct numbers from a set of 49) is much easier if you randomize ALL the items first.

  • Think about it: What would you do if someone asked you to select 10 random cards from a deck? Would you select one card, put it back in the deck, and then randomly select another card, making sure it's not the same as the first one you picked? No. You'd shuffle the deck, and then deal off the top 10 cards, wouldn't you?

    You might want to look at the Collections.shuffle() method and think about how this might help you.

    HIH

    Winston
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!