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

Determining the first available value in a list of integers

I got a simple List of ints. List myInts = new List(); myInts.Add(0); myInts.Add(1); myInts.Add(4); myInts.Add(6); myInts.Add(24); My goal is to get the first unused (available) value from the List. (the first positive value that's not…
user356178
16
votes
3 answers

scala: accumulate a var from collection in a functional manner (that is, no vars)

this is a newbie question I have the following code: var total = 0L docs.foreach(total += _.length) in docs I have a collection of objects with the .length property I'd like something like: val total = docs.[someScalaMethod](0, (element, acum) =>…
opensas
  • 60,462
  • 79
  • 252
  • 386
16
votes
3 answers

Using the key in collections.defaultdict

collections.defaultdict is great. Especially in conjunction with lambda: >>> import collections >>> a = collections.defaultdict(lambda : [None,None]) >>> a['foo'] [None, None] Is there a way to use the key given (e.g. 'foo') in the lambda? For…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
16
votes
4 answers

How can I use a collection within an Oracle SQL statement

I want to write an Oracle function that collects some data in multiple steps into a collection variable and use that collection data within a SELECT query like in this very simplified example: CREATE OR REPLACE FUNCTION TESTFUNC01 RETURN VARCHAR2 AS…
blerontin
  • 2,892
  • 5
  • 34
  • 60
16
votes
4 answers

How do I remove entries within a Counter object with a loop without invoking a RuntimeError?

from collections import * ignore = ['the','a','if','in','it','of','or'] ArtofWarCounter = Counter(ArtofWarLIST) for word in ArtofWarCounter: if word in ignore: del ArtofWarCounter[word] ArtofWarCounter is a Counter object containing all…
Louis93
  • 3,843
  • 8
  • 48
  • 94
16
votes
4 answers

How to check that List contains a value?

I have list List list, that contains: [160774, 7212775] and Long id = 7212775. I need to check if the list contains an element with value of id. How to do that? Unfortunately list.contains(id) returns false in my case. I'm using it that…
marioosh
  • 27,328
  • 49
  • 143
  • 192
16
votes
4 answers

How can I shuffle a specific range of an ArrayList?

In Java, I know that to shuffle an ArrayList, the method Collections.shuffle() exists, however this shuffles the entire list. How can I write a method (or, can someone write it and show me it?) such as the following: private ArrayList
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
16
votes
2 answers

Is there a viable generic alternative to apache.commons.collections.CollectionUtils?

Is there a viable generic version of org.apache.commons.collections.CollectionUtils? If not, why not? It seems like an obvious need. Or has the Java community just given up on functional coding until closures are added to Java 17?
kevin cline
  • 2,608
  • 2
  • 25
  • 38
16
votes
2 answers

Returning an ImmutableMap

I have a method that returns a Map. I would initially return the HashMap that the method generated, but thought it would be better to return an ImmutableMap. Unfortunately, the following statement refuses to work in eclipse: HashMap map…
cesar
  • 8,944
  • 12
  • 46
  • 59
16
votes
3 answers

Scala: Yielding from one type of collection to another

Concerning the yield command in Scala and the following example: val values = Set(1, 2, 3) val results = for {v <- values} yield (v * 2) Can anyone explain how Scala knows which type of collection to yield into? I know it is based on values, but…
Zecrates
  • 2,952
  • 6
  • 33
  • 50
16
votes
3 answers

Unit Test IQueryable

I am trying to write a unit test for a method which takes an IQueryable collection. How should I instantiate the collection in my unit test before I pass it to the method for test? This is the default code in my test IQueryable Items = null;…
Adrian S
  • 1,007
  • 4
  • 12
  • 26
16
votes
8 answers

Combine two state flows into new state flow

I have two state flows. Is it possible to combine them and get new state flow? Logically it should be possible because both state flows have initials values, but as I see combine function returns just Flow and not StateFlow.
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
16
votes
6 answers

What is LinkedHashMap?

Ok so i am new to these HashMaps but have some idea about LinkedLists and HashMaps. It would be great if you could give me some simple explanation regarding LinkedHashMap and as in the titile does this mean we are explicitly defining it to be of…
Johnydep
  • 6,027
  • 20
  • 57
  • 74
16
votes
5 answers

Double checked locking on Dictionary "ContainsKey"

My team is currently debating this issue. The code in question is something along the lines of if (!myDictionary.ContainsKey(key)) { lock (_SyncObject) { if (!myDictionary.ContainsKey(key)) { …
16
votes
1 answer

CancellationToken Cancel not breaking out of BlockingCollection

I have a cancellation token like so static CancellationTokenSource TokenSource= new CancellationTokenSource(); I have a blocking collection like so BlockingCollection items= new BlockingCollection(); var item =…
Kenoyer130
  • 6,874
  • 9
  • 51
  • 73