collections

Type-safe rectangular multidimensional array type

ⅰ亾dé卋堺 提交于 2019-12-21 12:14:47
问题 How do you represent a rectangular 2-dimensional (or multidimensional) array data structure in Scala? That is, each row has the same length, verified at compile time , but the dimensions are determined at runtime? Seq[Seq[A]] has the desired interface, but it permits the user to provide a "ragged" array, which can result in a run-time failure. Seq[(A, A, A, A, A, A)] (and similar) does verify that the lengths are the same, but it also forces this length to be specified at compile time.

Java: ArrayList add() and remove() performance, implementation?

萝らか妹 提交于 2019-12-21 12:07:38
问题 I have read somewhere that ArrayList's add() and remove() operations run in "amortized constant" time. What does this mean exactly? In the implementation of add(item) I can see that it ArrayList uses an array buffer, which is at most 3/2 of the list't size, and if it is full, System.arraycopy() is called, which should execute in O(n), not O(1) time. Is it then that System.arraycopy attempts to do something smarter than copying elements one by one into newly created array, since the time is

Designing a convenient default valued map in Scala

我的未来我决定 提交于 2019-12-21 09:56:00
问题 I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]], and I'd like to have new Maps, Sets, etc. created automatically when I access a new key. E.g. something like the following: val m = ... m(1992)("foo") += "bar" Note that I don't want to use getOrElseUpdate here if I don't have to because it gets pretty verbose when you have nested maps and obscures what's actually going on in the code: m.getOrElseUpdate(1992, Map[String, Set[String]]()).getOrElseUpdate("foo",

Designing a convenient default valued map in Scala

自闭症网瘾萝莉.ら 提交于 2019-12-21 09:54:18
问题 I find myself using a lot of nested maps, e.g a Map[Int, Map[String, Set[String]]], and I'd like to have new Maps, Sets, etc. created automatically when I access a new key. E.g. something like the following: val m = ... m(1992)("foo") += "bar" Note that I don't want to use getOrElseUpdate here if I don't have to because it gets pretty verbose when you have nested maps and obscures what's actually going on in the code: m.getOrElseUpdate(1992, Map[String, Set[String]]()).getOrElseUpdate("foo",

Java Generics for Upper bound & lower bound wild cards

那年仲夏 提交于 2019-12-21 09:25:18
问题 I was reading java generics, I came across an interesting query. My question is as follows. For an upper bounded wildcard public static void printList(List<? extends Number> list) { for (int i = 0; i < 10; i++) { list.add(i);// gives compilation error } } For a lower bounded wildcard public static void printList(List<? super Integer> list) { for (int i = 0; i < 10; i++) { list.add(i);// successfully compiles } } I am confused with this because looking at the Sun Oracle documentation I

Finding duplicate entries in Collection

被刻印的时光 ゝ 提交于 2019-12-21 08:05:59
问题 Is there a tool or library to find duplicate entries in a Collection according to specific criteria that can be implemented? To make myself clear: I want to compare the entries to each other according to specific criteria. So I think a Predicate returning just true or false isn't enough. I can't use equals . 回答1: It depends on the semantic of the criterion: If your criterion is always the same for a given class, and is inherent to the underlying concept , you should just implement equals and

Why are ConcurrentSkipListSet ascending Iterators 'faster' than descending ones?

情到浓时终转凉″ 提交于 2019-12-21 07:25:12
问题 I’m using the descendingIterator method on ConcurrentSkipListSet. I’ve just checked the documentation and noticed the following comment: ‘Ascending ordered views and their iterators are faster than descending ones.’ See https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#descendingIterator-- Unfortunately it doesn’t provide any more information on this. What kind of performance difference is there? is it significant? and why is there a performance

adding a key to HashMap without the value?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 07:16:37
问题 Is there a way to add a key to a HashMap without also adding a value? I know it seems strange, but I have a HashMap<String, ArrayList<Object>> amd I want to first be able to create keys as needed and then check if a certain key exists and, if so, put the appropriate value, namely the ArrayList<Object> Was that confusing enough? 回答1: Since you're using a Map<String, List<Object>> , you're really looking for a multimap. I highly recommend using a third-party library such as Google Guava for

hashCode and equals for Collections.unmodifiableCollection()

为君一笑 提交于 2019-12-21 07:16:35
问题 The Collections class has a number of static helper methods to provide read-only views of various collection types, such as unmodifiableSet() , unmodifiableList() , etc. For these view objects, the hashCode() and equals() methods forward calls to the underlying collection... With one odd exception: unmodifiableCollection() . The JavaDoc explicitly states: The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object 's equals

How can I detect changes to item properties in the BindingList<T>?

假如想象 提交于 2019-12-21 07:13:13
问题 I have a custom class Foo with properties A and B. I want to display it in a databinding control. I have created a class Foos : BindingList<Foo> . In order to update some internal properties of the Foos class I need to be notified of property changes (I can handle insertions, removals etc.) on the items in the list. How would you implement that functionality ? Should I inherit Foo from some object in the framework that supports that ? I think I could create events that notify me if changes,