我有一个像下面这样的结构.我在返回通用集合时遇到了一些麻烦.我错过了什么?
class Program
{
static void Main()
{
BusinessCollection businessCollection = new BusinessCollection();
//Why this is not working because businesscollection is a GenericCollection<BusinessEntity>
businessCollection = new GenericCollection<BusinessEntity>();
//or neither this
businessCollection = (BusinessCollection)new GenericCollection<BusinessEntity>();
}
}
public class BusinessEntity
{
public string Foo { get; set;}
}
public class BusinessCollection : GenericCollection<BusinessEntity>
{
//some implementation here
}
public class GenericCollection<T> : ICollection<T>
{
//some implementation here
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个IEnumerable类的扩展方法,并创建一个方法来检索集合中不是string.empty的最后一项.集合将始终是一个数组,返回的值是一个字符串.
我认为空值为空字符串.
我不知道如何以泛型方式执行此操作.我想知道我是否应该将它作为通用方法,因为类型将是一个字符串数组.
我会像这样调用这个函数:
string s = myArray.LastNotEmpty<???>();
Run Code Online (Sandbox Code Playgroud)
我怎么能面对这个?
static class Enumerable
{
public static TSource LastNotEmpty<TSource>(this IEnumerable<TSource> source)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我遇到了一个非常讨厌的我会说'副作用',但这显然是一个糟糕的设计问题.我正在使用Guava ForwardingList模式来装饰常规List.我的目的是建立一个大小限制列表,当满足maximumSize时,最旧的元素被踢出(简单的FIFO设计).请注意,我不会代理或克隆我现有的集合.但我有这个非常讨厌的副作用:
List<String> originalList = new ArrayList<String>();
int maximumSize = 2;
originalList.add("foo");
originalList.add("bar");
System.out.println(originalList); // [foo, bar]
ListFactory<String> factory = ListFactory.getInstance(String.class);
List<String> decoratedList = factory.newTalendList(originalList, maximumSize);
decoratedList.add("beer");
System.out.println(originalList); // [bar, beer]
originalList.add("ben");
System.out.println(originalList); // [bar, beer, ben] <-- !!!
System.out.println(decoratedList); // [bar, beer, ben] <-- !!!
Run Code Online (Sandbox Code Playgroud)
(注意:我的装饰类会覆盖add()以在添加新元素时删除列表的第一个元素.包含toString()的所有其他非重写方法都被委托给原始List
好吧,你可能会看到我是否使用原始的add()方法添加一个元素,我可以超过maximumsize ...好吧,我想这是不可避免的(毕竟设计并没有错).但这不是为decorList设计的.
我找到的唯一解决方法是:
List<String> decoratedList = factory.newTalendList(new ArrayList<String>(originalList), maximumSize);
Run Code Online (Sandbox Code Playgroud)
但它似乎并不是最好的方式(我不确定它是否适用于所有环境):我不是在装饰 originalList,而是她的匿名克隆!我想知道:也许我完全搞砸了我的设计?有没有更好的方法来构建它?
是否可以向IQueryable添加扩展方法,将其转换为另一种类型的IQueryable?
我在寻找像这样的东西;
IQueryable<foo> source;
IQueryable<bar> result = source.Convert<bar>();
Run Code Online (Sandbox Code Playgroud)
我有这个,显然它不起作用.大声笑
public static IQueryable<T1> Convert<T2>(this IQueryable<T1> source) where T1 : class
{
// Do some stuff
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
第一个问题是关于保护我的List不被改变(从外部删除/添加/清除等...)的方法
有我的方式:
class Foo
{
public int[] MyCollection
{
get{ return (_myCollection==null)?null:_myCollection.ToArray();
}
protected List<int> _myCollection;
}
Run Code Online (Sandbox Code Playgroud)
好吗?或者有更好的想法,或者可能是模式?
第二:当我用秒表测试这个解决方案时,我非常惊讶.
List -enumeration比使用强制转换时间的List.ToArray()枚举慢:
List<int> myList = new List<int>();
for (int j = 0; j < 10000; j++)
{
myList.Add(j);
}
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
//casting every iteration:
var ROC = myList.ToArray();
int count = 0;
foreach (var a in ROC)
{
count += a;
}
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
Run Code Online (Sandbox Code Playgroud)
它显示我700毫秒,和
List<int> …
Run Code Online (Sandbox Code Playgroud) 我正试图在这里打印出一个数组/集合.我有一个包含以下代码的类文件来打印文本:
//Display All
public void Display()
{
Console.WriteLine(ID + "\t" + Product + "\t" + Category + "\t" + Price + "\t" + Stock + "\t" + InBasket);
}
Run Code Online (Sandbox Code Playgroud)
然后,在main中我尝试使用以下方法将其实际打印到屏幕上:
foreach (KeyValuePair<int, Farm_Shop> temp in products)
{
//display each product to console by using Display method in Farm Shop class
temp.Display();
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
'System.Collections.Generic.KeyValuePair<int,Farm_Shop_Assignment.Farm_Shop>'
does not contain a definition for 'Display' and no extension method 'Display'
accepting a first argument of type
'System.Collections.Generic.KeyValuePair<int,Farm_Shop_Assignment.Farm_Shop>'
could be found (are you missing a …
Run Code Online (Sandbox Code Playgroud) 我有三行代码:
1) List<String> list = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
这不会产生任何错误,
但是当我编写以下代码行时
2)Map<String, List<String>> map = new HashMap<String, ArrayList<String>>();
Run Code Online (Sandbox Code Playgroud)
我得到以下错误
类型不匹配:无法转换HashMap<String,ArrayList<String>> to Map<String,List<String>>
3)Map<String,String> d= new HashMap<String,String>();
Run Code Online (Sandbox Code Playgroud)
此行不会产生任何错误
我想知道为什么
(2)行显示我错误.提前致谢.:)
我想知道做一个的替代方案toProcess.RemoveAll
,但并行.今天我的代码就像我的例子一样运作良好,但顺序,我想和平相处.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParallelTest
{
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
List<VerifySomethingFromInternet> foo = new List<VerifySomethingFromInternet>();
foo.Add(new VerifySomethingFromInternet(@"id1", true));
foo.Add(new VerifySomethingFromInternet(@"id2", false));
foo.Add(new VerifySomethingFromInternet(@"id3", true));
foo.Add(new VerifySomethingFromInternet(@"id4", false));
foo.Add(new VerifySomethingFromInternet(@"id5", true));
foo.Add(new VerifySomethingFromInternet(@"id6", false));
DoSomethingFromIntert bar = new DoSomethingFromIntert();
bar.DoesWork(foo);
Console.ReadLine();
}
}
public class DoSomethingFromIntert
{
bool RemoveIFTrueFromInternet(VerifySomethingFromInternet vsfi)
{
Console.WriteLine(String.Format("Identification : {0} - Thread : {1}", vsfi.Identification, Thread.CurrentThread.ManagedThreadId));
// Do some blocking work …
Run Code Online (Sandbox Code Playgroud) c# linq multithreading generic-collections task-parallel-library
我有两个类,它具有以下属性
Class A
{
public int CustID { get; set; }
public bool isProcessed { get; set; }
}
Class B
{
public int EmpId{ get; set; }
public bool isProcessed { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个接受所有这些类的泛型方法.'isProcessed'属性在这两个类中都很常见.
public void ProceesData<T>(IList<T> param1, string date1)
{
}
Run Code Online (Sandbox Code Playgroud)
我需要关注的事情
注意:我更喜欢使用反射的解决方案,因为属性名称是常量(即"IsProcessed")
任何人都可以帮助这个.
我正在调用一个返回 raw 的库方法Stream
。我知道流中元素的类型,并希望将其收集到具有声明元素类型的集合中。什么是好的或不那么骇人听闻的做法?
为了一个可重现的例子,假设我想调用这个方法:
@SuppressWarnings("rawtypes")
static Stream getStream() {
return Stream.of("Example");
}
Run Code Online (Sandbox Code Playgroud)
我曾希望这会起作用,但我不明白为什么它不起作用:
List<String> stringList = getStream().map(s -> (String) s).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我得到Type mismatch: cannot convert from Object to List。为什么?
未经检查的演员阵容有效。为了缩小需要标记为故意未检查的位,我们可以这样做
@SuppressWarnings("unchecked")
Stream<String> stringStream = getStream();
List<String> stringList = stringStream.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
在 Java 8 和 Java 11 上,返回的列表都是 a java.util.ArrayList
,它包含预期的String
元素。
我尝试使用我的搜索引擎搜索诸如java collect raw stream 之类的术语。它没有让我找到任何有用的东西。有一个封闭的 Java 错误(底部的链接)提到一旦您对原始类型进行操作,一切都会变得原始。但是这个完全原始的版本也不起作用:
List stringList = getStream().collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我仍然遇到Type mismatch: cannot convert from Object to List。怎么来的? …
java raw-types generic-collections type-parameter java-stream
一些上下文:我想编写一个类,其中向集合添加内容的主要方法是通过名为Add(或类似的东西)的方法(或方法).所以最好的签名是params object [].在内部,这个函数必须切换/ if-else它可以接受的所有类型.所以,最初它可以接受这个object []数组,但我可能希望看到它也接受object [] []和object [] [] []等,然后方法/函数可以在内部展平,所以在调用此函数之前,用户不需要这样做.
所以...
是否有可能编写一个函数,可以为一种对象接受各种级别的列表类型? 作为一个方面的问题,设计一个接口类,这是更好地接受时(编辑:从问题中解脱出来,因为已经有足够的事情了.)object[]
或IEnumerable<object>
或params object[]
?
举例来说,我在想,可同时接收/所有的功能object[]
,IEumerable<object>
以及可能进一步嵌套,如:IEnumerable<IEnumerable<object>>
,object[][]
(而上,上和,等等).
这可能吗?
c# ×8
collections ×4
generics ×3
java ×3
.net ×2
arrays ×1
decorator ×1
dictionary ×1
guava ×1
ienumerable ×1
iqueryable ×1
java-stream ×1
linq ×1
raw-types ×1