如何在Collection继承类中找到元素的索引?
public class MyCollection : Collection<MyClass>
{
// implementation here
}
Run Code Online (Sandbox Code Playgroud)
我试图在集合上使用.FindIndex但没有成功:
int index = collectionInstance.FindIndex(someLambdaExpression);
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以实现吗?
我正在使用Guava集合的转换函数,并发现自己制作了许多像这样的伪代码的匿名函数:
Function<T, R> TransformFunction = new Function<T, R>() {
public R apply(T obj) {
// do what you need to get R out of T
return R;
}
};
Run Code Online (Sandbox Code Playgroud)
...但是由于我需要重用其中的一些,我想把频繁的那些放到一个类中以便于访问.
我很尴尬地说(因为我不太使用Java),我无法弄清楚如何使类方法返回这样的函数.你能?
我正在尝试编写一个工厂方法,该方法将创建抽象泛型集合类的派生实例.这是基类......
abstract class ItemBase { }
abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { }
Run Code Online (Sandbox Code Playgroud)
......及其派生类......
class Item : ItemBase { }
class ItemCollection : CollectionBase<Item> {}
Run Code Online (Sandbox Code Playgroud)
现在,我想要一个将创建ItemCollection的工厂方法.但请注意,派生类Item和ItemCollection对于包含此工厂方法的类是未知的.这就是我想象的应该......
static T CreateItemCollection<T>() where T : CollectionBase<ItemBase>, new()
{
return new T();
}
Run Code Online (Sandbox Code Playgroud)
......我想像这样调用它......
var collection = CreateItemCollection<ItemCollection>();
Run Code Online (Sandbox Code Playgroud)
但是工厂方法不会编译,因为ItemBase必须具有无参数构造函数.并且invokation调用拒绝相信ItemCollection
来自CollectionBase<ItemBase>
.
有人可以指点我正确的方向吗?谢谢.
我想要的基本上是一个集合,它是字典和列表的混合体.我想要一个集合,我可以添加键/值对(像一个字典),但同时能够以相同的顺序检索值(没有键)我添加它们(如列表)?这样的集合是否存在于.NET中?
谢谢
我正在尝试创建一个处理我的两个模型之一的通用函数.请注意,这两个模型都具有相同的确切属性...
例如,在下面的代码中,即使"NewProduct"和"OldProduct"都具有此属性,智能感知也不知道在T中存在名为Price的属性.如何向VS指定我希望能够传入的两种类型?IList<NewProduct>, IList<OldProduct>
public static IList<T> GenericFunction<T>(IList<T> objList)
{
IList<T> filteredData = objList.Where(p => p.Price > 0));
}
Run Code Online (Sandbox Code Playgroud) 我有一个java方法,我试图使通用,以便它可以采取2种不同类型的对象的列表作为参数.(如下所示的琐碎例子).这两个不同的对象都将始终具有方法getDate()和getHour().代码如下所示:
public <T> List<T> getListOfStuff(List<T> statistics) {
List<T> resultList = new ArrayList<T>(statistics.size());
if(statistics.size() > 0){
resultList.add(statistics.get(0));
int date = Integer.parseInt(resultList.get(0).getDate());
int hour = Integer.parseInt(resultList.get(0).getHour());
}
return resultList;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.这两行不起作用:
int date = Integer.parseInt(resultList.get(0).getDate());
int hour = Integer.parseInt(resultList.get(0).getHour());
Run Code Online (Sandbox Code Playgroud)
错误说:"方法getDate()未定义类型T"和"方法getHour()未定义类型T"
它向我提出了向方法接收器添加强制转换的建议,但它不会让我使用T而是强制对象名称对我这样对我不起作用:
int date = Integer.parseInt((ObjectName1)resultList.get(0).getDate());
int hour = Integer.parseInt((ObjectName1)resultList.get(0).getHour());
Run Code Online (Sandbox Code Playgroud)
有什么方法可以做我想要的吗?
我想创建一些通用方法,如下面的代码:
public async Task<T> Get<T>(string url) where T : IBaseModel, IList<IBaseModel>
Run Code Online (Sandbox Code Playgroud)
显然我想支持可枚举的集合以及从IBaseModel
界面驱动的单个对象.方法定义没问题,但是当涉及到它的用法时,我会得到以下错误:
await myClass.Get<List<DrivedClassFromBaseModel>>("some url");
There is no implicit conversion from "System.Collection.Generic.List<DrivedClassFromBaseModel> to System.Collection.Generic.IList<IBaseModel>"
Run Code Online (Sandbox Code Playgroud) private PostDto MapIntegration(IntDto integ)
{
return new PostDto
{
prop1 = "7",
prop2 = "10",
prop3 = true,
prop4 = "EA Test",
product_list.Add(integ) // ERROR This says the name product_list does not exist in current context
};
}
Run Code Online (Sandbox Code Playgroud)
And when we look at PostDto
public class PostDto
{
public string prop1 { get; set; }
public string prop2 { get; set; }
public bool prop3 { get; set; }
public string prop4 { get; set; }
public List<IntDto> product_list { …
Run Code Online (Sandbox Code Playgroud) 我有一个包含
A,15
B,67
C,45
D,10的文件
我正在从文件中读取数据,但我想将数据读入字典或散列表,但数据可以通过其值
B,67
C,45
A,15
D.10进行分类.
如果任何其他列表将以有效的方式工作,请建议
谢谢
我有两个类非常相似的类
A类:
String getString(Set<Map.Entry<String, List<String>>> headers) {
return headers.stream().map(h -> String.join(": ", h.getKey(), h.getValue().stream().
collect(Collectors.joining(", ")))).collect(Collectors.joining(System.lineSeparator()));
}
Run Code Online (Sandbox Code Playgroud)
B级
String getString(Set<Map.Entry<String, Collection<String>>> headers) {
return headers.stream().map(h -> String.join(": ", h.getKey(), h.getValue().stream().
collect(Collectors.joining(", ")))).collect(Collectors.joining(System.lineSeparator()));
}
Run Code Online (Sandbox Code Playgroud)
方法参数泛型类型的唯一区别:
Set<Map.Entry<String, List<String>>> headers
Set<Map.Entry<String, Collection<String>>> headers
Run Code Online (Sandbox Code Playgroud)
我不会编码重复.并寻找方式haw我可以在一个重构这两个方法.
我正在尝试使用不同的通用通配符组合编写代码(?super或?extends).但失败了.例如:
Set<Map.Entry<String, ? extends Collection<String>>>
Run Code Online (Sandbox Code Playgroud)
你能不能支持我如何重构这个泛型的想法.谢谢
c# ×6
generics ×4
java ×3
.net ×2
collections ×2
.net-core ×1
constraints ×1
dictionary ×1
factory ×1
guava ×1
inheritance ×1
java-8 ×1
list ×1
methods ×1
object ×1
oop ×1