collections

How to use Sets as keys in Java Maps

╄→尐↘猪︶ㄣ 提交于 2020-01-09 07:35:50
问题 I have a Map that uses a Set for the key type, like this: Map<Set<Thing>, Val> map; When I query map.containsKey(myBunchOfThings), it returns false, and I don't understand why. I can iterate through each key in the keyset and verify there is a key that (1) has the same hashCode, and (2) is equals() to myBunchOfThings. System.out.println(map.containsKey(myBunchOfThings)); // false. for (Set<Thing> k : map.keySet()) { if (k.hashCode() == myBunchOfThings.hashCode() && k.equals(myBunchOfThings) {

How to use Sets as keys in Java Maps

懵懂的女人 提交于 2020-01-09 07:35:07
问题 I have a Map that uses a Set for the key type, like this: Map<Set<Thing>, Val> map; When I query map.containsKey(myBunchOfThings), it returns false, and I don't understand why. I can iterate through each key in the keyset and verify there is a key that (1) has the same hashCode, and (2) is equals() to myBunchOfThings. System.out.println(map.containsKey(myBunchOfThings)); // false. for (Set<Thing> k : map.keySet()) { if (k.hashCode() == myBunchOfThings.hashCode() && k.equals(myBunchOfThings) {

Does the Enumerator of a Dictionary<TKey, TValue> return key value pairs in the order they were added?

馋奶兔 提交于 2020-01-09 03:45:07
问题 I understand that a dictionary is not an ordered collection and one should not depend on the order of insertion and retrieval in a dictionary. However, this is what I noticed: Added 20 key value pairs to a Dictionary Retrieved them by doing a foreach(KeyValuePair...) The order of retrieval was same as the order in which they were added. Tested for around 16 key value pairs. Is this by design? 回答1: It's by coincidence, although predictably so. You absolutely shouldn't rely on it. Usually it

How should I use properties when dealing with read-only List<T> members

陌路散爱 提交于 2020-01-09 03:20:28
问题 When I want to make a value type read-only outside of my class I do this: public class myClassInt { private int m_i; public int i { get { return m_i; } } public myClassInt(int i) { m_i = i; } } What can I do to make a List<T> type readonly (so they can't add/remove elements to/from it) outside of my class? Now I just declare it public: public class myClassList { public List<int> li; public myClassList() { li = new List<int>(); li.Add(1); li.Add(2); li.Add(3); } } 回答1: You can expose it

Is there an insertion order preserving Set that also implements List?

纵饮孤独 提交于 2020-01-09 03:12:47
问题 I'm trying to find an implementation of java.util.List and java.util.Set at the same time in Java. I want this class to allow only unique elements (as Set ) and preserve their order (like List ). Does it exist in JDK 6? It's important to have List<T>#add(int, T) so I can insert into a specific position. 回答1: TreeSet is sorted by element order; LinkedHashSet retains insertion order. Hopefully one of those is what you were after. You've specified that you want to be able to insert at an

Is there an insertion order preserving Set that also implements List?

自古美人都是妖i 提交于 2020-01-09 03:12:28
问题 I'm trying to find an implementation of java.util.List and java.util.Set at the same time in Java. I want this class to allow only unique elements (as Set ) and preserve their order (like List ). Does it exist in JDK 6? It's important to have List<T>#add(int, T) so I can insert into a specific position. 回答1: TreeSet is sorted by element order; LinkedHashSet retains insertion order. Hopefully one of those is what you were after. You've specified that you want to be able to insert at an

How to extend a Scala list to enable slicing not by explicit position but by given predicate/condition

本小妞迷上赌 提交于 2020-01-08 18:01:10
问题 For trait Item case class TypeA(i: Int) extends Item case class TypeB(i: Int) extends Item consider a Scala list of items such as val myList = List(TypeA(1), TypeB(11), TypeB(12), TypeA(2), TypeB(21), TypeA(3), TypeB(31)) The goal is to define a new slice method that can be applied onto myList and which takes a predicate or condition as argument; for instance myList.slice { x => x.isInstanceOf[TypeA] } would deliver List(List(TypeA(1), TypeB(11), TypeB(12)), List(TypeA(2), TypeB(21)), List

best way to pick a random subset from a collection?

女生的网名这么多〃 提交于 2020-01-08 16:05:29
问题 I have a set of objects in a Vector from which I'd like to select a random subset (e.g. 100 items coming back; pick 5 randomly). In my first (very hasty) pass I did an extremely simple and perhaps overly clever solution: Vector itemsVector = getItems(); Collections.shuffle(itemsVector); itemsVector.setSize(5); While this has the advantage of being nice and simple, I suspect it's not going to scale very well, i.e. Collections.shuffle() must be O(n) at least. My less clever alternative is

Collection was modified; enumeration operation may not execute in ArrayList [duplicate]

谁都会走 提交于 2020-01-08 11:25:41
问题 This question already has answers here : How to remove elements from a generic list while iterating over it? (26 answers) Closed 6 years ago . I'm trying to remove an item from an ArrayList and I get this Exception: Collection was modified; enumeration operation may not execute. Any ideas? 回答1: You are removing the item during a foreach , yes? Simply, you can't. There are a few common options here: use List<T> and RemoveAll with a predicate iterate backwards by index, removing matching items

How can I return a list of rows of dynamic table? [duplicate]

℡╲_俬逩灬. 提交于 2020-01-07 08:36:12
问题 This question already has answers here : DataTable iteration gives unwanted data (2 answers) Anonymous type result from sql query execution entity framework (5 answers) Closed 5 days ago . My previous question was closed due to typo. But my issue is unresolved. So rephrasing the question. Hi, I have a query which returns data dynamically. Number of columns, their name and datatypes are not known. It's like a Select * from table_name I need to get the result in a list. I tried with IEnumerable