Creating a constant Dictionary in C#

Creating a truly compile-time generated constant dictionary in C# is not really a straightforward task. Actually, none of the answers here really achieve that. There is one solution though which meets your requirements, although not necessarily a nice one; remember that according to the C# specification, switch-case tables are compiled to constant hash jump tables. … Read more

Checking that a List is not empty in Hamcrest

Well there’s always assertThat(list.isEmpty(), is(false)); … but I’m guessing that’s not quite what you meant 🙂 Alternatively: assertThat((Collection)list, is(not(empty()))); empty() is a static in the Matchers class. Note the need to cast the list to Collection, thanks to Hamcrest 1.2’s wonky generics. The following imports can be used with hamcrest 1.3 import static org.hamcrest.Matchers.empty; import … Read more

What .NET collection provides the fastest search

In the most general case, consider System.Collections.Generic.HashSet as your default “Contains” workhorse data structure, because it takes constant time to evaluate Contains. The actual answer to “What is the fastest searchable collection” depends on your specific data size, ordered-ness, cost-of-hashing, and search frequency.

Collections.emptyMap() vs new HashMap()

It is, in my personal experience admittedly, very useful in cases where an API requires a collection of parameters, but you have nothing to provide. For example you may have an API that looks something like this, and does not allow null references: public ResultSet executeQuery(String query, Map<String, Object> queryParameters); If you have a query … Read more

Java Map equivalent in C#

You can index Dictionary, you didn’t need ‘get’. Dictionary<string,string> example = new Dictionary<string,string>(); … example.Add(“hello”,”world”); … Console.Writeline(example[“hello”]); An efficient way to test/get values is TryGetValue (thanx to Earwicker): if (otherExample.TryGetValue(“key”, out value)) { otherExample[“key”] = value + 1; } With this method you can fast and exception-less get values (if present). Resources: Dictionary-Keys Try Get … Read more

Why doesn’t Java Map extend Collection?

From the Java Collections API Design FAQ: Why doesn’t Map extend Collection? This was by design. We feel that mappings are not collections and collections are not mappings. Thus, it makes little sense for Map to extend the Collection interface (or vice versa). If a Map is a Collection, what are the elements? The only … Read more

Why is a ConcurrentModificationException thrown and how to debug it

This is not a synchronization problem. This will occur if the underlying collection that is being iterated over is modified by anything other than the Iterator itself. Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Entry item = it.next(); map.remove(item.getKey()); } This will throw a ConcurrentModificationException when the it.hasNext() is called the second time. The correct … Read more

Binding a list in @RequestParam

Or you could just do it that way: public String controllerMethod(@RequestParam(value=”myParam[]”) String[] myParams){ …. } That works for example for forms like this: <input type=”checkbox” name=”myParam[]” value=”myVal1″ /> <input type=”checkbox” name=”myParam[]” value=”myVal2″ /> This is the simplest solution 🙂

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)