collections

Why is the initial capacity in HashMap 16 (power of two) and the initial capacity of Hashtable 11(prime number)?

核能气质少年 提交于 2020-08-19 04:20:10
问题 Please describe the reason if you know. I Googled it, but didn't find well explained answers. Is it for making index of bucket positive when your hashCode is negative? 回答1: For HashMap , the index in the array that stores the entries of the Map is calculated this way (where h is calculated from the hashCode of the key): static int indexFor(int h, int length) { return h & (length-1); } Where length is the length of the array. This only works when length is a power of 2. If length wasn't power

.net dictionary and lookup add / update

断了今生、忘了曾经 提交于 2020-08-17 23:51:33
问题 I am sick of doing blocks of code like this for various bits of code I have: if (dict.ContainsKey[key]) { dict[key] = value; } else { dict.Add(key,value); } and for lookups (i.e. key -> list of value) if (lookup.ContainsKey[key]) { lookup[key].Add(value); } else { lookup.Add(new List<valuetype>); lookup[key].Add(value); } Is there another collections lib or extension method I should use to do this in one line of code no matter what the key and value types are? e.g. dict.AddOrUpdate(key,value)

How to convert/transform a collection to another collection by element's property?

心不动则不痛 提交于 2020-08-17 23:51:23
问题 If I have a collection of an object in Kotlin, is there a quick way to get a collection of a certain property of those objects? I looked at a list of collection operations for Kotlin, but nothing stood out for me (but I may have overlooked something) In python it would be akin to: [person.name for person in persons] And I'd prefer to use a collections function instead of doing: var nameMap = mutableListOf<String>() persons.forEach{person -> nameMap.add(person.name)} I'm pretty lacking in

How to convert/transform a collection to another collection by element's property?

江枫思渺然 提交于 2020-08-17 23:48:07
问题 If I have a collection of an object in Kotlin, is there a quick way to get a collection of a certain property of those objects? I looked at a list of collection operations for Kotlin, but nothing stood out for me (but I may have overlooked something) In python it would be akin to: [person.name for person in persons] And I'd prefer to use a collections function instead of doing: var nameMap = mutableListOf<String>() persons.forEach{person -> nameMap.add(person.name)} I'm pretty lacking in

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

拜拜、爱过 提交于 2020-08-10 05:04:51
问题 I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it. data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) { constructor(title: String, groups: List<SidebarGroup>) : this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList()) } In the above code Platform declaration

Kotlin make constructor of data class accept both List and MutableList but store a mutable instance of them

匆匆过客 提交于 2020-08-10 05:04:29
问题 I want to make a data class which can accept both list and mutable-list and if the list is instance of MutableList then directly make it a property else if it is a List then convert it into a MutableList and then store it. data class SidebarCategory(val title: String, val groups: MutableList<SidebarGroup>) { constructor(title: String, groups: List<SidebarGroup>) : this(title, if (groups is MutableList<SidebarGroup>) groups else groups.toMutableList()) } In the above code Platform declaration

Collections.nCopies() vs. For-loop Initialization

可紊 提交于 2020-08-07 04:03:52
问题 I recently found out that you can initialize Lists in Java by calling the Collections.nCopies() method rather than using a for-loop. But that got me wondering, is there a performance advantage/disadvantage in using this method over a for-loop or is it just a simpler way of doing the same thing? 回答1: Since the collection returned by nCopies is immutable, the entries in this collection need not be "materialized". In other words, all that is needed is a space for a single object of type T ;

can we have a nested map as key within other map?

时光怂恿深爱的人放手 提交于 2020-08-05 08:02:46
问题 I have just started implementing data structures in Java and was wondering can we have a situation like this. Map<HashMap<String,String>,String> map = new HashMap<HashMap<String,String>,String>(); And if yes ,, please give a small example. If you don't found question relevant ,, please mention in comments, 回答1: You can do this, but you should not do so in most cases. The key to a map needs to be constant and its equals and hashcode need to be set up to give the correct behavior. If you modify

Is foreach by-definition guaranteed to iterate the subject collection sequentially in Scala?

爱⌒轻易说出口 提交于 2020-08-01 09:42:42
问题 Is foreach by-definition guaranteed to iterate the subject collection (if it defines order) sequentially from the very first to the very last (unless accidentally interrupted) element? Aren't there any compiler optimization switches which can brake it (shuffle the sequence) or plans to make the ordinary foreach parallel in future versions? 回答1: Foreach is guaranteed to be sequential for sequential collections (that is, the normal hierarchy, or for anything transformed by .seq ). The parallel

Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet

和自甴很熟 提交于 2020-07-28 05:55:08
问题 The bounty expires in 2 days . Answers to this question are eligible for a +50 reputation bounty. Concurrent Bhai is looking for a more detailed answer to this question. I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet . I said "no" but according to him the answer is "yes". How is it even possible? 回答1: Your interviewer is right, they do not hold equivalence relation for