我需要将键/对对添加到字典中,但我当然需要首先检查密钥是否已经存在,否则我得到" 密钥已存在于字典中 "错误.下面的代码解决了这个问题,但很笨重.
在没有像这样的字符串帮助方法的情况下,这样做更优雅的方法是什么?
using System;
using System.Collections.Generic;
namespace TestDictStringObject
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, object> currentViews = new Dictionary<string, object>();
StringHelpers.SafeDictionaryAdd(currentViews, "Customers", "view1");
StringHelpers.SafeDictionaryAdd(currentViews, "Customers", "view2");
StringHelpers.SafeDictionaryAdd(currentViews, "Employees", "view1");
StringHelpers.SafeDictionaryAdd(currentViews, "Reports", "view1");
foreach (KeyValuePair<string, object> pair in currentViews)
{
Console.WriteLine("{0} {1}", pair.Key, pair.Value);
}
Console.ReadLine();
}
}
public static class StringHelpers
{
public static void SafeDictionaryAdd(Dictionary<string, object> dict, string key, object view)
{
if (!dict.ContainsKey(key))
{
dict.Add(key, view);
}
else
{
dict[key] = view; …
Run Code Online (Sandbox Code Playgroud) 我有一个List<SomeBean>
从Web服务填充的.我想将该列表的内容复制/克隆到相同类型的空列表中.谷歌搜索复制列表建议我使用Collections.copy()
方法.在我看到的所有示例中,目标列表应该包含要进行复制的确切项目数.
由于我使用的列表是通过Web服务填充的,它包含数百个对象,我不能使用上述技术.或者我使用它错了?? !! 无论如何,为了使它工作,我试图做这样的事情,但我仍然有一个IndexOutOfBoundsException
.
List<SomeBean> wsList = app.allInOne(template);
List<SomeBean> wsListCopy=new ArrayList<SomeBean>(wsList.size());
Collections.copy(wsListCopy,wsList);
System.out.println(wsListCopy.size());
Run Code Online (Sandbox Code Playgroud)
我尝试使用wsListCopy=wsList.subList(0, wsList.size())
但ConcurrentAccessException
后来我在代码中得到了一个.命中和审判.:)
无论如何,我的问题很简单,如何将列表的整个内容复制到另一个列表?当然不是通过迭代.
我正在寻找一种非常快速的方法来过滤C#中的集合.我目前正在使用通用的List <object>集合,但如果它们表现更好,我愿意使用其他结构.
目前,我只是创建一个新的List <object>并循环到原始列表.如果过滤条件匹配,我将副本放入新列表.
有一个更好的方法吗?有没有办法过滤到位,所以不需要临时列表?
是否可以在MongoDB中显示所有集合及其内容?
是一个一个一个展示的唯一方法吗?
我自己写了一个实用程序来将列表分成给定大小的批次.我只是想知道是否已经有任何apache commons util.
public static <T> List<List<T>> getBatches(List<T> collection,int batchSize){
int i = 0;
List<List<T>> batches = new ArrayList<List<T>>();
while(i<collection.size()){
int nextInc = Math.min(collection.size()-i,batchSize);
List<T> batch = collection.subList(i,i+nextInc);
batches.add(batch);
i = i + nextInc;
}
return batches;
}
Run Code Online (Sandbox Code Playgroud)
如果已有相同的实用程序,请告诉我.
我是Java 8的新手.我仍然不深入了解API,但我已经做了一个小的非正式基准测试来比较新Streams API与优秀旧Collections的性能.
测试包括过滤一个列表Integer
,并为每个偶数计算平方根并将其存储在结果List
中Double
.
这是代码:
public static void main(String[] args) {
//Calculating square root of even numbers from 1 to N
int min = 1;
int max = 1000000;
List<Integer> sourceList = new ArrayList<>();
for (int i = min; i < max; i++) {
sourceList.add(i);
}
List<Double> result = new LinkedList<>();
//Collections approach
long t0 = System.nanoTime();
long elapsed = 0;
for (Integer i : sourceList) {
if(i % 2 == 0){
result.add(Math.sqrt(i));
} …
Run Code Online (Sandbox Code Playgroud) 在这个问题中如何在C++ 11中有效地选择标准库容器?是一个在选择C++集合时使用的方便流程图.
我认为这对于那些不确定他们应该使用哪个集合的人来说是一个有用的资源,所以我试图找到类似Java的流程图而无法这样做.
什么资源和"备忘单"可以帮助人们选择在Java编程时使用的正确的集合?人们如何知道应该使用哪些List,Set和Map实现?
在Collections.singletonList(something)上使用Arrays.asList(something)制作包含一个项目的列表是否有优势(或许多不同之处)?后者使得返回的列表也是不可变的.
我有HashMap
各种键和值,我怎么能得到一个值?
我在地图中有一个键my_code
,它应该包含一个字符串,我怎么能在不必迭代地图的情况下得到它?
到目前为止我已经......
HashMap newMap = new HashMap(paramMap);
String s = newMap.get("my_code").toString();
Run Code Online (Sandbox Code Playgroud)
我期待看到一个String
,如"ABC"或"DEF",因为这是我最初放在那里,但如果我做了一个System.out.println()
我得到像java.lang.string#F0454
对不起,我不太熟悉你可能猜到的地图;)
collections ×10
java ×7
c# ×2
hashmap ×2
algorithm ×1
arraylist ×1
copy ×1
dictionary ×1
filtering ×1
find ×1
java-8 ×1
java-stream ×1
mongodb ×1
performance ×1