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
405
votes
11 answers

Java - How to create new Entry (key, value)

I'd like to create new item that similarly to Util.Map.Entry that will contain the structure key, value. The problem is that I can't instantiate a Map.Entry because it's an interface. Does anyone know how to create a new generic key/value object…
Spiderman
  • 9,602
  • 13
  • 48
  • 56
396
votes
8 answers

How to easily initialize a list of Tuples?

I love tuples. They allow you to quickly group relevant information together without having to write a struct or class for it. This is very useful while refactoring very localized code. Initializing a list of them however seems a bit redundant. var…
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
387
votes
10 answers

Java Ordered Map

Is there an object in Java that acts like a Map for storing and accessing key/value pairs, but can return an ordered list of keys and an ordered list of values, such that the key and value lists are in the same order? So as explanation-by-code, I'm…
Whatsit
  • 10,227
  • 11
  • 42
  • 41
378
votes
5 answers

Difference between a Seq and a List in Scala

I've seen in many examples that sometimes a Seq is being used, while other times is the List... Is there any difference, other than the former one being a Scala type and the List coming from Java?
opensas
  • 60,462
  • 79
  • 252
  • 386
374
votes
12 answers

What is the Simplest Way to Reverse an ArrayList?

What is the simplest way to reverse this ArrayList? ArrayList aList = new ArrayList<>(); //Add elements to ArrayList object aList.add("1"); aList.add("2"); aList.add("3"); aList.add("4"); aList.add("5"); while…
Ishu
  • 5,357
  • 4
  • 16
  • 17
351
votes
14 answers

Java: Get first item from a collection

If I have a collection, such as Collection strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there a less wasteful way to do it?
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
347
votes
11 answers

How to convert a Collection to List?

I am using TreeBidiMap from the Apache Collections library. I want to sort this on the values which are doubles. My method is to retrieve a Collection of the values using: Collection coll = themap.values(); Which naturally works fine. Main…
Ankur
  • 50,282
  • 110
  • 242
  • 312
340
votes
5 answers

What is difference between Collection.stream().forEach() and Collection.forEach()?

I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the elements of the…
VladS
  • 4,116
  • 4
  • 16
  • 17
329
votes
8 answers

The opposite of Intersect()

Intersect can be used to find matches between two collections, like so: // Assign two arrays. int[] array1 = { 1, 2, 3 }; int[] array2 = { 2, 3, 4 }; // Call Intersect extension method. var intersect = array1.Intersect(array2); // Write intersection…
Peter Bridger
  • 9,123
  • 14
  • 57
  • 89
329
votes
21 answers

Simple way to find if two different lists contain exactly the same elements?

What is the simplest way to find if two Lists contain exactly the same elements, in the standard Java libraries? It shouldn't matter if the two Lists are the same instance or not, and it shouldn't matter if the type parameter of the Lists are…
Grundlefleck
  • 124,925
  • 25
  • 94
  • 111
325
votes
10 answers

Best practice to validate null and empty collection in Java

I want to verify whether a collection is empty and null. Could anyone please let me know the best practice. Currently, I am checking as below: if (null == sampleMap || sampleMap.isEmpty()) { // do something } else { // do something else }
srk
  • 3,606
  • 2
  • 18
  • 23
320
votes
9 answers

Remove elements from collection while iterating

AFAIK, there are two approaches: Iterate over a copy of the collection Use the iterator of the actual collection For instance, List fooListCopy = new ArrayList(fooList); for(Foo foo : fooListCopy){ // modify actual…
user1329572
  • 6,176
  • 5
  • 29
  • 39
308
votes
20 answers

How do you cast a List of supertypes to a List of subtypes?

For example, lets say you have two classes: public class TestA {} public class TestB extends TestA{} I have a method that returns a List and I would like to cast all the objects in that list to TestB so that I end up with a List.
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
308
votes
21 answers

How to clone ArrayList and also clone its contents?

How can I clone an ArrayList and also clone its items in Java? For example I have: ArrayList dogs = getDogs(); ArrayList clonedList = ....something to do with dogs.... And I would expect that objects in clonedList are not the same as in…
palig
  • 7,651
  • 6
  • 24
  • 18
295
votes
10 answers

.NET HashTable Vs Dictionary - Can the Dictionary be as fast?

I am trying to figure out when and why to use a Dictionary or a HashTable. I have done a bit of a search on here and have found people talking about the generic advantages of the Dictionary which I totally agree with, which leads the boxing and…
Jon
  • 15,110
  • 28
  • 92
  • 132