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
4287
votes
35 answers

What are the differences between a HashMap and a Hashtable in Java?

What are the differences between a HashMap and a Hashtable in Java? Which is more efficient for non-threaded applications?
dmanxiii
  • 51,473
  • 10
  • 33
  • 23
3937
votes
45 answers

How do I efficiently iterate over each entry in a Java Map?

If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation…
iMack
  • 38,281
  • 3
  • 21
  • 19
3585
votes
33 answers

When to use LinkedList over ArrayList in Java?

I've always been one to simply use: List names = new ArrayList<>(); I use the interface as the type name for portability, so that when I ask questions such as this, I can rework my code. When should LinkedList be used over ArrayList and…
sdellysse
  • 39,108
  • 9
  • 25
  • 24
3292
votes
34 answers

Initialization of an ArrayList in one line

I wanted to create a list of options for testing purposes. At first, I did this: ArrayList places = new ArrayList(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then, I refactored the code as…
Macarse
  • 91,829
  • 44
  • 175
  • 230
1872
votes
65 answers

Sort a Map by values

I need to sort a Map on the values. Since the values are not unique, I find myself converting the keySet into an array, and sorting that array through array sort with a custom comparator that sorts on the value associated with the…
Abe
  • 2,463
  • 4
  • 19
  • 16
1574
votes
22 answers

How to directly initialize a HashMap (in a literal way)?

Is there some way of initializing a Java HashMap like this?: Map test = new HashMap{"test":"test","test":"test"}; What would be the correct syntax? I have not found anything regarding this. Is this possible? I am…
jens
  • 16,455
  • 4
  • 21
  • 20
1295
votes
31 answers

Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop

We all know you can't do the following because of ConcurrentModificationException: for (Object i : l) { if (condition(i)) { l.remove(i); } } But this apparently works sometimes, but not always. Here's some specific code: public…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1265
votes
17 answers

Converting 'ArrayList to 'String[]' in Java

How might I convert an ArrayList object to a String[] array in Java?
Alex
  • 16,375
  • 7
  • 22
  • 19
1236
votes
43 answers

How can I initialise a static Map?

How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating the two methods: import…
dogbane
  • 266,786
  • 75
  • 396
  • 414
1034
votes
14 answers

Difference between and in Java

What is the difference between List and List ? I used to use List, but it does not allow me to add elements to it list.add(e), whereas the List does.
Anand
  • 11,872
  • 10
  • 39
  • 51
905
votes
24 answers

How to initialize HashSet values by construction?

I need to create a Set with initial values. Set h = new HashSet(); h.add("a"); h.add("b"); Is there a way to do this in one line of code? For instance, it's useful for a final static field.
Serg
  • 13,470
  • 8
  • 36
  • 47
903
votes
25 answers

How to make a new List in Java

We create a Set as: Set myset = new HashSet() How do we create a List in Java?
user93796
  • 18,749
  • 31
  • 94
  • 150
902
votes
15 answers

Efficiency of Java "Double Brace Initialization"?

In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax: Set flavors = new HashSet() {{ add("vanilla"); add("strawberry"); add("chocolate"); add("butter…
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
825
votes
12 answers

How can I turn a List of Lists into a List in Java 8?

If I have a List>, how can I turn that into a List that contains all the objects in the same iteration order by using the features of Java 8?
Sarah Szabo
  • 10,345
  • 9
  • 37
  • 60
821
votes
19 answers

How to convert an Array to a Set in Java

I would like to convert an array to a Set in Java. There are some obvious ways of doing this (i.e. with a loop) but I would like something a bit neater, something like: java.util.Arrays.asList(Object[] a); Any ideas?
user130076
1
2 3
99 100