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
14
votes
3 answers

Same iteration order on Map.keySet and Map.values?

For a map like: Map map = ...; map.put(1, 1); map.put(2, 2); map.put(3, 3); map.put(4, 4); Is this code... for (Integer i : map.keySet()) System.out.println(i); for (Integer i : map.values()) System.out.println(i); ...guaranteed…
dacwe
  • 43,066
  • 12
  • 116
  • 140
14
votes
8 answers

Way to check if two Collections contain the same elements, independent of order?

Let say I have two different hashsets as shown below how can I check that two Hashset contain the same elements and these two hashsets are equal, independent of the order of elements in collection, please advise..!! Set set1=new HashSet(); …
user1582269
  • 285
  • 1
  • 6
  • 12
14
votes
1 answer

What collection to store a tree structure?

I want to store an organisation chart in a collection. I think a tree data structure will be best suited to my needs, as I need to add multiple nodes to one node. LinkedList only provides adding one node to another node, if I understand it…
gunnerz
  • 1,898
  • 5
  • 24
  • 39
14
votes
5 answers

Applying [AutoFixture] SemanticComparison OfLikeness to sequences / collections / arrays / IEnumerable

We have written a test which looks like the following. This test requires that we have created en Equal-overload for the CodeTableItem-class: ICollection expectedValutaList = new List(); expectedValutaList.Add(new…
14
votes
5 answers

Standard Collection for IDisposable objects

I've got a bunch of IDisposable objects in a lookup table (plain old Dictionary<>, right now), but to simplify the code and avoid error's I'm looking for a collection class which "owns" the items it holds, and to avoid reinventing the wheel - does…
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
14
votes
5 answers

TDD - writing tests for a method that iterates / works with collections

As a newcomer to TDD I'm stuggling with writing unit tests that deal with collections. For example at the moment I'm trying to come up with some test scearios to essentially test the following method int Find(List list, Predicate
Justin
  • 84,773
  • 49
  • 224
  • 367
14
votes
3 answers

CollectionViewSource does not re-sort on property change

I'm binding ItemsControl to CollectionViewSource. Here is code: this.Trucks = new ObservableCollection(); foreach (var truck in DataRepository.Trucks.Where(t => t.ReadyDate.Date.Equals(this.Date))) { …
katit
  • 17,375
  • 35
  • 128
  • 256
14
votes
5 answers

Scala: var List vs val MutableList

In Odersky et al's Scala book, they say use lists. I haven't read the book cover to cover but all the examples seem to use val List. As I understand it one also is encouraged to use vals over vars. But in most applications is there not a trade off…
Rich Oliver
  • 6,001
  • 4
  • 34
  • 57
14
votes
5 answers

Threadsafe collection without lock

I am preparing myself for an interview and I came across the followign question. I tried but I could not find anything which can create a class containing thread safe collection without "lock". If know any solution then please help. Create a C#…
Mayur
  • 347
  • 1
  • 4
  • 10
14
votes
4 answers

Interface/Superclass for Collections/Containers in c++

I'm coming from the Java world and are building a small c++ program at the moment. I have an object that does some work and then returns the result of the work as a list. Now a day later i changed the behavior of the object to save the results in a…
Janusz
  • 187,060
  • 113
  • 301
  • 369
14
votes
4 answers

Symfony2+Doctrine - Validating one-to-many collection of entities

I have a form to create a new entity. That entity has a collection of other entities that are also entered in that form. I want to use the validation options of the entity in the collection to validate those entities but it does not work. The…
Bramklg
  • 151
  • 1
  • 2
  • 6
13
votes
2 answers

Why does guava Multimap.values() return a flat collection instead of a collection of collections?

I really like the Multimap class of the google guava library. It is a map type where you can add multiple values for a key, so it effectively maps from a key to a collection of some type. What I especially love is the Multimaps.index() function…
nansen
  • 2,912
  • 1
  • 20
  • 33
13
votes
11 answers

Declaring a LinkedList in Java

I always learn when we declare a collection we should do, Interface ob = new Class(), if i want to use for example a LinkedList i'll do List ob = new LinkedList(), but then i can't have access to all methods from LinkedList.. Isn't LinkedList ob =…
Márcio Duarte
  • 453
  • 3
  • 5
  • 14
13
votes
3 answers

what happens when you modify an element of an std::set?

If I change an element of an std::set, for example, through an iterator, I know it is not "reinserted" or "resorted", but is there any mention of if it triggers undefined behavior? For example, I would imagine insertions would screw up. Is there any…
rlbond
  • 65,341
  • 56
  • 178
  • 228
13
votes
4 answers

Removing element from list with predicate

I have a list from the .NET collections library and I want to remove a single element. Sadly, I cannot find it by comparing directly with another object. I fear that using FindIndex and RemoveAt will cause multiple traversals of the list. I don't…
Steinbitglis
  • 2,482
  • 2
  • 27
  • 40