NameValueCollection vs Dictionary [duplicate]

They aren’t semantically identical. The NameValueCollection can have duplicate keys while the Dictionary cannot. Personally if you don’t have duplicate keys, then I would stick with the Dictionary. It’s more modern, uses IEnumerable<> which makes it easy to mingle with Linq queries. You can even create a Dictionary using the Linq ToDictionary() method.

How to convert a Java object (bean) to key-value pairs (and vice versa)?

Lots of potential solutions, but let’s add just one more. Use Jackson (JSON processing lib) to do “json-less” conversion, like: ObjectMapper m = new ObjectMapper(); Map<String,Object> props = m.convertValue(myBean, Map.class); MyBean anotherBean = m.convertValue(props, MyBean.class); (this blog entry has some more examples) You can basically convert any compatible types: compatible meaning that if you did … Read more

Count property vs Count() method?

Decompiling the source for the Count() extension method reveals that it tests whether the object is an ICollection (generic or otherwise) and if so simply returns the underlying Count property: So, if your code accesses Count instead of calling Count(), you can bypass the type checking – a theoretical performance benefit but I doubt it … Read more

assertAlmostEqual in Python unit-test for collections of floats

if you don’t mind using NumPy (which comes with your Python(x,y)), you may want to look at the np.testing module which defines, among others, a assert_almost_equal function. The signature is np.testing.assert_almost_equal(actual, desired, decimal=7, err_msg=”, verbose=True) >>> x = 1.000001 >>> y = 1.000002 >>> np.testing.assert_almost_equal(x, y) AssertionError: Arrays are not almost equal to 7 decimals … Read more

How do I keep the iteration order of a List when using Collections.toMap() on a stream?

The 2-parameter version of Collectors.toMap() uses a HashMap: public static <T, K, U> Collector<T, ?, Map<K,U>> toMap( Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) { return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new); } To use the 4-parameter version, you can replace: Collectors.toMap(Function.identity(), String::length) with: Collectors.toMap( Function.identity(), String::length, (u, v) -> … Read more

Is there anything like asynchronous BlockingCollection?

There are four alternatives that I know of. The first is Channels, which provides a threadsafe queue that supports asynchronous Read and Write operations. Channels are highly optimized and optionally support dropping some items if a threshold is reached. The next is BufferBlock<T> from TPL Dataflow. If you only have a single consumer, you can … Read more

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