collections

How to sort ArrayList<Object> in Ascending order android

佐手、 提交于 2020-01-21 00:05:17
问题 I have a requirement to sort ArrayList<Object> (Custom objects) in Ascending order based upon name. For that purpose I am using comparator method as like My ArrayList : ArrayList<Model> modelList = new ArrayList<Model>(); Code I am using: Comparator<Model> comparator = new Comparator<Model>() { @Override public int compare(CarsModel lhs, CarsModel rhs) { String left = lhs.getName(); String right = rhs.getName(); return left.compareTo(right); } }; ArrayList<Model> sortedModel = Collections

How to sort ArrayList<Object> in Ascending order android

倾然丶 夕夏残阳落幕 提交于 2020-01-21 00:05:11
问题 I have a requirement to sort ArrayList<Object> (Custom objects) in Ascending order based upon name. For that purpose I am using comparator method as like My ArrayList : ArrayList<Model> modelList = new ArrayList<Model>(); Code I am using: Comparator<Model> comparator = new Comparator<Model>() { @Override public int compare(CarsModel lhs, CarsModel rhs) { String left = lhs.getName(); String right = rhs.getName(); return left.compareTo(right); } }; ArrayList<Model> sortedModel = Collections

CollectionViewSource Filter not refreshed when Source is changed

吃可爱长大的小学妹 提交于 2020-01-20 21:45:08
问题 I have a WPF ListView bound to a CollectionViewSource. The source of that is bound to a property, which can change if the user selects an option. When the list view source is updated due to a property changed event, everything updates correctly, but the view is not refreshed to take into account any changes in the CollectionViewSource filter. If I attach a handler to the Changed event that the Source property is bound to I can refresh the view, but this is still the old view, as the binding

What is the Kotlin equivalent of Java Stream.collect?

感情迁移 提交于 2020-01-20 21:44:07
问题 If I want to collect my Kotlin collection into something that isn't built into the stdlib, how do I do it? 回答1: For scenarios not covered by built in operations toList() etc, you can use the fact that collect is just a fold. So given val list: List<Pair<String, Int>> = listOf("Ann" to 19, "John" to 23) you can collect to a collection of your choice with a fold val map: Map<String, Int> = list.fold(HashMap(), { accumulator, item -> accumulator.put(item.first, item.second); accumulator}) If you

Function which generically takes a type and returns the same type

你说的曾经没有我的故事 提交于 2020-01-20 19:42:05
问题 I am having a tough time understanding why the Scala compiler is unhappy about this function definition: def trimNonWordCharacters[T <: Iterable[String]](items: T): T = items map { _.replaceAll("\\W", "") } Here is the REPL output: scala> def trimNonWordCharacters[T <: Iterable[String]](items: T): T = items map { _.replaceAll("\\W", "") } <console>:5: error: type mismatch; found : Iterable[java.lang.String] required: T def trimNonWordCharacters[T <: Iterable[String]](items: T): T = items map

.NET SortedDictionary But Sorted By Values

喜欢而已 提交于 2020-01-20 08:10:12
问题 I need a data structure that acts like a SortedDictionary<int, double> but is sorted based on the values rather than the keys. I need it to take about 1-2 microseconds to add and remove items when we have about 3000 items in the dictionary. My first thought was simply to switch the keys and values in my code. This very nearly works. I can add and remove elements in about 1.2 microseconds in my testing by doing this. But the keys have to be unique in a SortedDictionary so that means that

.NET SortedDictionary But Sorted By Values

别说谁变了你拦得住时间么 提交于 2020-01-20 08:09:52
问题 I need a data structure that acts like a SortedDictionary<int, double> but is sorted based on the values rather than the keys. I need it to take about 1-2 microseconds to add and remove items when we have about 3000 items in the dictionary. My first thought was simply to switch the keys and values in my code. This very nearly works. I can add and remove elements in about 1.2 microseconds in my testing by doing this. But the keys have to be unique in a SortedDictionary so that means that

.NET SortedDictionary But Sorted By Values

妖精的绣舞 提交于 2020-01-20 08:08:07
问题 I need a data structure that acts like a SortedDictionary<int, double> but is sorted based on the values rather than the keys. I need it to take about 1-2 microseconds to add and remove items when we have about 3000 items in the dictionary. My first thought was simply to switch the keys and values in my code. This very nearly works. I can add and remove elements in about 1.2 microseconds in my testing by doing this. But the keys have to be unique in a SortedDictionary so that means that

Dictionary with integer array as a key

半腔热情 提交于 2020-01-20 07:30:25
问题 I need a Dictionary whose key is an array of integers for example Dictionary<int[],string> or Dictionary<List<int>,string>. But I am quite surprised that the Equality method and hash code method is not defined for me. Is there any easy way to implement such a structure other than creating my own MyType: List<int> and to define all necessary methods? 回答1: It isn't predefined because it is expensive . If you know your list is short then just implement the obvious overrides. If not, you'll have

Merging arrays based on a value of the key

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-20 06:02:47
问题 I have two arrays of arrays that have an id key, and I'd like to merge the data together based on that array's key and key value. The data would look something like: $color = [ ['id' => 1, 'color' => 'red'], ['id' => 2, 'color' => 'green'], ['id' => 3, 'color' => 'blue'], ]; $size = [ ['id' => 1, 'size' => 'SM'], ['id' => 2, 'size' => 'XL'], ['id' => 3, 'size' => 'MD'], ['id' => 4, 'size' => 'LG'], ]; $combined = [ ['id' => 1, 'color' => 'red', 'size' => 'SM'], ['id' => 2, 'color' => 'green',