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
7 answers

Accessing a property of derived class from the base class in C#

In C#, what is the best way to access a property of the derived class when the generic list contains just the base class. public class ClassA : BaseClass { public object PropertyA { get; set; } } public class ClassB: BaseClass { public…
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
13
votes
3 answers

Java Collections and Garbage Collector

A little question regarding performance in a Java web app. Let's assume I have a List listRubriques with ten Rubrique objects. A Rubrique contains one list of products (List listProducts) and one list of clients (List
Anth0
  • 3,004
  • 6
  • 26
  • 36
13
votes
6 answers

Getting a Scala Map from a Java Properties

I was trying to pull environment variables into a scala script using java Iterators and / or Enumerations and realised that Dr Frankenstein might claim parentage, so I hacked the following from the ugly tree instead: import…
Don Mackenzie
  • 7,953
  • 7
  • 31
  • 32
13
votes
2 answers

JavaFX - Observable Collections in the data model classes

Here follows a simple piece of JavaFX code, to illustrate my question. List list1 = new ArrayList(); list1.add("foo"); ... someListView = new ListView<>(); ObservableList someObservableList =…
bvdb
  • 22,839
  • 10
  • 110
  • 123
13
votes
11 answers

improved collection Iterator

Personally, I find the range of functionality provided by java.util.Iterator to be fairly pathetic. At a minimum, I'd like to have methods such as: peek() returns next element without moving the iterator forward previous() returns the previous…
Dónal
  • 185,044
  • 174
  • 569
  • 824
13
votes
7 answers

How to get the type contained in a collection through reflection

In some part of my code I am passed a collection of objects of type T. I don't know which concrete colletion I will be passed, other than it impements IEnumerable. At run time, I need to find out which type T is (e.g. System.Double, System.String,…
Stefano Ricciardi
  • 2,923
  • 3
  • 33
  • 40
13
votes
2 answers

Difference between Collections.sort() and getting a sorted collection by adding into a TreeSet?

Set ts = new TreeSet(); for(Student s : studentInfo){ ts.add(s); } System.out.println(ts); I've written this above snippet in one of my case block in order to sort a collection of Student Objects. My…
Anjan Baradwaj
  • 1,219
  • 5
  • 27
  • 54
13
votes
9 answers

HashSet vs. ArrayList

So I have a custom class Class that will have a set of another custom class Students. So it will look something like this: public class Class { private Set students; // other methods } Now I will be adding and removing many…
letter Q
  • 14,735
  • 33
  • 79
  • 118
13
votes
1 answer

Why does HashSet.removeAll take a quadratic amount of operations?

I have this code that generates a HashSet and calls removeAll() on it. I made a class A which is just a wrapper of an int, which records the number of times equals is called - the program outputs that number. import java.util.*; class A { int…
Dog
  • 7,707
  • 8
  • 40
  • 74
13
votes
2 answers

How to Assert Dictionaries in Unit Testing

Do you know how I can Assert two dictionaries of type Dictionary> in my Unit test project? I tried with CollectionsAssert but it didn' work for me.I guess that it takes to simple Dictionaries as parameters(e.g.…
mathinvalidnik
  • 1,566
  • 10
  • 35
  • 57
13
votes
6 answers

How might a class like .NET's ConcurrentBag be implemented?

I find myself very intrigued by the existence of a ConcurrentBag class in the upcoming .NET 4.0 framework: Bags are useful for storing objects when ordering doesn't matter, and unlike sets, bags support duplicates. My question is: how might…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
13
votes
3 answers

.NET collection that throws an exception when a duplicate is added

Is there a collection (apart from Dictionary) in the .NET framework (3.5) that throws an exception when a duplicate is added? HashSet does not throw an exception here: HashSet strings = new…
geejay
  • 5,440
  • 8
  • 50
  • 60
13
votes
4 answers

Iterate Doctrine Collection ordered by some field

I need something like this: $products = Products::getTable()->find(274); foreach ($products->Categories->orderBy('title') as $category) { echo "{$category->title}
"; } I know is it not possible,…
inakiabt
  • 1,953
  • 1
  • 16
  • 28
13
votes
1 answer

Select distinct values in all nested collections using LINQ to objects?

Given the following code setup: public class Foo { List MyStrings { get; set; } } List foos = GetListOfFoosFromSomewhere(); How do I get a list of all of the distinct strings in MyStrings across all of the Foo instances using LINQ? I…
Brian Vallelunga
  • 9,869
  • 15
  • 60
  • 87
13
votes
3 answers

Java: Difference Between a collection and 'Data Structure'

In Java, I don't understand a collection vs a 'data structure'. It seems to me that collection refers to list, set, map, queue, and 'data structure' refers to the data structure used to implement the collection such as an array, linked list, or…
user1888243
  • 2,591
  • 9
  • 32
  • 44