collections

C# List Generic Extension Method vs Non-Generic

蹲街弑〆低调 提交于 2019-12-23 08:06:14
问题 This is a simple question (I hope), there are generic and non-generic methods in collection classes like List<T> that have methods such as Where and Where<T> . Example: List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; IEnumerable<int> evens = numbers.Where((x) => { return x % 2 == 0; }); IEnumerable<int> evens2 = numbers.Where<int>((x) => { return x % 2 == 0; }); Why use one over the other (Generic or Non-Generic)? 回答1: They're the same method (documentation here). The

using capitalize on a collection_select

浪子不回头ぞ 提交于 2019-12-23 07:58:25
问题 If this has been answered before I cannot find it. I have the following: = f.collection_select :sex_id, @sexes, :id, :name and this in the controller: @sexes = Sex.all the sexes are all stored in lowercase, like this: id|name 1|steer 2|heifer 3|holstein I need them to output with Capital First letters: Steer Heifer Holstein I tried: = f.collection_select :sex_id, @sexes, :id, :name.capitalize = f.collection_select :sex_id, @sexes, 'id', 'name'.capitalize but they do not work, and I didn't

Should I use Double as keys in a TreeMap?

99封情书 提交于 2019-12-23 07:29:15
问题 As described in the answer to Double in HashMap, Doubles shouldn't be used in HashMaps because they are difficult to compare for equality. I believe my case is different, but I thought I'd ask to make sure since I didn't see anything about this. I'm going to have a series of double values associated with objects, and I want them to be sorted by the double values. Is TreeMap an appropriate solution? Would there be a better one? The double values are generated a bunch of math, so the likelihood

Should I use Double as keys in a TreeMap?

风流意气都作罢 提交于 2019-12-23 07:29:04
问题 As described in the answer to Double in HashMap, Doubles shouldn't be used in HashMaps because they are difficult to compare for equality. I believe my case is different, but I thought I'd ask to make sure since I didn't see anything about this. I'm going to have a series of double values associated with objects, and I want them to be sorted by the double values. Is TreeMap an appropriate solution? Would there be a better one? The double values are generated a bunch of math, so the likelihood

Preventing Duplicate List<T> Entries

半腔热情 提交于 2019-12-23 07:09:08
问题 I expect I'll be able to make a work around but I can't for the life of me understand why this code is not functioning correctly and allowing duplicate entries to be added to the List. The if statement condition is never met, even when I drag identical files in from the same location. I don't understand why the "Contains" method isn't matching them up. public class Form1:Form { private List<FileInfo> dragDropFiles = new List<FileInfo>(); private void Form1_DragDrop(object sender,

Iterating over Dictionary with foreach, in what order is this done?

回眸只為那壹抹淺笑 提交于 2019-12-23 07:02:32
问题 Say I have a Dictionary , and I add each key and value entry in a specific order. Now, if I want later to be able to iterate this Dictionary in the same order entries were added, is it the order I get with simple foreach loop on this dictionary? If not, I will be glad to hear how can I do that, I know this can be done easily with List instead of Dictionary but I don't want to. Thanks 回答1: Normal Dictionary does not guarantee order of items. You need OrderedDictionary if you want to maintain

Understanding mutable Seq

北城以北 提交于 2019-12-23 06:58:42
问题 I'm pretty new to Scala and try to understand mutable Seq . Since it's in package mutable I expected there is a method that allows us to append element without copying the whole collection. But there is no += method in the mutable.Seq , but in Buffer is. :+ and +: both copy the collection. So why is it mutable? 回答1: Because mutable and growable isn't the same thing. (the latter is one specific type of the former: everything, that's growable is mutable, but not everything that's mutable is

JDK implementation of AbstractList::equals() does not check for list size equality first

对着背影说爱祢 提交于 2019-12-23 06:57:17
问题 Strangely the default JDK 6 implementation of AbstractList::equals() does not seems to check first if the two lists have the same size : public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof List)) return false; ListIterator<E> e1 = listIterator(); ListIterator e2 = ((List) o).listIterator(); while(e1.hasNext() && e2.hasNext()) { E o1 = e1.next(); Object o2 = e2.next(); if (!(o1==null ? o2==null : o1.equals(o2))) return false; } return !(e1.hasNext() || e2.hasNext()

Why does the Java List interface not support getLast()?

妖精的绣舞 提交于 2019-12-23 06:49:15
问题 I'm trying to understand the API inconsistency in the Java standard collections library. There is no method in List or in AbstractList to get the last item, although one can simulate that with size and getIndex(). However, LinkedList supports that function. Any idea why it was decided not to support this method in the interface? 回答1: The java.util.List interface doesn't support getLast() because the designers went for a 'minimal interface'. With the minimal number of methods defined it makes

Opposite of intersect in groovy collections

老子叫甜甜 提交于 2019-12-23 06:49:10
问题 what would be the opposite of intersect in groovy collections? 回答1: You probably want to combine both the answers from @Andre and @denis I think what you want is the union and then subtract the intersection from this def a = [1,2,3,4,5] def b = [2,3,4] assert [1,5] == ( a + b ) - a.intersect( b ) The solution given by denis would depend on whether you do def opposite = leftCollection-rightCollection // [1,5] or def opposite = rightCollection-leftCollection // [] which I don't think you wanted