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

Implementing a concurrent LinkedHashMap

I'm trying to create a concurrent LinkedHashMap for a multithreaded architecture. If I use Collections#synchronizedMap(), I would have to use synchronized blocks for iteration. This implementation would lead to sequential addition of elements. If I…
Nilesh
  • 557
  • 2
  • 10
  • 21
14
votes
6 answers

Collect HashSet / Java 8 / Regex Pattern / Stream API

Recently I change version of the JDK 8 instead 7 of my project and now I overwrite some code snippets using new features that came with Java 8. final Matcher mtr = Pattern.compile(regex).matcher(input); HashSet set = new HashSet()…
Anton Dozortsev
  • 4,782
  • 5
  • 34
  • 69
14
votes
5 answers

Why does sorted list have to have a key value pair?

If I just want a sorted list of just dates, integers, or doubles is it really necessary to have to define a SortedList(of Integer, Integer)? Seems intriguing to me, but may just be trival. I'd prefer just to use a SortedList(of Integer). (This…
chris.au
  • 1,088
  • 2
  • 15
  • 41
14
votes
7 answers

How to get nth element of a Set

More specifically: how to get the nth element of a LinkedHashSet (which has a predictable iteration order)? I want to retrieve the nth element inserted into this Set (which wasn't already present). Is it better to use a List: List list = new…
markvgti
  • 4,321
  • 7
  • 40
  • 62
14
votes
1 answer

Joins in Java 8 Collection API

I have two List> object. [{Month=August-2013, Sales=282200}, {Month=July-2013, Sales=310400}, {Month=June-2013, Sales=309600}, {Month=May-2013, Sales=318200}, {Month=September-2013, Sales=257800}] and [{Month=April-2013,…
Anurag Tripathi
  • 1,208
  • 1
  • 12
  • 31
14
votes
1 answer

Java 8 - stream ideology

I have recently started playing with Java 8, having done bits and pieces in Haskell/Scala before. I am trying to play with higher-order functions in Java such as map or forEach, and I am struggling to understand what motivation it was to push…
Bober02
  • 15,034
  • 31
  • 92
  • 178
14
votes
8 answers

List with multiple indexes

Given a generic List I would need some kind of index (in the database sense) that would allow me fast retrieval. The keys for this index would not be unique, so I can't use a dictionary. Here's what I have in mind: Given a class Foo { P1, P2, P3 }…
pbz
  • 8,865
  • 14
  • 56
  • 70
14
votes
15 answers

Duplicate values in the Set collection?

Is it possible to allow duplicate values in the Set collection? Is there any way to make the elements unique and have some copies of them? Is there any functions for Set collection for having duplicate values in it?
Johanna
  • 27,036
  • 42
  • 89
  • 117
14
votes
5 answers

Suitable collection class for event listeners in Java

Related: Does java have a "LinkedConcurrentHashMap" data structure? I am looking for a collection class to hold references to event listeners. Ideally I would like the collection to have the following properties (in order of priority): Maintains…
finnw
  • 47,861
  • 24
  • 143
  • 221
14
votes
2 answers

Convert List of 1 element to Option

Let's say I have a List[T] from which I need a single element, and I'd like to convert it to an Option. val list = List(1,2,3) list.take(1).find(_=>true) // Some(1) val empty = List.empty empty.take(1).find(_=>true) // None This would appear to be…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
14
votes
2 answers

How to join collections in Magento?

I am trying to join a custom collection with products to show the product name (not just the id) in the admin grid widget. So far i can't find the correct syntax. I am able to retrieve the products with the product name by the…
user2683224
  • 203
  • 1
  • 3
  • 5
14
votes
4 answers

Inferred type is not a valid substitute for a Comparable generic type

Consider the code: public abstract class Item implements Comparable { protected T item; public int compareTo(T o) { return 0; // this doesn't matter for the time being } } public class MyItem extends…
terrorcell
  • 187
  • 1
  • 2
  • 8
14
votes
3 answers

LinkedIdentityHashSet

I know both the IdentityHashSet (via Collections#newSetFromMap(Map))and the LinkedHashSet. However, what I need is a combination of the two, a LinkedIdentityHashSet. I could not find any ready-made solution on the net. Anybody knows how to tweak…
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
14
votes
6 answers

Hashcode for NULL key in HashMap

I was just reading about the difference between HashMap and HashTable class in java. There I found a difference that former allow null key and later doesn't privileges for the same. As far as the working of HashMap is concern I know that, it calls…
Prashant
  • 692
  • 2
  • 11
  • 26
14
votes
3 answers

Compiler error on Java generic interface with a List<> method

I don't understand the compiler error resulting from the following code. I define a generic interface, see Task, with two methods: U doSomething(String value) and List getIDs(). The doSomething() method actually uses the generic type as the…
Martin Woolstenhulme
  • 3,968
  • 4
  • 24
  • 25