collections

What is the difference between List (of T) and Collection(of T)?

百般思念 提交于 2019-12-17 02:59:25
问题 I've seen them used in a lot of the same ways, and I am worried I'm about to go down a path in design that is irreversible if I don't understand this better. Also, I am using .NET. 回答1: Collection<T> is a customizable wrapper around IList<T> . While IList<T> is not sealed, it doesn't provide any customization points. Collection<T> 's methods are by default delegated to the standard IList<T> methods, but can be easily overridden to do what you want. It is also possible to wireup events inside

Filtering collections in C#

前提是你 提交于 2019-12-17 02:44:08
问题 I am looking for a very fast way to filter down a collection in C#. I am currently using generic List<object> collections, but am open to using other structures if they perform better. Currently, I am just creating a new List<object> and looping thru the original list. If the filtering criteria matches, I put a copy into the new list. Is there a better way to do this? Is there a way to filter in place so there is no temporary list required? 回答1: If you're using C# 3.0 you can use linq, way

Collection<T> versus List<T> what should you use on your interfaces?

别说谁变了你拦得住时间么 提交于 2019-12-17 02:39:10
问题 The code looks like below: namespace Test { public interface IMyClass { List<IMyClass> GetList(); } public class MyClass : IMyClass { public List<IMyClass> GetList() { return new List<IMyClass>(); } } } When I Run Code Analysis i get the following recommendation. Warning 3 CA1002 : Microsoft.Design : Change 'List' in 'IMyClass.GetList()' to use Collection, ReadOnlyCollection or KeyedCollection How should I fix this and what is good practice here? 回答1: To answer the "why" part of the question

How can this function be rewritten to implement OrderedDict?

谁说胖子不能爱 提交于 2019-12-17 02:38:36
问题 I have the following function which does a crude job of parsing an XML file into a dictionary. Unfortunately, since Python dictionaries are not ordered, I am unable to cycle through the nodes as I would like. How do I change this so it outputs an ordered dictionary which reflects the original order of the nodes when looped with for . def simplexml_load_file(file): import collections from lxml import etree tree = etree.parse(file) root = tree.getroot() def xml_to_item(el): item = None if el

How can this function be rewritten to implement OrderedDict?

 ̄綄美尐妖づ 提交于 2019-12-17 02:38:17
问题 I have the following function which does a crude job of parsing an XML file into a dictionary. Unfortunately, since Python dictionaries are not ordered, I am unable to cycle through the nodes as I would like. How do I change this so it outputs an ordered dictionary which reflects the original order of the nodes when looped with for . def simplexml_load_file(file): import collections from lxml import etree tree = etree.parse(file) root = tree.getroot() def xml_to_item(el): item = None if el

How to sort an arraylist of objects by a property?

二次信任 提交于 2019-12-17 02:35:23
问题 Lets say you have an Arraylist of HockeyPlayer objects. How could you sort that if they all have a variable int goalsScored . How could you sort them by goalsScored? 回答1: You can use Collections.sort with a custom Comparator<HockeyPlayer>. class HockeyPlayer { public final int goalsScored; // ... }; List<HockeyPlayer> players = // ... Collections.sort(players, new Comparator<HockeyPlayer>() { @Override public int compare(HockeyPlayer p1, HockeyPlayer p2) { return p1.goalsScored - p2

ReadOnlyCollection or IEnumerable for exposing member collections?

为君一笑 提交于 2019-12-17 02:28:09
问题 Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection? class Bar { private ICollection<Foo> foos; // Which one is to be preferred? public IEnumerable<Foo> Foos { ... } public ReadOnlyCollection<Foo> Foos { ... } } // Calling code: foreach (var f in bar.Foos) DoSomething(f); As I see it IEnumerable is a subset of the interface of ReadOnlyCollection and it does not allow the user to modify

C# Set collection?

情到浓时终转凉″ 提交于 2019-12-17 02:26:57
问题 Does anyone know if there is a good equivalent to Java's Set collection in C#? I know that you can somewhat mimic a set using a Dictionary or a HashTable by populating but ignoring the values, but that's not a very elegant way. 回答1: Try HashSet: The HashSet(Of T) class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order... The capacity of a HashSet(Of T) object is the number of elements that the

Why do many Collection classes in Java extend the abstract class and implement the interface as well?

佐手、 提交于 2019-12-17 02:23:50
问题 Why do many Collection classes in Java extend the Abstract class and also implement the interface (which is also implemented by the given abstract class)? For example, class HashSet extends AbstractSet and also implements Set , but AbstractSet already implements Set . 回答1: It's a way to remember that this class really implements that interface. It won't have any bad effect and it can help to understand the code without going through the complete hierarchy of the given class. 回答2: From the

Why can Java Collections not directly store Primitives types?

时光怂恿深爱的人放手 提交于 2019-12-17 00:49:05
问题 Java collections only store Objects, not primitive types; however we can store the wrapper classes. Why this constraint? 回答1: It was a Java design decision, and one that some consider a mistake. Containers want Objects and primitives don't derive from Object. This is one place that .NET designers learned from the JVM and implemented value types and generics such that boxing is eliminated in many cases. In CLR, generic containers can store value types as part of the underlying container