在实践中,是能够更好地返回像一个空列表此:
return Collections.emptyList();
Run Code Online (Sandbox Code Playgroud)
或者像这样:
return new ArrayList<Foo>();
Run Code Online (Sandbox Code Playgroud)
或者这完全取决于你要对返回的列表做什么?
在标准Java库中,查找两个列表是否包含完全相同的元素的最简单方法是什么?
如果两个列表是相同的实例,则无关紧要,如果列表的类型参数不同则无关紧要.
例如
List list1
List<String> list2;
// ... construct etc
list1.add("A");
list2.add("A");
// the function, given these two lists, should return true
Run Code Online (Sandbox Code Playgroud)
我知道可能有些东西盯着我:-)
编辑:为了澄清,我正在按顺序寻找完全相同的元素和元素数量.
编辑:谢谢你指出我看不见的明显答案:-)
虽然到目前为止给出的所有答案都是正确的,但有些答案比其他答案更正确,所以在接受之前我会等待一段时间以获得最好的四舍五入的答案.
例如,假设您有两个类:
public class TestA {}
public class TestB extends TestA{}
Run Code Online (Sandbox Code Playgroud)
我有一个返回a的方法,List<TestA>
我想将该列表中的所有对象强制转换为TestB
最终得到一个List<TestB>
.
假设我在列表中有3个字符串(例如"1","2","3").
然后我想重新排序它们将"2"放在位置1(例如"2","1","3").
我正在使用此代码(将indexToMoveTo设置为1):
listInstance.Remove(itemToMove);
listInstance.Insert(indexToMoveTo, itemToMove);
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但我偶尔会得到奇怪的结果; 有时订单不正确或列表中的项目被删除!
有任何想法吗?并List<T>
保证订单?
我有一个名为Fruit的课程.我正在创建此类的列表并在列表中添加每个水果.我想根据水果名称的顺序对此列表进行排序.
public class Fruit{
private String fruitName;
private String fruitDesc;
private int quantity;
public String getFruitName() {
return fruitName;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
public String getFruitDesc() {
return fruitDesc;
}
public void setFruitDesc(String fruitDesc) {
this.fruitDesc = fruitDesc;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用for循环创建它的列表
List<Fruit> fruits= new ArrayList<Fruit>();
Fruit fruit;
for(int i=0;i<100;i++)
{
fruit = new fruit();
fruit.setname(...);
fruits.add(fruit);
}
Run Code Online (Sandbox Code Playgroud)
我需要使用列表中每个对象的水果名称对此arrayList进行排序 …
我发现自己现在经常在我的代码中使用当前模式
var dictionary = new Dictionary<type, IList<othertype>>();
// Add stuff to dictionary
var somethingElse = dictionary.ContainsKey(key) ? dictionary[key] : new List<othertype>();
// Do work with the somethingelse variable
Run Code Online (Sandbox Code Playgroud)
或者有时候
var dictionary = new Dictionary<type, IList<othertype>>();
// Add stuff to dictionary
IList<othertype> somethingElse;
if(!dictionary.TryGetValue(key, out somethingElse) {
somethingElse = new List<othertype>();
}
Run Code Online (Sandbox Code Playgroud)
这两种方式都让人觉得很迂回.我真正想要的是这样的
dictionary.GetValueOrDefault(key)
Run Code Online (Sandbox Code Playgroud)
现在,我可以为字典类编写一个扩展方法来为我做这个,但我想我可能会遗漏已经存在的东西.那么,有没有办法以更简单的方式做到这一点,而无需在字典中编写扩展方法?
我一直在寻找Java中的双向映射实现,并偶然发现了这两个库:
两者都是免费的,具有我正在寻找的双向地图实现(Apache中的BidiMap,Google中的BiMap),几乎相同的大小(Apache 493 kB,Google 499 kB)[编辑:不再是真的!]并且似乎在各方面都和我很相似.
我应该选择哪一个,为什么?是否有其他等效替代方案(必须是免费的并至少具有双向映射)?我正在使用最新的Java SE,所以不需要人为地限制Java 5或类似的东西.
我想在列表上有一个反向列表视图(以类似的方式List#sublist
提供列表上的子列表视图).是否有一些提供此功能的功能?
我不想制作任何类型的列表副本,也不想修改列表.
如果我在这种情况下至少可以在列表上获得反向迭代器就足够了.
另外,我知道如何自己实现这一点.我只是问Java是否已经提供了这样的东西.
演示实施:
static <T> Iterable<T> iterableReverseList(final List<T> l) {
return new Iterable<T>() {
public Iterator<T> iterator() {
return new Iterator<T>() {
ListIterator<T> listIter = l.listIterator(l.size());
public boolean hasNext() { return listIter.hasPrevious(); }
public T next() { return listIter.previous(); }
public void remove() { listIter.remove(); }
};
}
};
}
Run Code Online (Sandbox Code Playgroud)
我刚刚发现某些List
实现具有descendingIterator()
我需要的功能.虽然没有一般的这样的实现List
.这有点奇怪,因为我看到的实现已经LinkedList
足够通用了List
.
我目前正在这样做:
Set<String> setOfTopicAuthors = ....
List<String> list = Arrays.asList(
setOfTopicAuthors.toArray( new String[0] ) );
Run Code Online (Sandbox Code Playgroud)
你能打败这个吗?
AFAIK有两种方法:
例如,
List<Foo> fooListCopy = new ArrayList<Foo>(fooList);
for(Foo foo : fooListCopy){
// modify actual fooList
}
Run Code Online (Sandbox Code Playgroud)
和
Iterator<Foo> itr = fooList.iterator();
while(itr.hasNext()){
// modify actual fooList using itr.remove()
}
Run Code Online (Sandbox Code Playgroud)
是否有任何理由偏好一种方法而不是另一种方法(例如,由于可读性的简单原因,更喜欢第一种方法)?