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

Under what conditions can TryDequeue and similar System.Collections.Concurrent collection methods fail

I have recently noticed that inside the collection objects contained in System.Collections.Concurrent namespace it is common to see Collection.TrySomeAction() rather then Collection.SomeAction(). What is the cause of this? I assume it has something…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
14
votes
6 answers

Reverse / transpose a one-to-many map in Scala

What is the best way to turn a Map[A, Set[B]] into a Map[B, Set[A]]? For example, how do I turn a Map(1 -> Set("a", "b"), 2 -> Set("b", "c"), 3 -> Set("c", "d")) into a Map("a" -> Set(1), "b" -> Set(1, 2), "c" -> Set(2, 3), "d"…
aioobe
  • 413,195
  • 112
  • 811
  • 826
14
votes
6 answers

Should I declare/Initialize ArrayLists as Lists, ArrayLists, or ArrayLists of

What is the difference in declaring a collection as such public class CatHerder{ private List cats; public CatHerder(){ this.cats = new ArrayList(); } } //or public class CatHerder{ private ArrayList cats; public…
davidahines
  • 3,976
  • 16
  • 53
  • 87
14
votes
3 answers

How to sum fields of collection elements without mapping them first (like foldLeft/reduceLeft)?

Consider this class: case class Person(val firstName: String, val lastName: String, age: Int) val persons = Person("Jane", "Doe", 42) :: Person("John", "Doe", 45) :: Person("Joe", "Doe", 43) :: Person("Doug", "Don", 65) :: …
soc
  • 27,983
  • 20
  • 111
  • 215
14
votes
2 answers

Collect Java8 IntStream into Deque interface

How to collect Java8 IntStream into Deque interface? I can perform this kind of operation with List like that: List integerList = IntStream.of(1, 2, 3) .boxed() …
maheryhaja
  • 1,617
  • 11
  • 18
14
votes
1 answer

How to create List from Set in Kotlin?

Suppose I have a set with elements. How to create List of the same elements? I see methiods asSequence and asIterable, but no asList, why?
Dims
  • 47,675
  • 117
  • 331
  • 600
14
votes
1 answer

How does the VecDeque ring buffer work internally?

The VecDeque documentation says that it uses a growable ring buffer as implementation. How does it work internally? In the case when I only use it as a queue (only push_back and pop_front), when is the shifting done? Each time I call pop_front?…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
14
votes
2 answers

Why null key is not allowed in TreeMap?

I am trying to understand the concept behind Java Collection framework and came along to this question - Why null key is not allowed in TreeMap? Its giving NullPointerException if we try to add null key in TreeMap. Tried to google the Internal…
Amol Patil
  • 985
  • 2
  • 11
  • 43
14
votes
4 answers

IEqualityComparer interface in Java

More specifically I want an interface to compare objects which can be only compared for equality [e.g. complex numbers] but don't have total order on them. It should have [Note that it only returns a boolean yes/no] boolean Equals(T object1, T…
Fakrudeen
  • 5,778
  • 7
  • 44
  • 70
14
votes
3 answers

Java case-insensitive map with null key

Is there a Map implementation in Java that will use case-insensitive String matching for the key, but also supports the null key? I know that new TreeMap(String.CASE_INSENSITIVE_ORDER) supports case-insensitive matching of String…
Gilo
  • 640
  • 3
  • 23
14
votes
7 answers

Java: Writing/Reading a Map from disk

I have a data structure that I would like to be able to write to a file before closing the program, and then read from the file to re-populate the structure the next time the application starts. My structure is HashMap. The Object is…
jazz99
  • 143
  • 1
  • 1
  • 5
14
votes
3 answers

Array map vs. forEach in Swift

In Swift arrays you can do: var myArray = [1,2,3,4] myArray.forEach() { print($0 * 2) } myArray.map() { print($0 * 2) } They both do the same thing. The only difference is .map also returns an array of voids as well, [(),(),(),()], which gets…
CommaToast
  • 11,370
  • 7
  • 54
  • 69
14
votes
4 answers

Using collection size in for loop comparison

Is there a compiler optimization for the size() methods of Collections in Java? Consider the following code: for(int i=0;i
athena
  • 5,579
  • 8
  • 30
  • 31
14
votes
11 answers

Hashmap implementation to count the occurrences of each character

The below code is to count the occurence of each character and it should print the count. But with the code I have tried I get only a 1 I don't know the changes I should make. import java.io.BufferedReader; import java.io.DataInputStream; import…
Sumithra
  • 6,587
  • 19
  • 51
  • 50
14
votes
1 answer

Difference between all() and toArray() in Laravel 5

When I manage a collection that I need to convert to an array, I usually use toArray(). But I can also use all(). I'm not aware of the diference of those 2 function... Anybody knows?
Juliatzin
  • 18,455
  • 40
  • 166
  • 325