我有一个List<String>
包含国家/地区名称的对象.如何按字母顺序对此列表进行排序?
我有一个返回的方法IEnumerable<KeyValuePair<string, ArrayList>>
,但有些调用者要求方法的结果是字典.如何将其IEnumerable<KeyValuePair<string, ArrayList>>
转换为Dictionary<string, ArrayList>
可以使用的TryGetValue
?
方法:
public IEnumerable<KeyValuePair<string, ArrayList>> GetComponents()
{
// ...
yield return new KeyValuePair<string, ArrayList>(t.Name, controlInformation);
}
Run Code Online (Sandbox Code Playgroud)
呼叫者:
Dictionary<string, ArrayList> actual = target.GetComponents();
actual.ContainsKey("something");
Run Code Online (Sandbox Code Playgroud) 在Java中,我有一个Set
,我想把它变成一个排序的List
.java.util.Collections
包中有一个方法可以帮我吗?
Entry<K,V>
如果密钥未知,是否有一种优雅的方式只从HashMap 中获取一个,而不进行迭代.
由于进入的顺序并不重要,我们可以说类似的东西
hashMapObject.get(zeroth_index);
Run Code Online (Sandbox Code Playgroud)
虽然我知道没有这样的get by index方法.
如果我尝试下面提到的方法,它仍然必须得到hashmap的所有条目集.
for(Map.Entry<String, String> entry : MapObj.entrySet()) {
return entry;
}
Run Code Online (Sandbox Code Playgroud)
欢迎提出建议.
编辑:请建议任何其他数据结构满足要求.
我想存储一些像素位置而不允许重复,所以首先想到的是HashSet<Point>
或类似的类.然而,与类似的情况相比,这似乎非常缓慢HashSet<string>
.
例如,这段代码:
HashSet<Point> points = new HashSet<Point>();
using (Bitmap img = new Bitmap(1000, 1000))
{
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; y++)
{
points.Add(new Point(x, y));
}
}
}
Run Code Online (Sandbox Code Playgroud)
大约需要22.5秒.
虽然以下代码(由于显而易见的原因不是一个好的选择)只需1.6秒:
HashSet<string> points = new HashSet<string>();
using (Bitmap img = new Bitmap(1000, 1000))
{
for (int x = 0; x < img.Width; x++)
{
for (int y = 0; y < img.Height; …
Run Code Online (Sandbox Code Playgroud) 虽然我们可以从基类/接口继承,但为什么我们不能声明List<>
使用相同的类/接口?
interface A
{ }
class B : A
{ }
class C : B
{ }
class Test
{
static void Main(string[] args)
{
A a = new C(); // OK
List<A> listOfA = new List<C>(); // compiler Error
}
}
Run Code Online (Sandbox Code Playgroud)
有办法吗?
我有一个场景,我有多个线程添加到队列和多个线程从同一队列读取.如果队列达到特定大小,则填充队列的所有线程将在添加时被阻止,直到从队列中删除项目为止.
下面的解决方案就是我现在正在使用的问题,我的问题是:如何改进?是否有一个对象已经在我应该使用的BCL中启用此行为?
internal class BlockingCollection<T> : CollectionBase, IEnumerable
{
//todo: might be worth changing this into a proper QUEUE
private AutoResetEvent _FullEvent = new AutoResetEvent(false);
internal T this[int i]
{
get { return (T) List[i]; }
}
private int _MaxSize;
internal int MaxSize
{
get { return _MaxSize; }
set
{
_MaxSize = value;
checkSize();
}
}
internal BlockingCollection(int maxSize)
{
MaxSize = maxSize;
}
internal void Add(T item)
{
Trace.WriteLine(string.Format("BlockingCollection add waiting: {0}", Thread.CurrentThread.ManagedThreadId));
_FullEvent.WaitOne();
List.Add(item);
Trace.WriteLine(string.Format("BlockingCollection …
Run Code Online (Sandbox Code Playgroud) 通过使用大量编程语言和库,我注意到用于集合中元素总数的各种术语.
最常见的似乎是length
,count
和size
.
例如.
array.length
vector.size()
collection.count
Run Code Online (Sandbox Code Playgroud)
是否有任何首选术语?它取决于它是什么类型的集合?即.可变/不可变
是否倾向于将其作为属性而不是方法?
我是Java的初学者.请建议可以/应该使用哪些集合来维护Java中的排序列表.我曾尝试Map
和Set
,但他们不是我所期待的.
除了HashSet
不允许重复值的事实之外,HashMap
和之间有什么区别HashSet
?
我的意思是实施明智?它有点模糊,因为它们都使用哈希表来存储值.
collections ×10
java ×5
c# ×4
.net ×2
hashset ×2
list ×2
sorting ×2
alphabetical ×1
covariance ×1
dictionary ×1
hashmap ×1
idictionary ×1
ienumerable ×1
inheritance ×1
performance ×1
queue ×1
semantics ×1
terminology ×1