如果我有一个Dictionary<String,...>
是否有可能使方法ContainsKey
不区分大小写?
这似乎是相关的,但我没有理解它:c#Dictionary:通过声明使Key不区分大小写
有谁知道为什么下面的代码不能编译?add()和addAll()都不能按预期工作.删除"?extends"部分会使一切正常,但之后我将无法添加Foo的子类.
List<? extends Foo> list1 = new ArrayList<Foo>();
List<? extends Foo> list2 = new ArrayList<Foo>();
/* Won't compile */
list2.add( new Foo() ); //error 1
list1.addAll(list2); //error 2
Run Code Online (Sandbox Code Playgroud)
错误1:
IntelliJ说:
add(capture<? extends Foo>) in List cannot be applied to add(Foo)
Run Code Online (Sandbox Code Playgroud)
编译器说:
cannot find symbol
symbol : method addAll(java.util.List<capture#692 of ? extends Foo>)
location: interface java.util.List<capture#128 of ? extends Foo>
Run Code Online (Sandbox Code Playgroud)
错误2:
IntelliJ给了我
addAll(java.util.Collection<? extends capture<? extends Foo>>) in List cannot be applied to addAll(java.util.List<capture<? extends Foo>>)
Run Code Online (Sandbox Code Playgroud)
而编译器只是说
cannot find symbol
symbol …
Run Code Online (Sandbox Code Playgroud) 在C#中获取类型化的,只读的空列表的标准方法是什么,还是有一个?
ETA:对于那些问"为什么?"的人:我有一个虚拟方法返回IList
(或者更确切地说,回答后IEnumerable
),默认实现为空.无论列表返回什么都应该是readonly因为写入它将是一个bug,如果有人试图,我想立即停止并着火,而不是等待bug稍后以某种微妙的方式出现.
我想一个添加KeyValuePair<T,U>
到Dictionary<T, U>
我没能做到.我必须分别传递密钥和值,这必然意味着Add方法必须创建一个新的KeyValuePair对象来插入,这不是非常有效.我不敢相信Add(KeyValuePair<T, U>)
Add方法没有重载.任何人都可以提出这种明显疏忽的可能原因吗?
阅读在C#中创建只读原始向量的问题(基本上,你不能这样做),
public readonly int[] Vector = new int[]{ 1, 2, 3, 4, 5 }; // You can still changes values
Run Code Online (Sandbox Code Playgroud)
我了解了ReadOnlyListBase.这是对象容器的基类,可以访问但不修改其位置.甚至在Microsoft msdn中也有一个例子.
http://msdn.microsoft.com/en-us/library/system.collections.readonlycollectionbase.aspx
我稍微修改了msdn中的示例以使用任何类型:
public class ReadOnlyList<T> : ReadOnlyCollectionBase {
public ReadOnlyList(IList sourceList) {
InnerList.AddRange( sourceList );
}
public T this[int index] {
get {
return( (T) InnerList[ index ] );
}
}
public int IndexOf(T value) {
return( InnerList.IndexOf( value ) );
}
public bool Contains(T value) {
return( InnerList.Contains( value ) ); …
Run Code Online (Sandbox Code Playgroud) 我有很多控制器使用相同的方法,差异只是实体类.
我想创建通用的BaseController并且QuerydslPredicate注释有问题(不能设置root,这是通用的)
class abstract BaseController<T extends BaseEntity> {
@Autowired
private Repository<T, Long> repository;
@RequestMapping(method = RequestMethod.GET)
public Page<T> findAll(@QuerydslPredicate Predicate predicate, Pageable pageable) {
return repository.findAll(predicate, pageable);
}
}
Run Code Online (Sandbox Code Playgroud)
Method extractTypeInfo from QuerydslPredicateArgumentResolver return T
.但是需要Entity类.
我无法将根值设置为QuerydslPredicate(没有类)
@QuerydslPredicate(root = T.class)
Run Code Online (Sandbox Code Playgroud)
有关如何实现这一目标的任何帮助?
在Java中,集合仅在插入时仅检查对象与已在集合中的对象的相等性.这意味着如果在对象已经存在于集合中之后,它变得等于集合中的另一个对象,则该集合将保持两个相等的对象而不会抱怨.
编辑:例如,考虑一个简单的对象,并假设hashCode和equals是按照最佳实践/定义的
class Foo {
int foo;
Foo(int a){ foo = a; }
//+ equals and hashcode based on "foo"
}
Foo foo1 = new Foo(1);
Foo foo2 = new Foo(2);
Set<Foo> set = new HashSet<Foo>();
set.add(foo1);
set.add(foo2);
//Here the set has two unequal elements.
foo2.foo = 1;
//At this point, foo2 is equal to foo1, but is still in the set
//together with foo1.
Run Code Online (Sandbox Code Playgroud)
如何为可变对象设计一个集合类?我期望的行为如下:如果集合中的一个对象在任何时候变得等于集合中的另一个对象,则该集合中的该对象将被删除.有没有?是否有一种编程语言可以使这更容易实现?
我有两个几乎相同的方法,但我试图避免代码重复.
它们中的每一个都将一个唯一的对象作为参数,并从中找出最高的值.
这是一个例子:
public Integer getHighestIndexValue(List<ObjectA> list) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (ObjectA a: list) {
indexes.add(Integer.parseInt(a.getA()));
}
highestValue = Collections.max(indexes);
return highestValue;
}
Run Code Online (Sandbox Code Playgroud)
要么:
public Integer getHighestIndexValue(List<ObjectB> list) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (ObjectB b: list) {
indexes.add(Integer.parseInt(b.getB()));
}
highestValue = Collections.max(indexes);
return highestValue;
}
Run Code Online (Sandbox Code Playgroud)
如何使用通用参数组合这两个?
我尝试创建一个BaseClass
包含这两个类并在方法签名中扩展它的类.还是需要铸造.
public <T extends BaseClass> Integer getHighestIndexValue(List<T> objectList) {
int highestValue;
List<Integer> indexes = new ArrayList<Integer>();
for (T objects: objectList) {
indexes.add(Integer.parseInt(objects.getAorB())); ------ …
Run Code Online (Sandbox Code Playgroud) 在我的UT代码中,在下面提取,我看到警告:
Unchecked generic array creation for varargs parameter of
type Matcher <? extends String> []
我已经在另一个stackoverflow中读到了关于使用varargs方法的泛型参数的问题.
但有没有一个简洁的方法来稍微重组这个测试,摆脱丑陋的警告,避免@SuppressWarnings
?
package stackoverflow;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.matchers.JUnitMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
public class FooTest {
@SuppressWarnings({"unchecked"})
@Test
public void sampleTest() {
Assert.assertThat("foo bar",
CoreMatchers.allOf(
containsString("foo"),
containsString("bar"),
not(containsString("baz"))));
}
}
Run Code Online (Sandbox Code Playgroud) 为什么这个程序会报告内存泄漏?
{$APPTYPE CONSOLE}
uses
System.Generics.Collections;
type
TDerivedGenericObjectList = class(TObjectList<TObject>)
public
constructor Create;
end;
constructor TDerivedGenericObjectList.Create;
begin
inherited;
end;
var
List: TDerivedGenericObjectList;
begin
ReportMemoryLeaksOnShutdown := True;
List := TDerivedGenericObjectList.Create;
List.Add(TObject.Create);
List.Free;
end.
Run Code Online (Sandbox Code Playgroud) java ×5
c# ×4
collections ×4
generics ×3
delphi ×1
dictionary ×1
generic-list ×1
hamcrest ×1
inheritance ×1
key-value ×1
mutable ×1
querydsl ×1
readonly ×1
set ×1
spring-data ×1