collections

Which collection for storing unique strings?

落爺英雄遲暮 提交于 2019-12-18 20:31:09
问题 I'm looking for a collection just like Dictionary(OF Key, Value) but I don't actually need a key and value. Key itself is enough. So something like Collection(Key) . It shouldn't accept duplicate keys. I've looked up couple of collections in .NET Framework but couldn't find what I want. Currently I'm abusing Dictionary(OF String, String) and setting Value as Nothing all the time. Shall I just continue abusing Dictionary(OF T,T)? 回答1: I think what you want is a HashSet<T>. 回答2: HashSet<T>

customising serialisation of java collections using xstream

喜你入骨 提交于 2019-12-18 19:31:53
问题 I have an object that needs to be serialised as XML, which contains the following field: List<String> tags = new List<String>(); XStream serialises it just fine (after some aliases) like this: <tags> <string>tagOne</string> <string>tagTwo</string> <string>tagThree</string> <string>tagFour</string> </tags> That's OK as far as it goes, but I'd like to be able to rename the <string> elements to, say, <tag> . I can't see an obvious way to do that from the alias documentation on the XStream site.

How to modify python collections by filtering in-place?

寵の児 提交于 2019-12-18 19:18:43
问题 I was wondering, if there is way in Python to modify collections without creating new ones. E.g.: lst = [1, 2, 3, 4, 5, 6] new_lst = [i for i in lst if i > 3] Works just fine, but a new collection is created. Is there a reason, that Python collections lack a filter() method (or similar) that would modify the collection object in place? 回答1: The other answers are correct; if you want all the names pointing to the old list to point to the new list you can use slice assignment. However, that's

Which is faster, the List<T>.Remove(T) or List<T>.RemoveAt(int) method?

一曲冷凌霜 提交于 2019-12-18 19:16:12
问题 Is List<T>.Remove(T) faster than the List<T>.RemoveAt(int) method in .NET collections? Is speed different for value types or reference types? 回答1: List.Remove(T) uses IndexOf and RemoveAt(int) in its implementation. So List.RemoveAt(int) is faster. public bool Remove(T item) { int index = this.IndexOf(item); if (index >= 0) { this.RemoveAt(index); return true; } return false; } 回答2: Simple answer: In general, RemoveAt is quicker, though not always hugely. Long answer: Let's just consider

Implementing a concurrent LinkedHashMap

旧街凉风 提交于 2019-12-18 19:13:20
问题 I'm trying to create a concurrent LinkedHashMap for a multithreaded architecture. If I use Collections#synchronizedMap() , I would have to use synchronized blocks for iteration. This implementation would lead to sequential addition of elements. If I use ConcurrentSkipListMap is there any way to implement a Comparator to store sequentially, as stored in Linked List or queue. I would like to use java's built in instead of third party packages. EDIT: In this concurrent LinkedHashMap , if the

Implementing a concurrent LinkedHashMap

前提是你 提交于 2019-12-18 19:13:09
问题 I'm trying to create a concurrent LinkedHashMap for a multithreaded architecture. If I use Collections#synchronizedMap() , I would have to use synchronized blocks for iteration. This implementation would lead to sequential addition of elements. If I use ConcurrentSkipListMap is there any way to implement a Comparator to store sequentially, as stored in Linked List or queue. I would like to use java's built in instead of third party packages. EDIT: In this concurrent LinkedHashMap , if the

Generic Map of Generic key/values with related types

大兔子大兔子 提交于 2019-12-18 18:56:23
问题 I'm trying to create a generic type that keeps a map of the versions of itself that have been created for later use. Effectively, it's an singleton pattern where there's one instance per type. The code I have so far is: public class FieldBinder<T> { static final Map<Class<? extends Object>,FieldBinder<? extends Object>> instanceMap = new HashMap<Class<? extends Object>,FieldBinder<? extends Object>>(); private FieldBinder() {} synchronized public static <V extends Object> FieldBinder<V>

Generic Map of Generic key/values with related types

。_饼干妹妹 提交于 2019-12-18 18:56:18
问题 I'm trying to create a generic type that keeps a map of the versions of itself that have been created for later use. Effectively, it's an singleton pattern where there's one instance per type. The code I have so far is: public class FieldBinder<T> { static final Map<Class<? extends Object>,FieldBinder<? extends Object>> instanceMap = new HashMap<Class<? extends Object>,FieldBinder<? extends Object>>(); private FieldBinder() {} synchronized public static <V extends Object> FieldBinder<V>

C#: When adding the same object to two List<object> variables, is the object cloned in the process?

风流意气都作罢 提交于 2019-12-18 18:54:04
问题 I have something similar to this: // Declarations: List<SomeType> list1 = new List<SomeType>(); List<SomeType> list2 = new List<SomeType>(); ... SomeType something = new SomeType("SomeName"); list1.Add(something); list2.Add(something); ... list1[indexOfSomething] = new SomeType("SomeOtherName"); And the object in list2 isn't changed... Is that the expected result? 回答1: Yes, but nothing's cloned. Before the assignment, the same object is in both lists. After the assignment, you have two unique

Why HashSet<T> does not implement IReadOnlyCollection<T>?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 18:53:47
问题 I've just found that .NET Fx now has 3 useful interfaces: IReadOnlyCollection<T> IReadOnlyList<T> IReadOnlyDictionary<K,V> And I'm bit confused why HashSet<T> do not implement IReadOnlyCollection<T> ? Are there any reasons, or Microsoft just forgot about sets again? UPD After two-hours googling I've found that there are many collections in BCL which has .Count property but do not implement IReadOnlyCollection<T> interface. UPD2 I've found this post http://social.msdn.microsoft.com/Forums/en