Questions tagged [collections]

Collections APIs provide developers with a set of classes and interfaces that make it easier to handle collections of objects.

Collections APIs provide developers with a set of classes and interfaces that make it easier to handle collections of objects. In a sense, collections work a bit like , except their size can change dynamically, and they have more advanced behaviour than arrays.

In

There is a standard C library, GLib, that provides lists, hash tables, growable arrays, trees, simple and multi-key maps and some uncommon collections like quarks, keyed lists and memory chunks.

In

C++ Container framework provides vectors (sizeable arrays), queues, lists, stacks, sets and maps. Maps in this framework may have multiple keys.

In

Java collections framework provides sets, lists, hash tables, ordered (linked) hash tables, stacks and queues. There are also specialized collections to work with multiple threads (blocking queues, etc).

There are three main types of collections:

  1. Lists: always ordered, may contain duplicates and can be handled the same way as usual arrays
  2. Sets: cannot contain duplicates and provide random access to their elements
  3. Maps: connect unique keys with values, provide random access to its keys and may host duplicate values

In

The .NET Framework provides specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces, and these interfaces may be inherited to create new collection classes that fit more specialized data storage needs.

System.Collections Namespace

Collections (C#)

Collections (Visual Basic)

Some Popular Questions in Stackoverflow:

23956 questions
13
votes
6 answers

Enumerate through a subset of a Collection in C#?

Is there a good way to enumerate through only a subset of a Collection in C#? That is, I have a collection of a large number of objects (say, 1000), but I'd like to enumerate through only elements 250 - 340. Is there a good way to get an…
Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
13
votes
5 answers

defaultdict equivalent for lists

Is there\How would you build an equivalent of python's very useful collections.defaultdict? Imagined usage of such a container: >>> a = collections.defaultlist(0) >>> a[2]=7 >>> a[4]='x' >>> a [0,0,7,0,'x'] UPDATE: I've added a follow up question…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
13
votes
4 answers

Should persistent classes initialize instance variable collections

In my Hibernate classes should instance collections be initialized public class Basket { private List items = new ArrayList(); ...getters and setters... } or left uninitalized public class Basket { private List items; ...getters…
Danny
  • 7,368
  • 8
  • 46
  • 70
13
votes
4 answers

How to return a readonly copy of a collection

I have a class that contains a collection. I want to provided a method or property that returns the contents of the collection. It's ok if calling classes can modify the individual objects but I do not want them adding or removing object from the…
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
13
votes
3 answers

Overhead of Iterating T[] cast to IList

I've noticed a performance hit of iterating over a primitive collection (T[]) that has been cast to a generic interface collection (IList or IEnumberable). For example: private static int Sum(int[] array) { int sum = 0; …
Generic Comrade
  • 327
  • 2
  • 7
13
votes
5 answers

Why no immutable double linked list in Scala collections?

Looking at this question, where the questioner is interested in the first and last instances of some element in a List, it seems a more efficient solution would be to use a DoubleLinkedList that could search backwards from the end of the list.…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
13
votes
2 answers

Merged ObservableCollection

I have two ObservableCollections and I need to show them in one ListView control together. For this purpose I created MergedCollection which presents these two collections as one ObservableCollection. This way I can set the ListView.ItemsSource to…
Zefo
  • 217
  • 1
  • 3
  • 7
13
votes
6 answers

How can I use a List<> Collection as a Repeater Datasource in ASP.NET with C#

I have a list collection like below : using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace FileExplorer.Classes { public class NewAddedFiles { public string FileName; public string…
SilverLight
  • 19,668
  • 65
  • 192
  • 300
13
votes
3 answers

Do Sets exist in Go? (like in Python)

Is there any Go collection similar to 'Set's in python? alternatives: Is there an easy way of implementing Sets in Go? Is there any method to eliminate duplicates in a slice?
rivalitaet
  • 386
  • 2
  • 7
13
votes
4 answers

Convert Generic Dictionary to different type

Is there a quick way to convert a Generic Dictionary from one type to another I have this IDictionary _commands; and need to pass it to a function that takes a slightly different typed Dictionary public void…
Nick
  • 5,848
  • 4
  • 28
  • 33
13
votes
10 answers

Off by one with sliding?

One of the advantages of not handling collections through indices is to avoid off-by-one errors. That's certainly not the only advantage, but it is one of them. Now, I often use sliding in some algorithms in Scala, but I feel that it usually results…
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
13
votes
3 answers

Which collection for storing unique strings?

I'm looking for a collection just like Dictionary(OF Key, Value) but I don't actually need a key and value. Key itself is enough. So something like Collection(Key). It shouldn't accept duplicate keys. I've looked up couple of collections in .NET…
dr. evil
  • 26,944
  • 33
  • 131
  • 201
13
votes
2 answers

C# : Distinctions between various Collections

Here's a question that I always come back too every so often: What's the best Collection to use, for some xyz situation (most often to bind to a DropDownList) ? Does anyone have a cheat sheet for this? a comprehensive list of…
bash_79
  • 173
  • 4
13
votes
7 answers

How implementation of java.util.queue uses LIFO?

In Java doc: [...] Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out) How…
celsowm
  • 846
  • 9
  • 34
  • 59
13
votes
7 answers

Using iterator on a TreeSet

SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet. TreeSet ts=new TreeSet(); Iterator itr=ts.iterator(); while(itr.hasNext()){ …
aps
  • 2,452
  • 10
  • 35
  • 47