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

List Sort uses Comparer instead of IEquatable, Why?

I have written a whole bunch of objects that are all parts of collections and on which I will need to do lots of sort and search. On most of these object I have implemented and overridden the Equals method, IEquatable and operator! and operator==.…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
12
votes
4 answers

What's the best practice for returning an internal collection in Java?

I'm curious what is considered a better practice when returning a collection of objects from a mutable class: public class Someclass { public List strings; public add(String in){ strings.add(in); } public remove(String in) {…
Eric Rosenberg
  • 1,543
  • 9
  • 18
12
votes
3 answers

Is there a way to check if a Stream contains all collection elements?

For example, I need something like: Collection collection = /* ... */; Stream stream = /* ... */; boolean containsAll = stream.map(Object::toString).containsAll(collection); Of course, I could accumulate all elements of the stream…
Nolequen
  • 3,032
  • 6
  • 36
  • 55
12
votes
4 answers

Removing an item from a BlockingCollection

How can an item be removed from a BlockingCollection? Which of the following is correct? myBlockingCollection.Remove(Item); or myBlockingCollection.Take(Item);
Stacker
  • 8,157
  • 18
  • 73
  • 135
12
votes
8 answers

Map collection elements and keep reference to source collection

I'm looking for a way to create a collection, list, set, or map which contains the transformed elements of an original collection and reflects every modification in that collection. For example if I have a List from a third party API and…
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
12
votes
2 answers

How to create dynamic Map key in javascript?

I have tried to create a map like example below... var myMap= {"one": 1,"two": "two","three": 3.0}; So, I iterate them like: for (var key in myMap) { window.alert("myMapmap property \"" + key + "\" = " + myMap[key]); } If my data is dynamic...…
Sthepen
  • 203
  • 2
  • 6
  • 15
12
votes
9 answers

Reversing a Queue and converting it into an int array

I have a Queue declared as Queue queue=new LinkedList();, I need to reverse the elments order in it, and then convert it into an int array. I wrote below code: Collections.reverse((List)queue); int[]…
ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
12
votes
5 answers

How would you implement the IEnumerator interface?

I have a class that map objects to objects, but unlike dictionary it maps them both ways. I am now trying to implement a custom IEnumerator interface that iterates through the values. public class Mapper : IEnumerable, IEnumerator { …
Tomas Pajonk
  • 5,132
  • 8
  • 41
  • 51
12
votes
3 answers

How to get index of element in Laravel collection

How to retrieve the index of an element in a collection ? My code : $users = User::has('posts')->withCount('posts')->orderBy('posts_count')->take(50)->get(); if($users->contains(Auth::id())){ //get the index of auth user id } Thank's for…
Simpledev
  • 598
  • 1
  • 11
  • 22
12
votes
4 answers

Java: Generic List to Set conversion and vice versa

I Need a java function which converts from java.util.List to java.util.Set and vice versa, independent of type of objects in the List/Set.
Arjun
  • 6,501
  • 10
  • 32
  • 34
12
votes
7 answers

How to hide some members of an interface

I would like to create a custom collection that implements ICollection. But I would like not to expose some memebers of ICollection like Clear method. How to achieve this?
nan
  • 19,595
  • 7
  • 48
  • 80
12
votes
2 answers

What makes Hashmap.putIfAbsent faster than containsKey followed by put?

Question How is the HashMap method putIfAbsent able to perform a put conditionally in a way thats faster than calling containsKey(x) prior? For example, if you didn't use putIfAbsent you could use: if(!map.containsKey(x)){ …
Usman Mutawakil
  • 4,993
  • 9
  • 43
  • 80
12
votes
4 answers

Collections emptyList/singleton/singletonList/List/Set toArray

Suppose I have this code: String[] left = { "1", "2" }; String[] leftNew = Collections.emptyList().toArray(left); System.out.println(Arrays.toString(leftNew)); This will print [null, 2]. This sort of makes sense, since we have an empty list it is…
Eugene
  • 117,005
  • 15
  • 201
  • 306
12
votes
3 answers

VBA - How to add a collection to a collection of collections

I am trying to create code to represent a form document using VBA in Word 2007. I have created classes to represent Section, QuestionSet and Question. So I have 15 Sections. I have created a function to create each 'Section' Object add it to the…
Stevo
  • 2,601
  • 3
  • 24
  • 32
12
votes
2 answers

why type variable is non-reifiable type in java

I'm currently learning java generics and below is a list of types in java that is non-reifiable. A type is not reifiable if it is one of the following: • A type variable (such as T) • A parameterized type with actual parameters (such as…
Thor
  • 9,638
  • 15
  • 62
  • 137
1 2 3
99
100