.NET HashTable Vs Dictionary – Can the Dictionary be as fast?

System.Collections.Generic.Dictionary<TKey, TValue> and System.Collections.Hashtable classes both maintain a hash table data structure internally. None of them guarantee preserving the order of items. Leaving boxing/unboxing issues aside, most of the time, they should have very similar performance. The primary structural difference between them is that Dictionary relies on chaining (maintaining a list of items for each … Read more

How do you cast a List of supertypes to a List of subtypes?

Simply casting to List<TestB> almost works; but it doesn’t work because you can’t cast a generic type of one parameter to another. However, you can cast through an intermediate wildcard type and it will be allowed (since you can cast to and from wildcard types, just with an unchecked warning): List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;

Remove elements from collection while iterating

Let me give a few examples with some alternatives to avoid a ConcurrentModificationException. Suppose we have the following collection of books List<Book> books = new ArrayList<Book>(); books.add(new Book(new ISBN(“0-201-63361-2”))); books.add(new Book(new ISBN(“0-201-63361-3”))); books.add(new Book(new ISBN(“0-201-63361-4”))); Collect and Remove The first technique consists in collecting all the objects that we want to delete (e.g. using an … Read more

How to clone ArrayList and also clone its contents?

You will need to iterate on the items, and clone them one by one, putting the clones in your result array as you go. public static List<Dog> cloneList(List<Dog> list) { List<Dog> clone = new ArrayList<Dog>(list.size()); for (Dog item : list) clone.add(item.clone()); return clone; } For that to work, obviously, you will have to get your … Read more

The opposite of Intersect()

As stated, if you want to get 4 as the result, you can do like this: var nonintersect = array2.Except(array1); If you want the real non-intersection (also both 1 and 4), then this should do the trick: var nonintersect = array1.Except(array2).Union( array2.Except(array1)); This will not be the most performant solution, but for small lists it … Read more

What is difference between Collection.stream().forEach() and Collection.forEach()?

For simple cases such as the one illustrated, they are mostly the same. However, there are a number of subtle differences that might be significant. One issue is with ordering. With Stream.forEach, the order is undefined. It’s unlikely to occur with sequential streams, still, it’s within the specification for Stream.forEach to execute in some arbitrary … Read more

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