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
542
votes
12 answers

HashSet vs. List performance

It's clear that a search performance of the generic HashSet class is higher than of the generic List class. Just compare the hash-based key with the linear approach in the List class. However calculating a hash key may itself take some CPU…
Michael Damatov
  • 15,253
  • 10
  • 46
  • 71
511
votes
48 answers

Get nth character of a string in Swift

How can I get the nth character of a string? I tried bracket([]) accessor with no luck. var string = "Hello, world!" var firstChar = string[0] // Throws error ERROR: 'subscript' is unavailable: cannot subscript String with an Int, see the…
Mohsen
  • 64,437
  • 34
  • 159
  • 186
509
votes
28 answers

Most efficient way to increment a Map value in Java

I hope this question is not considered too basic for this forum, but we'll see. I'm wondering how to refactor some code for better performance that is getting run a bunch of times. Say I'm creating a word frequency list, using a Map (probably a…
gregory
  • 9,464
  • 4
  • 18
  • 13
493
votes
15 answers

Retrieving a List from a java.util.stream.Stream in Java 8

I was playing around with Java 8 lambdas to easily filter collections. But I did not find a concise way to retrieve the result as a new list within the same statement. Here is my most concise approach so far: List sourceLongList =…
Daniel K.
  • 5,747
  • 3
  • 19
  • 22
489
votes
21 answers

Easy way to convert Iterable to Collection

In my application I use 3rd party library (Spring Data for MongoDB to be exact). Methods of this library return Iterable, while the rest of my code expects Collection. Is there any utility method somewhere that will let me quickly convert one…
Ula Krukar
  • 12,549
  • 20
  • 51
  • 65
481
votes
19 answers

Is it better to return null or empty collection?

That's kind of a general question (but I'm using C#), what's the best way (best practice), do you return null or empty collection for a method that has a collection as a return type ?
Omu
  • 69,856
  • 92
  • 277
  • 407
479
votes
6 answers

LINQ .Any VS .Exists - What's the difference?

Using LINQ on collections, what is the difference between the following lines of code? if(!coll.Any(i => i.Value)) and if(!coll.Exists(i => i.Value)) Update 1 When I disassemble .Exists it looks like there is no code. Update 2 Anyone know why…
Anthony D
  • 10,877
  • 11
  • 46
  • 67
468
votes
6 answers

defaultdict of defaultdict?

Is there a way to have a defaultdict(defaultdict(int)) in order to make the following code work? for x in stuff: d[x.a][x.b] += x.c_int d needs to be built ad-hoc, depending on x.a and x.b elements. I could use: for x in stuff: d[x.a,x.b]…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
457
votes
21 answers

How to sort a List/ArrayList?

I have a List of doubles in java and I want to sort ArrayList in descending order. Input ArrayList is as below: List testList = new…
Himanshu
  • 4,751
  • 2
  • 15
  • 16
453
votes
3 answers

Is there an AddRange equivalent for a HashSet in C#

With a list you can do: list.AddRange(otherCollection); There is no add range method in a HashSet. What is the best way to add another ICollection to a HashSet?
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
450
votes
11 answers

What are the reasons why Map.get(Object key) is not (fully) generic

What are the reasons behind the decision to not have a fully generic get method in the interface of java.util.Map. To clarify the question, the signature of the method is V get(Object key) instead of V get(K key) and I'm wondering why (same…
WMR
  • 12,661
  • 5
  • 35
  • 30
432
votes
22 answers

Getting an element from a Set

Why doesn't Set provide an operation to get an element that equals another element? Set set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains…
foobar
  • 4,968
  • 3
  • 17
  • 11
421
votes
9 answers

How to get the first element of the List or Set?

I'd like to know if I can get the first element of a list or set. Which method to use?
user496949
  • 83,087
  • 147
  • 309
  • 426
414
votes
8 answers

How to quickly and conveniently create a one element arraylist

Is there a Utility method somewhere that can do this in 1 line? I can't find it anywhere in Collections, or List. public List stringToOneElementList(String s) { List list = new ArrayList(); list.add(s); return…
David T.
  • 22,301
  • 23
  • 71
  • 123
412
votes
5 answers

Cost of len() function

What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary)
Imran
  • 87,203
  • 23
  • 98
  • 131