collections

Generics and wildcards with collections in Java

故事扮演 提交于 2020-01-13 12:17:38
问题 In a test class using AssertJ, I have code similar to the following: public void someTest() { assertThat(getNames()).has(sameNamesAs(getExpectedNames())); assertThat(getNames()).doesNotHave(sameNamesAs(getOtherNames())); } private List<String> getNames() { return null; } private List<String> getExpectedNames() { return null; } private List<String> getOtherNames() { return null; } private Condition<List<String>> sameNamesAs(List<String> rhs) { return new Condition<List<String>>("same names as

Get an item from a Java Set

孤街浪徒 提交于 2020-01-13 09:37:07
问题 I have a Set of expensive objects. These objects have IDs and the equals uses these IDs for equality. These objects' type has two constructors; one for the expensive object, and one that just sets the ID. So I can check if a particular ID is in the Set using Set.contains(new Object(ID)) . However, having determined the object is in the set, I cannot get the object instance in the set. How can I get the exact object that the set contains? 回答1: Consider using the UnifiedSet class in Eclipse

How do you rotate (circular shift) of a Scala collection

有些话、适合烂在心里 提交于 2020-01-13 08:48:34
问题 I can do this quite easily, and cleanly, using a for loop. For instance, if I wanted to traverse a Seq from every element back to itself I would do the following: val seq = Seq(1,2,3,4,5) for (i <- seq.indices) { for (j <- seq.indices) { print(seq(i + j % seq.length)) } } But as I'm looking to fold over the collection, I'm wondering if there is a more idiomatic approach. A recursive approach would allow me to avoid any var s. But basically, I'm wondering if something like the following is

Are auto-adding indexers on Dictionaries and Collections a good design decision?

不羁岁月 提交于 2020-01-13 08:32:51
问题 When is it acceptable for an indexer to automatically add items to a collection/dictionary? Is this reasonable, or contrary to best practices? public class I { /* snip */ } public class D : Dictionary<string, I> { public I this[string name] { get { I item; if (!this.TryGetValue(name, out item)) { item = new I(); this.Add(name, item); } return item; } } } Sample of how this may be used in a collection: public class I { public I(string name) {/* snip */} public string Name { get; private set; }

Not able to understand how to define a List of List in java [duplicate]

萝らか妹 提交于 2020-01-13 06:57:23
问题 This question already has answers here : Incompatible types List of List and ArrayList of ArrayList (6 answers) Initialize List<List<Integer>> in Java (2 answers) Closed 2 years ago . I want to understand the difference between the two definitions and why the correct one is correct and the wrong is wrong. The one showing me the compile-error List<List<Integer>> arr2 = new ArrayList<ArrayList<Integer>>(); The error it gave me : try2.java:8: error: incompatible types: ArrayList<ArrayList

Hashcode() Vs Equals()

给你一囗甜甜゛ 提交于 2020-01-13 05:16:13
问题 I have below this two classes .. class Emp //implements Comparable { String name,job; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } int salary; public Emp(String n,String j,int sal) { name=n; job=j; salary=sal; } public void display() { System.out.println

Why do Scala parallel collections sometimes cause an OutOfMemoryError?

て烟熏妆下的殇ゞ 提交于 2020-01-13 02:13:11
问题 This takes around 1 second (1 to 1000000).map(_+3) While this gives java.lang.OutOfMemoryError: Java heap space (1 to 1000000).par.map(_+3) EDIT: I have standard scala 2.9.2 configuration. I am typing this on scala prompt. And in the bash i can see [ -n "$JAVA_OPTS" ] || JAVA_OPTS="-Xmx256M -Xms32M" AND i dont have JAVA_OPTS set in my env. 1 million integers = 8MB, creating list twice = 16MB 回答1: It seems definitely related to the JVM memory option and to the memory required to stock a

Fast copy of TList <T>?

北慕城南 提交于 2020-01-12 14:37:55
问题 Is there a fast way to copy a generic TList? Copy.Capacity := List.Count; for Item in List do Copy.Add (Item); is very slow. There seems to be no way to use CopyMemory since I can't get the memory adress of the internal array (which is obvious from an information hiding viewpoint). I miss something like List.Copy (Copy); which uses the knowledge of the internal representation to improve performance. Can it be done? 回答1: For the generic TList<T> it is simply not possible to implement the

Fast copy of TList <T>?

拥有回忆 提交于 2020-01-12 14:36:06
问题 Is there a fast way to copy a generic TList? Copy.Capacity := List.Count; for Item in List do Copy.Add (Item); is very slow. There seems to be no way to use CopyMemory since I can't get the memory adress of the internal array (which is obvious from an information hiding viewpoint). I miss something like List.Copy (Copy); which uses the knowledge of the internal representation to improve performance. Can it be done? 回答1: For the generic TList<T> it is simply not possible to implement the

Map in Map in Guava

▼魔方 西西 提交于 2020-01-12 13:48:08
问题 I have some code with Map<String, Map<String, String>> objects, which works (it is instantiated as a HashMap of HashMaps), but I wonder whether there is a better way to represent this data structure in Guava. I have considered Multimap , but while there is ListMultimap and SetMultimap in Guava, I have found no "MapMultimap". I have also checked Table, which seems to be more like it, but its name is making me uncomfortable: what I have is definitely not a table but a tree. (There is no overlap