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

Should I use put() or putIfAbsent() after using getOrDefault()?

Java8 introduced those nice methods getOrDefault() and putIfAbsent(), allowing to write code like: Map> itemsByFoo = ... List bars = itemsByFoo.getOrDefault(key, new ArrayList<>()); bars.add(someNewBar); Now I am wondering if…
GhostCat
  • 137,827
  • 25
  • 176
  • 248
13
votes
4 answers

Dictionary in python with order I set at start

I'm making dictionary: d = {"server":"mpilgrim", "database":"master"} d['mynewkey'] = 'mynewvalue' But when I display it I saw that this dict is reversed. print(d) {'mynewkey': 'mynewvalue', 'database': 'master', 'server': 'mpilgrim'} How to…
user278618
  • 19,306
  • 42
  • 126
  • 196
13
votes
10 answers

STL + Ordered set + without duplicates

I need to have an ordered set of values without duplicates. So, what is the fast/best method : 1 - Create a vector, sort it and remove duplicates ? 2 - Use a kind of "sorted" vector (if it exists) ? Which one can be the more efficient ?
Spectral
  • 717
  • 2
  • 12
  • 28
13
votes
4 answers

In TreeSet, Sorting & Uniqueness of custom objects based on different properties

Below is my Student class class Student implements Comparable { String name; int rollNo; @Override public int compareTo(Object obj) { return ((Student)obj).name.compareTo(this.name); } } latest modification: but still no…
SmartSolution
  • 2,320
  • 5
  • 37
  • 49
13
votes
12 answers

Neat way to write loop that has special logic for the first item in a collection

Often I have to code a loop that needs a special case for the first item, the code never seems as clear as it should ideally be. Short of a redesign of the C# language, what is the best way to code these loops? // this is more code to read then I…
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
13
votes
4 answers

What kind of Java collection should I use for this?

I am fairly new to Java and am having trouble understanding the differences between different collections. I have a list of 19 resources. Each resource is assigned a hex color. I want to get a random resource with its color and use that resource…
ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
13
votes
3 answers

Why ArrayList doesn't implements Queue?

Maybe it's silly, but I have to know the answer. I am scratching my head looking at its source code and doesn't see any reason why authors implement Queue in LinkedList, but decided not to do the same with ArrayList, instead they have created…
ieXcept
  • 1,088
  • 18
  • 37
13
votes
1 answer

How to array_values laravel collection?

I have a search result from image model and $photo saved the data which $photo->type == 'photo'. $photo = $image->filter(function($photo,$key) use($path){ if($photo->type == 'photo'){ $photo->url = $path.$photo->image; …
Fan
  • 1,124
  • 3
  • 17
  • 35
13
votes
6 answers

android rxjava sort list with comparator class

I started to use rxjava with my android projects. I need to sort returning event list from api call. I wrote comparator class to sort list : public class EventParticipantComparator { public static class StatusComparator implements…
okarakose
  • 3,692
  • 5
  • 24
  • 44
13
votes
9 answers

Which Delphi data structure can hold a list of unique integers?

When I approach Java problems, I use the collection pattern. However, doing it in Delphi is quite a nightmare since there is no Integer object to handle things. I need a data structure that holds numbers. I want to be able to add numbers, remove…
none
  • 4,669
  • 14
  • 62
  • 102
13
votes
9 answers

Split C# collection into equal parts, maintaining sort

I am trying to split a collection into multiple collections while maintaining a sort I have on the collection. I have tried using the following extension method, but it breaks them incorrectly. Basically, if I was to look at the items in the…
Brian David Berman
  • 7,514
  • 26
  • 77
  • 144
13
votes
3 answers

ArrayList and HashSet memory allocation strange test results

I was inspired by this topic: Performance and Memory allocation comparision between List and Set to actually run some tests and measure the performance difference between ArrayList and HashSet. The most upvoted answer, in the mentioned topic, that…
Atais
  • 10,857
  • 6
  • 71
  • 111
13
votes
3 answers

How to check if collection contains any element from other collection in Scala?

Title says it all, what is the best practice for finding out if collection contains any element of other collection? In java I would execute it like this CollectionUtils.containsAny(a, b) using common apache collection utils, where variables a/b…
toucheqt
  • 725
  • 2
  • 8
  • 15
13
votes
3 answers

Java collections covariance problem

Lets say we have a program which contains such classes: public interface AbstractItem { } public SharpItem implements AbstractItem { } public BluntItem implements AbstractItem { } public interface AbstractToolbox { //well the problem starts…
Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
13
votes
2 answers

java 8 merge all elements of ListB into ListA if not present

I need to merge all elements of a listB into another list listA. If an element is already present (based on a custom equality-check) in listA I don't want to add it. I don't want to use Set, and I don't want to override equals() and hashCode().…
SebastianRiemer
  • 1,495
  • 2
  • 20
  • 33