How to check whether key or value exist in Map?

There are several different options, depending on what you mean. If you mean by “value” key-value pair, then you can use something like myMap.exists(_ == (“fish”,3)) myMap.exists(_ == “fish” -> 3) If you mean value of the key-value pair, then you can myMap.values.exists(_ == 3) myMap.exists(_._2 == 3) If you wanted to just test the … Read more

Is there any way to enforce typing on NSArray, NSMutableArray, etc.?

Nobody’s put this up here yet, so I’ll do it! Tthis is now officially supported in Objective-C. As of Xcode 7, you can use the following syntax: NSArray<MyClass *> *myArray = @[[MyClass new], [MyClass new]]; Note It’s important to note that these are compiler warnings only and you can technically still insert any object into … Read more

When would I use java Collections singletonMap method?

Basically, it allows you to do this: callAPIThatTakesAMap(Collections.singletonMap(key, value)); rather than this: Map<KeyType, ValueType> m = new HashMap<KeyType, ValueType>(); m.put(key, value); callAPIThatTakesAMap(m); which is much nicer when you only have a single key/value pair. This situation probably does not arise very often, but singleton() and singletonList() can quite frequently be useful.

C#: Difference between List and Collection (CA1002, Do not expose generic lists) [duplicate]

In short, the generic list does not have virtual methods for Add, Remove etc, as it was designed to be fast, not extensible. This means that you cannot swap this concrete implementation out for a useful subclass (even though you can subclass it as it is not sealed). Therefore, by exposing the List itself, you … Read more

Why this code throws ‘Collection was modified’, but when I iterate something before it, it doesn’t?

The problem is that the way that List<T> detects modifications is by keeping a version field, of type int, incrementing it on each modification. Therefore, if you’ve made exactly some multiple of 232 modifications to the list between iterations, it will render those modifications invisible as far as detection is concerned. (It will overflow from … Read more

The HashSet.removeAll method is surprisingly slow

The behaviour is (somewhat) documented in the javadoc: This implementation determines which is the smaller of this set and the specified collection, by invoking the size method on each. If this set has fewer elements, then the implementation iterates over this set, checking each element returned by the iterator in turn to see if it … Read more

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