collections

Javascript collection

人盡茶涼 提交于 2019-12-17 21:38:33
问题 sorry for noobie question. Can you explain please, what is the difference between: 1. var a = []; a['b'] = 1; 2. var a = {}; a['b'] = 1; I could not find article in the internet so wrote here. 回答1: Literals The [] and {} are called the array and object literals respectively. var x = [] is short for var x = new Array(); and var y = {} is short for var y = new Object(); Arrays Arrays are structures with a length property. You can access values via their numeric index. var x = [] or var x = new

Java ArrayList of ? extends Interface

时光毁灭记忆、已成空白 提交于 2019-12-17 21:32:06
问题 I have a group of classes that all implement a validation interface which has the method isValid() . I want to put a group of objects--all of different classes--into an ArrayList, loop through them and call isValid() on each. Here's my code Email email = new email(); Address address = new Address(); ArrayList<? extends Validation> myValidationObjects = new ArrayList(); But when I try to do: myValidationObjects.add(email); I get: The method add(capture#2-of ? extends Validation) in the type

Why is Java's AbstractList's removeRange() method protected?

六月ゝ 毕业季﹏ 提交于 2019-12-17 21:27:16
问题 Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList) is protected ? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to subclass the List implementation. Is there some hidden rationale? Seems quite inexplicable to me. 回答1: Yes, because that's not how you remove a range from outside code. Instead, do this: list.subList(start, end).clear(); This actually calls removeRange behind the scenes. † The OP asks why

Map of Maps data structure

不羁的心 提交于 2019-12-17 21:03:44
问题 The MultiValueMap class (Apache commons collections) makes it easy to work with a Map whose values are Collections. I'm looking for a a class that makes it easy to work with a Map whose keys are objects and values are Maps. I'm using Java 1.4, so can't use Google Collections or generics. 回答1: Map of maps is actually a tree-type structure without single root node (as well as map of maps of maps...). You can look at Composite pattern which is widely used for implementing tree structures (if

How does an Iterator in Java know when to throw ConcurrentModification Exception

烈酒焚心 提交于 2019-12-17 20:42:33
问题 I have the following code which throws ConcurrentModificationException because I am using two different iterators on the same list and one of them is modifying the list. So, the second iterator throws the exception when reading the list because some other iterator has modified the list. List<Integer> list = new ArrayList<>(); populate(list);//A method that adds integers to list ListIterator<Integer> iterator1 = list.listIterator(); ListIterator<Integer> iterator2 = list.listIterator(); while

C# associative array

[亡魂溺海] 提交于 2019-12-17 20:34:22
问题 I've been using a Hashtable, but by nature, hashtables are not ordered, and I need to keep everything in order as I add them (because I want to pull them out in the same order). Forexample if I do: pages["date"] = new FreeDateControl("Date:", false, true, false); pages["plaintiff"] = new FreeTextboxControl("Primary Plaintiff:", true, true, false); pages["loaned"] = new FreeTextboxControl("Amount Loaned:", true, true, false); pages["witness"] = new FreeTextboxControl("EKFG Witness:", true,

Why inserting 1000 000 values in a transient map in Clojure yields a map with 8 items in it?

感情迁移 提交于 2019-12-17 20:06:35
问题 If I try to do 1000 000 assoc! on a transient vector, I'll get a vector of 1000 000 elements (count (let [m (transient [])] (dotimes [i 1000000] (assoc! m i i)) (persistent! m))) ; => 1000000 on the other hand, if I do the same with a map, it will only have 8 items in it (count (let [m (transient {})] (dotimes [i 1000000] (assoc! m i i)) (persistent! m))) ; => 8 Is there a reason why this is happening? 回答1: The transient datatypes' operations don't guarantee that they will return the same

Extending Collection with a recursive property/method that depends on the element type

南笙酒味 提交于 2019-12-17 20:03:43
问题 In the context of this question, I though about how one could implement a property or method that counts across all nesting levels in collections. Intuitively, something this should work: extension Collection { var flatCount: Int { if self.count == 0 { return 0 } else if self.first is Collection { // .Iterator.Element: Collection return self.reduce(0) { (res, elem) -> Int in res + (elem as! Collection).flatCount // ERROR } } else { return self.reduce(0) { (res,_) in res + 1 } } } } However,

Bounded, auto-discarding, non-blocking, concurrent collection

倾然丶 夕夏残阳落幕 提交于 2019-12-17 19:59:10
问题 I'm looking for a collection that: is a Deque / List - i.e. supports inserting elements at "the top" (newest items go to the top) - deque.addFirst(..) / list.add(0, ..) . It could be a Queue , but the iteration order should be reverse - i.e. the most recently added items should come first. is bounded - i.e. has a limit of 20 items auto-discards the oldest items (those "at the bottom", added first) when the capacity is reached non-blocking - if the deque is empty, retrievals should not block.

Collection/Array contains method

核能气质少年 提交于 2019-12-17 19:51:12
问题 i was wondering if there's contains method for collections/array in EL 2.2 or i will have to make a custom one ? REQUIREMENT : i have a string array, and i want to find if it contains a specific string. CASE : i am looping on list of input checkboxes to render them, and i want to check the current checkbox, if it's value exists in the array of checkboxes. UPDATE : is such method is available in EL? If such method is not available, then please provide your suggestion for best performance