1

I know exactly what I am looking for, but not how to ask for it in a search engine. I want a collection that can hold any number of objects including duplicates and allows for them to be pulled out randomly. Similar to a deck of cards.

I don't know what this kind of collection would be called in c#. I imagined FIAO(First In Any Out) but that pulls up nothing. Some direction would be appreciated.

  • 1
    `List` is good enough. Also I'm not sure what does "First In Any Out" even mean. – Sriram Sakthivel May 04 '15 at 17:55
  • 1
    @LuisLavieri There is no `ArrayList` in c#. Are you confusing with java? – Sriram Sakthivel May 04 '15 at 17:56
  • What structures have you tried, and what problems have you had using them? – Servy May 04 '15 at 17:58
  • Yes. My bad. List – Luis Lavieri May 04 '15 at 18:01
  • In java you can use a set, which has the function .removeAny(). A set in java though cannot contain duplicates. In my case I want to have what is essentially a shuffled deck. I put a bunch of stuff in, but then when I pull from it I get random objects out which are then removed from the container. – Andrew Adkins May 04 '15 at 18:03
  • In other words I very decidedly do not want to preserve the order of the objects I put in. – Andrew Adkins May 04 '15 at 18:04
  • If you are using this for a deck of cards, is there a reason you want duplicates? – austin wernli May 04 '15 at 18:06
  • Imagine its magic cards. This kind of deck could contain duplicates of the same card. – Andrew Adkins May 04 '15 at 18:07
  • @AndrewAdkins A deck of cards is conceptually ordered (there is such a thing as a "top card"). Of course, if your actual situation is different, and doesn't need a logical ordering, that's fine, but if you're actually modeling a deck (and aren't using it for a game that uses the top or bottom of the deck), you may want to consider an ordered collection. – Servy May 04 '15 at 18:08
  • For the sake of simplicity I want to know if there is a collection that does this. – Andrew Adkins May 04 '15 at 18:10
  • Also the deck is just an example to show what I am going for. The reality is I don't want to pull things in order because I do not need to and do not want to. – Andrew Adkins May 04 '15 at 18:11
  • I was hoping that I could avoid prediction through not keeping the actual 'deck' in memory. I found a fairly simple random function using Lists somewhere else. I was hoping there was a collection built out of the box for this task, but there does not seem to be one. – Andrew Adkins May 04 '15 at 18:22
  • http://stackoverflow.com/questions/2019417/access-random-item-in-list – Andrew Adkins May 04 '15 at 18:22
  • `IEnumerable` would be a nice collection. The collection would in theory work like a *First In, First Out*. Because every time you add an item, it will take the next index. So when you loop, it will iterate from the first index onward. – Greg May 04 '15 at 18:30
  • Well nevermind about my previous comment. I would like to remove by index but list does not seem to work that way. It seems to work by value. I wish I knew my way around C# a bit better. – Andrew Adkins May 04 '15 at 18:33
  • A data structure with no order and allowing duplicates is called a bag or multiset. There is a "Bag" implementation in the PowerCollections library. – Meta-Knight May 04 '15 at 18:36
  • Cool, now I know what to call it. Thanks. – Andrew Adkins May 04 '15 at 19:05

2 Answers2

1

I think you're looking for ConcurrentBag - I'm not sure if there's a non-concurrent version of this data structure though.

Ani
  • 10,826
  • 3
  • 27
  • 46
  • This is currently the closest thing to what I am looking for. – Andrew Adkins May 04 '15 at 18:36
  • What would be the difference between a concurrent and non concurrent version? – Andrew Adkins May 04 '15 at 18:37
  • ConcurrentBag is thread-safe, meant for programs where it is accessed by producers and consumers. This means that (sometimes, when there is real contention between threads) there is a higher cost adding and removing items - but for the most part, this will be negligible. – Ani May 04 '15 at 18:44
  • Thanks, this seems to be what I want. – Andrew Adkins May 04 '15 at 18:46
0

A Dictionary does not guarantee any particular order of retrieval, with the Keys collection not being ordered. You could keep an incrementing count in the value to signify duplicates.

DWright
  • 9,258
  • 4
  • 36
  • 53