collections

Comparing two lists and removing duplicates from one

落爺英雄遲暮 提交于 2019-12-19 08:43:09
问题 I have an object called FormObject that contains two ArrayLists - oldBooks and newBooks - both of which contain Book objects. oldBooks is allowed to contain duplicate Book objects newBooks is not allowed to contain duplicate Book objects within itself and cannot include any duplicates of Book objects in the oldBooks list. The definition of a duplicate Book is complex and I can't override the equals method as the definition is not universal across all uses of the Book object. I plan to have a

Comparing two lists and removing duplicates from one

蹲街弑〆低调 提交于 2019-12-19 08:42:02
问题 I have an object called FormObject that contains two ArrayLists - oldBooks and newBooks - both of which contain Book objects. oldBooks is allowed to contain duplicate Book objects newBooks is not allowed to contain duplicate Book objects within itself and cannot include any duplicates of Book objects in the oldBooks list. The definition of a duplicate Book is complex and I can't override the equals method as the definition is not universal across all uses of the Book object. I plan to have a

How can collections use implicit conversions on element types?

本小妞迷上赌 提交于 2019-12-19 08:31:31
问题 While working on this question, I came up with the following issue. Consider two method definitions: def foo[T <: Ordered[T]](s : Seq[T]) = s.sorted def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted The first one compiles, the second does not. The compiler does not figure out that it can use the asserted implicit conversion to get an Ordering . If we help a bit, it works: def foo[T <% Ordered[T]](s : Seq[T]) = s.sortWith(_<=_) While compiling the anonymous function the compiler applies the

How can collections use implicit conversions on element types?

柔情痞子 提交于 2019-12-19 08:31:03
问题 While working on this question, I came up with the following issue. Consider two method definitions: def foo[T <: Ordered[T]](s : Seq[T]) = s.sorted def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted The first one compiles, the second does not. The compiler does not figure out that it can use the asserted implicit conversion to get an Ordering . If we help a bit, it works: def foo[T <% Ordered[T]](s : Seq[T]) = s.sortWith(_<=_) While compiling the anonymous function the compiler applies the

C# Cannot convert from IEnumerable<Base> to IEnumerable<Derived>

◇◆丶佛笑我妖孽 提交于 2019-12-19 07:39:09
问题 I recently run into trouble when trying to AddRange(IEnumerable) to a List. Probably a classic issue, but I do not really get it yet. I understand that methods expecting a List parameter are not satisfied with a List, because they might try to add a Base to the List, which is obviously impossible. But if i get this correctly, since IEnumerables themselves cannot be changed, it ought to work in this case. The code i thought of looks like this: class Foo { } class Bar : Foo { } class FooCol {

Which value types in Swift supports copy-on-write?

我们两清 提交于 2019-12-19 07:16:10
问题 I read about copy-on-write implementation for Array in Swift here. Arrays, like all variable-size collections in the standard library, use copy-on-write optimization. Multiple copies of an array share the same storage until you modify one of the copies. When that happens, the array being modified replaces its storage with a uniquely owned copy of itself, which is then modified in place. Optimizations are sometimes applied that can reduce the amount of copying. I was wondering if you have any

Transposing arbitrary collection-of-collections in Scala

独自空忆成欢 提交于 2019-12-19 06:02:44
问题 I have to often transpose a "rectangular" collection-of-collections in Scala, e.g.: a list of maps, a map of lists, a map of maps, a set of lists, a map of sets etc. Since collections can be uniformly viewed as a mapping from a specific domain to a co-domain (e.g.: a List[A]/Array[A] is a mapping from the Int domain to the A co-domain, Set[A]is a mapping from the A domain to the Boolean co-domain etc.), I'd like to write a clean, generic function to do a transpose operation (e.g.: turn a map

What does it mean that Java arrays are homogeneous, but ArrayLists are not?

杀马特。学长 韩版系。学妹 提交于 2019-12-19 05:22:27
问题 If we have a Type[], we can only store Type or its subtypes in it. The same goes for ArrayList. So why is it said that one is homogeneous while the other is not? 回答1: Arrays have a runtime check on the type of the added element. That is, if a new element that is not of the same type is added, an ArrayStoreException is thrown at runtime. That's why they are considered as "homegeneous". This is not true for ArrayList s ( List s in general). Due to type erasure at runtime, it can practically

Compiler error on Java generic interface with a List<> method [duplicate]

旧巷老猫 提交于 2019-12-19 05:15:14
问题 This question already has answers here : What is a raw type and why shouldn't we use it? (15 answers) Closed last year . I don't understand the compiler error resulting from the following code. I define a generic interface, see Task, with two methods: U doSomething(String value) and List<Integer> getIDs() . The doSomething() method actually uses the generic type as the type of its return value, but doesn't seem to be causing problems. The getIDs() method returns a List, which is unrelated to

Any Intersection in Two Collections

こ雲淡風輕ζ 提交于 2019-12-19 05:14:38
问题 i have to find out whether or not two collections have any intersection, the way that i did that is using LINQ's "Join" to get the Intersection of the two collections and then i use "Any". But i wonder, is there other more "elegant" way of doing this? 回答1: Enumerable.Intersect is probably what you're looking for. From MSDN: int[] id1 = { 44, 26, 92, 30, 71, 38 }; int[] id2 = { 39, 59, 83, 47, 26, 4, 30 }; IEnumerable<int> both = id1.Intersect(id2); if(both.Any())... 回答2: bool intersects =