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:

22240 questions
3964
votes
36 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
  • 47,479
  • 10
  • 30
  • 23
3555
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
  • 34,445
  • 3
  • 19
  • 19
3293
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 these I can rework my code. When should LinkedList be used over ArrayList and…
sdellysse
  • 36,011
  • 9
  • 23
  • 24
2940
votes
32 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
  • 87,001
  • 42
  • 169
  • 229
1737
votes
59 answers

Sort a Map by values

I am relatively new to Java, and often find that 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…
Abe
  • 2,463
  • 4
  • 19
  • 16
1282
votes
20 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
  • 13,525
  • 4
  • 19
  • 20
1238
votes
28 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
  • 206,738
  • 150
  • 445
  • 651
1171
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
  • 242,394
  • 72
  • 372
  • 395
1125
votes
17 answers

Converting 'ArrayList to 'String[]' in Java

How might I convert an ArrayList object to a String[] array in Java?
Alex
  • 14,719
  • 6
  • 20
  • 19
893
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
  • 10,392
  • 9
  • 36
  • 50
858
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
  • 29,162
  • 12
  • 52
  • 83
826
votes
24 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
  • 17,329
  • 29
  • 84
  • 133
805
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
  • 12,200
  • 8
  • 33
  • 45
769
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
718
votes
13 answers

How to convert a Map to List in Java?

What is the best way to convert a Map to a List? Just iterate over all values and insert them in a list or am I overlooking something?
Javaa
  • 7,229
  • 3
  • 15
  • 5
1
2 3
99 100