collections

Casting a ListBox.SelectedObjectCollection to a ListBox.ObjectCollection?

倾然丶 夕夏残阳落幕 提交于 2020-01-11 11:18:32
问题 Is it possible to cast a ListBox.SelectedObjectCollection to a ListBox.ObjectCollection in C#? If so, how would I go about it? 回答1: I have a function that accepts List<string> . I can pass both SelectedItems and Items by casting them. Try this: SelectedItems.Cast<string>().ToList() Items.Cast<string>().ToList() <string> could be replaced with some other object type. 回答2: This is not possible. Instead, you should use an IList . Both of these types implement IList , so you can pass either one

Is there a more concise way to do this in Java 8 - Map manipulation

岁酱吖の 提交于 2020-01-11 10:07:51
问题 A very common operation on maps of collections is to create a new collection with an initial value when the key is not present, or if the key is present, do some function on the existing collection. Take for example a Map<String, Set<Integer>> , if the key is not there, create a Set with an initial value of 1. If the key is there, add the value map.size()+1 to the set (or replace this function with some other simple one-liner operation). In Java 7, it's straightforward with if/else, but

Is there a more concise way to do this in Java 8 - Map manipulation

心不动则不痛 提交于 2020-01-11 10:07:24
问题 A very common operation on maps of collections is to create a new collection with an initial value when the key is not present, or if the key is present, do some function on the existing collection. Take for example a Map<String, Set<Integer>> , if the key is not there, create a Set with an initial value of 1. If the key is there, add the value map.size()+1 to the set (or replace this function with some other simple one-liner operation). In Java 7, it's straightforward with if/else, but

Clojure: summing values in a collection of maps

大兔子大兔子 提交于 2020-01-11 09:18:12
问题 I am trying to sum up values of a collection of maps by their common keys. I have this snippet: (def data [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 3}] (for [xs data] (map xs [:a :b])) ((1 2) (1 2)) Final result should be ==> (2 4) Basically, I have a list of maps. Then I perform a list of comprehension to take only the keys I need. My question now is how can I now sum up those values? I tried to use "reduce" but it works only over sequences, not over collections. Thanks. ===EDIT==== Using the

Re-ordering arbitrary an array of integers

穿精又带淫゛_ 提交于 2020-01-11 07:58:06
问题 I have a method that accepts as argument an array of integers and I'd just change arbitrary the order of its values public static int[] _game_number = new int[4]; public static int[] _current_number = new int[4]; public static void GetRandomTwentyFour() { HashSet<int> nums = new HashSet<int>(); for (int i = 0; i < 4; i++) { Random r = new Random(); nums.Add(r.Next(0, 4)); } List<int> liste = nums.ToList<int>(); _current_number = new int[] { _game_number[liste[0]], _game_number[liste[1]],

understanding IEnumerable<T> in C#

僤鯓⒐⒋嵵緔 提交于 2020-01-11 06:55:38
问题 I am trying to understand the IEnumerable interface in C#. As it is stated in MSDN IEnumerable exposes an enumerator, which supports a simple iteration over a non-generic collection. This is quite simple. When we have a collection that implements this interface then, we get an enumerator, which implements the IEnumerator interface, (http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.getenumerator(v=vs.110).aspx), and the using this enumerator we can iterate through all the

Why does changing a mutable array contained in a dictionary not require updating the dictionary?

前提是你 提交于 2020-01-11 06:28:07
问题 I was trying a piece of code from CS193P course (Objective-C). I noticed something in the way that the compiler works. An NSMutableArray called photos was added to an NSMutableDictionary , photosByPhotographer . Later on, a change was made to photos without any changes to photosByPhotographer . When I logged photosByPhotographer , the change was automatically applied to it and it did not need any extra lines of code! I wonder how the compiler makes this work? Any materials to read from? The

Mongo Bulk Insert across multiple collections

给你一囗甜甜゛ 提交于 2020-01-11 04:33:10
问题 I see that mongo has bulk insert, but I see nowhere the capability to do bulk inserts across multiple collections. Since I do not see it anywhere I'm assuming its not available from Mongo. Any specific reason for that? 回答1: You are correct in that the bulk API operates on single collections only. There is no specific reason but the APIs in general are collection-scoped so a "cross-collection bulk insert" would be a design deviation. You can of course set up multiple bulk API objects in a

How to extract all elements of a specific property in a list?

你说的曾经没有我的故事 提交于 2020-01-11 03:09:25
问题 how can I best get all "name" string elements of the following structure: class Foo { List<Bar> bars; } class Bar { private String name; private int age; } My approach would be: List<String> names = new ArrayList<String>(); for (Bar bar : foo.getBars()) { names.add(bar.getName()); } This might work, but isn't there something in java. Collections where I just can write like this: Collections.getAll(foo.getBars(), "name"); ? 回答1: Using Java 8: List<String> names = foo.getBars().stream().map(Bar

What is alternative hashing for String keys in Java 8?

混江龙づ霸主 提交于 2020-01-10 13:41:28
问题 Java 8 is providing alternative hashing for String keys to improve performance when a large number of key hash code collisions are encountered. Can anybody explain what is that and how it will work? 回答1: From this email of core-lib-devs@openjkd : A new interface Hashable32 is introduced. Hashable32 provides a method hash32() String implements Hashable32 and hash32() method HashMap et al recognize String and invoke hash32() rather than hashCode() The revisions of the code: Murmur3 : https:/