Questions tagged [generic-collections]

A collection that supports generic typing of its elements

Since java 1.5 (released in 2004), the JDK java.util.Collection interface is generic (typed).

Unless you're using an ancient version of java, the word "generic" in "generic collections" is redundant.

390 questions
208
votes
6 answers

Case-INsensitive Dictionary with string key-type in C#

If I have a Dictionary is it possible to make methods like ContainsKey case-insensitive? This seemed related, but I didn't understand it properly: c# Dictionary: making the Key case-insensitive through declarations
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
50
votes
8 answers

Can't add keyValuePair directly to Dictionary

I wanted to add a KeyValuePair to a Dictionary and I couldn't. I have to pass the key and the value separately, which must mean the Add method has to create a new KeyValuePair object to insert, which can't be very efficient. I can't…
Dave
  • 3,429
  • 2
  • 26
  • 29
46
votes
5 answers

Lists with wildcards cause Generic voodoo error

Does anyone know why the following code does not compile? Neither add() nor addAll() works as expected. Removing the "? extends" part makes everything work, but then I would not be able to add subclasses of Foo. List list1 = new…
oligofren
  • 20,744
  • 16
  • 93
  • 180
40
votes
8 answers

C# / .NET equivalent for Java Collections.emptyList()?

What's the standard way to get a typed, readonly empty list in C#, or is there one? ETA: For those asking "why?": I have a virtual method that returns an IList (or rather, post-answers, an IEnumerable), and the default implementation is empty.…
David Moles
  • 48,006
  • 27
  • 136
  • 235
36
votes
1 answer

Why there isn't a ReadOnlyList class in the System.Collections library of C#?

Reading about the problem of creating a read only primitive vector in C# (basically, you cannot do that), public readonly int[] Vector = new int[]{ 1, 2, 3, 4, 5 }; // You can still changes values I learnt about ReadOnlyCollectionBase. This is a…
Baltasarq
  • 12,014
  • 3
  • 38
  • 57
19
votes
1 answer

QuerydslPredicate and generic class

I have many controllers with same methods, difference is only entity class. I want to create generic BaseController and having problem with QuerydslPredicate annotaion (cannot set root, this is generic) class abstract BaseController
Yura Heineken
  • 255
  • 1
  • 10
19
votes
10 answers

Set Collection for mutable objects in Java

In Java, a set only checks for equality of an object with objects already in the set only at insertion time. That means that if after an object is already present in the set, it becomes equal to another object in the set, the set will keep both…
Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62
19
votes
8 answers

Need an IDictionary implementation that will allow a null key

Basically, I want something like this: Dictionary dict = new Dictionary(); dict.Add(null, "Nothing"); dict.Add(1, "One"); Are there any built into the base class library that allow this? The preceding code will…
Clyde
  • 8,017
  • 11
  • 56
  • 87
16
votes
2 answers

How to create a function that creates a Cartesian product Iterator from an Iterator of Iterators?

If I want to create a Cartesian product of a list of lists in Haskell, I can do this: product [] = [[]] product (xs:xss) = concatMap (\k -> map (k:) (product1 xss)) xs or even this: sequence xss I'm trying to implement an efficient iterator that…
user1685095
  • 5,787
  • 9
  • 51
  • 100
15
votes
5 answers

C# HashSet read-only workaround

Here is this sample code: static class Store { private static List strList = new List(); private static HashSet strHashSet = new HashSet(); public static List NormalList { get { return…
VBTiger
  • 153
  • 1
  • 1
  • 7
14
votes
3 answers

Unable to convert List> to return type IList>

For level order traversal why does this exception occur? Following exception occurs: Cannot implicitly convert type 'System.Collections.Generic.List>' to…
Tarak
  • 149
  • 1
  • 7
13
votes
1 answer

C#: 'IEnumerable' does not contain a definition for 'Intersect'

It's been long since I write a single line of code so, please, be patient if I am asking a dumb question. Even though the IntelliSense shows the Intersect method after Names, I get the following error when trying to compare two IEnumerables. I am…
frankztein
  • 153
  • 1
  • 1
  • 6
13
votes
5 answers

Generic method arguments - Java

I have two almost identical methods, but I'm trying to avoid code duplication. Each of them takes a unique object as arguments and finds out the highest value from it. Here's an example: public Integer getHighestIndexValue(List list) { …
yonikawa
  • 581
  • 1
  • 9
  • 32
13
votes
4 answers

Avoiding Returning Wildcard Types

I have a class with a collection of Wildcard Types that is a singleton, something like: public ObliviousClass{ private static final ObliviousClass INSTANCE = new ObliviousClass(); private Map> map = new HashMap
sethro
  • 2,127
  • 1
  • 14
  • 30
12
votes
1 answer

"Unchecked generic array creation for varargs parameter of type Matcher []" warning using CoreMatchers.allOf()

In my UT code, extract below, I see warning : Unchecked generic array creation for varargs parameter of type Matcher [] I have read in another stackoverflow answer about the problems using a generic parameter to a varargs…
k1eran
  • 4,492
  • 8
  • 50
  • 73
1
2 3
25 26