Java 8 list processing – add elements conditionally

You could try to check the return value of addAll. It will return true whenever the list has been modified, so try this: List<Object> list = new ArrayList<>(); // ret unused, otherwise it doesn’t compile boolean ret = list.addAll(method1()) || list.addAll(method2()) || list.addAll(method3()) || list.addAll(method4()) || list.addAll(method5()) || list.addAll(method6()); return list; Because of lazy evaluation, … Read more

What benefits does dictionary initializers add over collection initializers?

While you could initialize a dictionary with collection initializers, it’s quite cumbersome. Especially for something that’s supposed to be syntactic sugar. Dictionary initializers are much cleaner: var myDict = new Dictionary<int, string> { [1] = “Pankaj”, [2] = “Pankaj”, [3] = “Pankaj” }; More importantly these initializers aren’t just for dictionaries, they can be used … Read more

How to determine if a type is a type of collection?

Really all of these types inherit IEnumerable. You can check only for it: bool IsEnumerableType(Type type) { return (type.GetInterface(nameof(IEnumerable)) != null); } or if you really need to check for ICollection: bool IsCollectionType(Type type) { return (type.GetInterface(nameof(ICollection)) != null); } Look at “Syntax” part: List<T> IList ICollection If you need to exclude strings (which are … Read more

How to compare two Dictionaries in C#

If you’ve already checked that the keys are the same, you can just use: var dict3 = dict2.Where(entry => dict1[entry.Key] != entry.Value) .ToDictionary(entry => entry.Key, entry => entry.Value); To explain, this will: Iterate over the key/value pairs in dict2 For each entry, look up the value in dict1 and filter out any entries where the … Read more

IList and IReadOnlyList

You are out of luck here. IList<T> doesn’t implement IReadOnlyList<T>. List<T> does implement both interfaces, but I think that’s not what you want. However, you can use LINQ: The Count() extension method internally checks whether the instance in fact is a collection and then uses the Count property. The ElementAt() extension method internally checks whether … Read more

MVVM and collections of VMs

Your general approach is perfectly fine MVVM, having a ViewModel exposing a collection of other ViewModels is a very common scenario, which I use all over the place. I would not recommend exposing items directly in a ViewModel, like nicodemus13 said, as you end up with your view binding to models without ViewModels in between … Read more

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