Asymptotic complexity of .NET collection classes

MSDN Lists these: Dictionary<,> List<> SortedList<,> (edit: wrong link; here’s the generic version) SortedDictionary<,> etc. For example: The SortedList(TKey, TValue) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary(TKey, TValue) generic class. The two classes … Read more

How to assert all items in a collection using fluent-assertions?

The recommended way is to use OnlyContain: items.Should().OnlyContain(x => x.IsActive, “because I said so!”); These will also work: items.All(x => x.IsActive).Should().BeTrue(“because I said so!”); items.Select(x => x.IsActive.Should().BeTrue(“because I said so!”)) .All(x => true); Note that the last line (.All(x => true)) forces the previous Select to execute for each item.

Spring/json: Convert a typed collection like List

In Spring 3.2 there is now support for generic types using the new exchange()-methods on the RestTemplate: ParameterizedTypeReference<List<MyBean>> typeRef = new ParameterizedTypeReference<List<MyBean>>() {}; ResponseEntity<List<MyBean>> response = template.exchange(“http://example.com”, HttpMethod.GET, null, typeRef); Works like a charm!

How to convert a list with properties to a another list the java 8 way?

you will have to use something like below : List<ProductManager> B = A.stream() .map(developer -> new ProductManager(developer.getName(), developer.getAge())) .collect(Collectors.toList()); // for large # of properties assuming the attributes have similar names //other wise with different names refer this List<ProductManager> B = A.stream().map(developer -> { ProductManager productManager = new ProductManager(); try { PropertyUtils.copyProperties(productManager, developer); } catch … Read more

Flatten array in PowerShell

Piping is the correct way to flatten nested structures, so I’m not sure what would be more “elegant”. Yes, the syntax is a bit line-noisy looking, but frankly quite serviceable. 2020 Edit The recommended syntax these days is to expand % to ForEach-Object. A bit more verbose but definitely more readable: @($a | ForEach-Object {$_}).count

Add one item multiple times to same List

You can use Repeat: List<int> listFullOfInts = Enumerable.Repeat(42, 50).ToList(); Demo If you already have a list and you don’t want to create a new one with ToList: listFullOfInts.AddRange(Enumerable.Repeat(42, 50)); If you want to do add reference types without repeating the same reference, you can use Enumerable.Range+Select: List<SomeClass> itemList = Enumerable.Range(0, 50) .Select(i => new SomeClass()) … Read more

Adding a range of values to an ObservableCollection efficiently

The ObservableCollection exposes an protected Items property which is the underlying collection without the notification semantics. This means you can build a collection that does what you want by inheriting ObservableCollection: class RangeEnabledObservableCollection<T> : ObservableCollection<T> { public void InsertRange(IEnumerable<T> items) { this.CheckReentrancy(); foreach(var item in items) this.Items.Add(item); this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } } Usage: void Main() { … Read more

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