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
16
votes
5 answers

List<> Capacity returns more items than added

There are several properties on List that seem to be related to number of items in the list - Capacity, Count (which is present as a property and a method). This is quite confusing especially compared with Array that has just Length. I'm using…
Pete
  • 179
  • 1
  • 2
  • 7
16
votes
3 answers

Why is there no IArray(T) interface in .NET?

Update 2011-Jan-06: Believe it or not, I went ahead and incorporated this interface into an open source library I've started, Tao.NET. I wrote a blog post explaining this library's IArray interface, which not only addresses the issues I…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
16
votes
6 answers

How to flatten all items from a nested Java Collection into a single List?

Given a complex nested collection of objects such as: Set>>> complexNestedCollection; Does a generic method exist to flatten this out and get a single List of all Objects contained within? A few details: The list…
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
16
votes
2 answers

Using the features in Java 8, what is the most concise way of transforming all the values of a list?

Using the new features of Java 8, what is the most concise way of transforming all the values of a List? Given this: List words = Arrays.asList("Apple", "Ananas", "Mango", "Banana", "Beer"); I am currently doing this: for (int n =…
The Coordinator
  • 13,007
  • 11
  • 44
  • 73
16
votes
1 answer

JDK implementation of AbstractList::equals() does not check for list size equality first

Strangely the default JDK 6 implementation of AbstractList::equals() does not seems to check first if the two lists have the same size: public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof List)) …
Laurent Grégoire
  • 4,006
  • 29
  • 52
16
votes
2 answers

Storing different types of elements in a List in Java

I'm trying to develop a general table loader which schema is known at runtime. This requires having a class which contains a list of different types of elements and supports various get and set method such as getInt(int index), asString(int index),…
keelar
  • 5,814
  • 7
  • 40
  • 79
16
votes
4 answers

'Don't expose generic list', why to use collection instead of list in method parameter

I am using FxCop and it shows warning for "Don't expose generic list" which suggests use Collection instead of List. The reason why it is preferred, I know all that stuff, as mentioned in this SO post and MSDN and many more articles I have…
Yogesh
  • 3,044
  • 8
  • 33
  • 60
16
votes
6 answers

Compare two lists for updates, deletions and additions

Simple question. I have a new list and an old list. In Java is there a standard way/library that allows me to compare these two lists and determine which items have been updated/deleted or are completely new? E.g. I should end up with three lists -…
Pablojim
  • 8,542
  • 8
  • 45
  • 69
16
votes
5 answers

Get last element in a SortedDictionary

I see this question. How can I get the last element in a SortedDictionary in .Net 3.5.
leora
  • 188,729
  • 360
  • 878
  • 1,366
16
votes
5 answers

How would you obtain the first and last items in a Queue?

Say I have a rolling collection of values where I specify the size of the collection and any time a new value is added, any old values beyond this specified size are dropped off. Obviously (and I've tested this) the best type of collection to use…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
16
votes
1 answer

Why HashSet does not implement IReadOnlyCollection?

I've just found that .NET Fx now has 3 useful interfaces: IReadOnlyCollection IReadOnlyList IReadOnlyDictionary And I'm bit confused why HashSet do not implement IReadOnlyCollection? Are there any reasons, or Microsoft just forgot…
hazzik
  • 13,019
  • 9
  • 47
  • 86
16
votes
4 answers

C# foreach loop - is order *stability* guaranteed?

Suppose I have a given collection. Without ever changing the collection in any way, I loop through its contents twice with a foreach. Barring cosmic rays and what not, is it absolutely guaranteed that the order will be consistent in both…
Superbest
  • 25,318
  • 14
  • 62
  • 134
16
votes
3 answers

What is the Collections.checkedList() call for in java?

I just want to know for what java.util.Collections.checkedList() is actually used. I have some code that I know is returning me a List but it's being passed through a chain of messaging calls and returned to me as a java.io.Serializable. …
Jay R.
  • 31,911
  • 17
  • 52
  • 61
16
votes
5 answers

Collections.newSetFromMap(»ConcurrentHashMap«) vs. Collections.synchronizedSet(»HashSet«)

Apparently, there are two ways to obtain a thread-safe HashSet instance using Java’s Collections utility class. Collections.newSetFromMap( ConcurrentHashMap ) Collections.synchronizedSet( HashSet ) I ask: How do they differ? Which, and under what…
fbahr
  • 853
  • 1
  • 8
  • 13
15
votes
2 answers

Why is the difference in declaration of generic Lists?

I want to decare two Lists: First is a list of Integers. I decare it as: List ints= Arrays.asList(1,2,3); It works fine. Second is a list of Objects. I declare it as: List objs= Arrays.asList(1,2.13,"three"); But it gives a…
Dipesh Gupta
  • 787
  • 1
  • 7
  • 20