collections

Get last element in a SortedDictionary

泄露秘密 提交于 2019-12-18 15:19:23
问题 I see this question. How can I get the last element in a SortedDictionary in .Net 3.5. 回答1: You can use LINQ: var lastItem = sortedDict.Values.Last(); You can also get the last key: var lastkey = sortedDict.Keys.Last(); You can even get the last key-value pair: var lastKeyValuePair = sortedDict.Last(); This will give you a KeyValuePair<TKey, TValue> with Key and Value properties. Note that this will throw an exception if the dictionary is empty; if you don't want that, call LastOrDefault .

Get last element in a SortedDictionary

╄→гoц情女王★ 提交于 2019-12-18 15:18:21
问题 I see this question. How can I get the last element in a SortedDictionary in .Net 3.5. 回答1: You can use LINQ: var lastItem = sortedDict.Values.Last(); You can also get the last key: var lastkey = sortedDict.Keys.Last(); You can even get the last key-value pair: var lastKeyValuePair = sortedDict.Last(); This will give you a KeyValuePair<TKey, TValue> with Key and Value properties. Note that this will throw an exception if the dictionary is empty; if you don't want that, call LastOrDefault .

Difference between CopyOnWriteArrayList and synchronizedList

邮差的信 提交于 2019-12-18 14:12:08
问题 As per my understanding concurrent collection classes preferred over synchronized collections because the concurrent collection classes don't take a lock on the complete collection object. Instead they take locks on a small segment of the collection object. But when I checked the add method of CopyOnWriteArrayList , we are acquiring a lock on complete collection object. Then how come CopyOnWriteArrayList is better than a list returned by Collections.synchronizedList ? The only difference I

Why does ArrayList implement RandomAccess Interface?

你离开我真会死。 提交于 2019-12-18 14:08:46
问题 ArrayList implements RandomAccess interface. RandomAccess interface has no methods. When I checked LinkedList it does not implement RandomAccess interface. So in case of ArrayList , what is the point of implementing it? 回答1: Interfaces with no methods are called marker interfaces in Java. As per the JavaDoc of RandomAccess: Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. For more information check the two JavaDoc pages.

Why does EnumSet have many overloaded “of” methods?

为君一笑 提交于 2019-12-18 13:53:19
问题 While going through the EnumSet<E> of method, I have seen multiple overloaded implementations of of method: public static <E extends Enum<E>> EnumSet<E> of(E e) public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) . . public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5) and then another overloaded method with varargs public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) { EnumSet<E> result = noneOf(first.getDeclaringClass()); result.add(first);

The correct way to return the only element from a set

六月ゝ 毕业季﹏ 提交于 2019-12-18 13:52:58
问题 I have the following kind of situation: Set<Element> set = getSetFromSomewhere(); if (set.size() == 1) { // return the only element } else { throw new Exception("Something is not right.."); } Assuming I cannot change the return type of getSetFromSomewhere() , is there a better or more correct way to return the only element in the set than Iterating over the set and returning immediately Creating a list from the set and calling .get(0) 回答1: You can use an Iterator to both obtain the only

Java: Enumeration from Set<String>

你。 提交于 2019-12-18 13:51:23
问题 I have a simple collections question. I have a Set<String> object. I want an enumeration of the strings in that set. What is the cleanest/best way to go about it? 回答1: EDIT: There's no need to write your own (although I'll leave the implementation below for posterity) - see Kevin Bourrillion's answer for the one in the JDK. If you really need an enumeration, could could use: Enumeration<String> x = new Vector(set).elements(); It would be better to use Iterable<E> if at all possible though...

ZF2 apigility - How can we validate collections in json data

北慕城南 提交于 2019-12-18 13:38:17
问题 How can I get validated json value using Apigility. For example, I need to get validated user_id under users collection in the following json data. { "log_type": "split food", "meal_type": "Break Fast", "meal_date": "12-2-2015", "users": [ { "user_id": 1, "food_details": [ { "food_id":101 } ] } ] } I know fields can be validated through apigility but here is from json. Thank you 回答1: You should look into the documentation of ZF2 validation for validating (form) collections. Some documentation

Why doesn't Collection extend Cloneable and Serializable? [closed]

心不动则不痛 提交于 2019-12-18 13:37:57
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . In Java library, what is the reason that the Collection interface doesn't extend Cloneable and Serializable interfaces? 回答1:

How to write a VBA collection to an Excel sheet [duplicate]

烈酒焚心 提交于 2019-12-18 13:21:29
问题 This question already has an answer here : Copy the values of a collection to a 2D array in VBA (1 answer) Closed 4 years ago . I have some existing code that I am modifying. This code creates a collection of rows from preexisting worksheet tables. It creates a large 2-D collection, with distinct information in each column. There is a separate class module that declares the data type for each column. The code writes the 2-D collection to a new sheet by looping through each item in turn. I