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
16
votes
1 answer

Freemarker 'Collection.contains' functionality

From my java code I'm returning a Set. The view needs to check if the Set contains a specific string.. I can't find any docs on how Freemarker can handle this. Any idea?
mickthompson
  • 5,442
  • 11
  • 47
  • 59
16
votes
3 answers

Best method to check if an object in a list is the last (by date)

I dont know what is the best way to check if my object in my list is the last created. What would be the optimal method to do it ? Should I get the last element in the list and check if my given element is the last ?
Kévin_Bransard
  • 676
  • 2
  • 11
  • 23
16
votes
9 answers

Return zero for Count() on null IEnumerables

I'm getting tired of using code like this: var count = 0; if (myEnumerable != null) { count = myEnumerable.Count(); } And this is a bit pedantic: var count = (myEnumerable ?? new string[0]).Count(); Is there any tidier way of doing this? I…
ProfK
  • 49,207
  • 121
  • 399
  • 775
16
votes
1 answer

Does Rust have Collection traits?

I'd like to write a library that's a thin wrapper around some of the functionality in BTreeMap. I'd prefer not to tightly couple it to that particular data structure though. Strictly speaking, I only need a subset of its functionality, something…
zslayton
  • 51,416
  • 9
  • 35
  • 50
16
votes
3 answers

Why does Collections.sort(List) work in Java 8 with CopyOnWriteArrayList but not in Java 7?

I can sort a list of users without any problems using the following code and Java 8: CopyOnWriteArrayList allCurrentLoginnedUsersList = new CopyOnWriteArrayList<>(); Collections.sort(allCurrentLoginnedUsersList); Now, I changed to Java 7 and…
Tum
  • 3,614
  • 5
  • 38
  • 63
16
votes
2 answers

Collection to DoubleStream

I'm looking for most elegant way to create DoubleStream (for using sum, average, etc.) from a List. At the moment I have such code: List doubles = getListOfDoubles(); double sum = doubles.stream() .mapToDouble(d…
Anatrollik
  • 313
  • 1
  • 3
  • 11
16
votes
3 answers

How to iterate a LinkedList elements in reverse order?

I m new to Java Collections and my doubt is why can't i traverse a element in linkedlist in backward directions.Below I'll explain what i did and please clarify my doubts. I've created interface iterator for forward iterations and listiterator for…
Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
16
votes
8 answers

Testing whether a string has repeated characters

I am trying to figure out the lightest method for determining whether a string has any repeated characters, in the lightest way possible. I have tried searching for similar questions, but cant find any. It also needs to be the shorted way possible,…
Armageddon80
  • 225
  • 1
  • 4
  • 12
16
votes
1 answer

MVC Action Filters Collection was modified; enumeration operation may not execute

We have this error occurring very frequently in an MVC 5.1.3 application, when it does happen, the user has to refresh the page and it does away, so its an intermittent issue. We have found it tricky to diagnose as it appears to happen within the…
Dan
  • 29,100
  • 43
  • 148
  • 207
16
votes
1 answer

Does Collections.sort keep order on equal elements?

I have a list of objects ordered by a date parameter and want to reorder them by category parameter, but keeping the date order within the category. Is something like this enough, or do I have to implement a comparator that takes on account the date…
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
16
votes
7 answers

Unit Testing - Am I doing it right?

Basically I have been programing for a little while and after finishing my last project can fully understand how much easier it would have been if I'd have done TDD. I guess I'm still not doing it strictly as I am still writing code then writing a…
baron
  • 11,011
  • 20
  • 54
  • 88
16
votes
3 answers

How to Replace the value in particular key in LinkedHashMap

I want to replace the value in particular key in LinkedHashMap. for example The initial Condition.. "key1" -> "value1" "key2" -> "value2" "key3" -> "value3" "key4" -> "value4" "key5" -> "value5" I want to expected result... "key1" ->…
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
16
votes
3 answers

Calling stream().reduce() on a list with only one element

I am new to functional programming in java, and wonder how I should code to avoid NPE in (for example) this operation: myList.stream() .reduce((prev, curr) -> prev.getTimestamp().isAfter(curr.getTimestamp()) ? prev : curr); …
aweibell
  • 405
  • 1
  • 5
  • 14
16
votes
2 answers

scala .seq vs .toSeq

In the Scala documentation, collections have both .seq and a .toSeq method defined on them. What is the difference between them, and why are they both there? I wasn't able to figure it out from reading the Scala collection documentation.
mp94
  • 1,209
  • 3
  • 11
  • 23
16
votes
4 answers

Why does a HashMap rehash the hashcode supplied by the key object?

I am reading the code of the HashMap class provided by the Java 1.6 API and unable to fully understand the need of the following operation (found in the body of put and get methods): int hash = hash(key.hashCode()); where the method hash() has the…
VGDIV
  • 679
  • 1
  • 7
  • 16