我正在为一个解析JSON的REST服务编写一些验证器,我发现了一些听起来很奇怪的东西(我根本不是JAVA专家).
考虑使用两个ArrayLists:
ArrayList<Object> list1 = new ArrayList<Object>();
ArrayList<Object> list2 = new ArrayList<Object>();
Run Code Online (Sandbox Code Playgroud)
两个列表都有一些共同点:它们完全是空的(或者是空元素).但如果我这样做:
list1.add(null);
Run Code Online (Sandbox Code Playgroud)
虽然两者都完全是空的,但它们的行为完全不同.并且为了使一些方法的结果非常不同:
System.out.println(list1.contains(null)); //prints true!
System.out.println(list2.contains(null)); //prints false
System.out.println(CollectionUtils.isNotEmpty(list1)); //prints true
System.out.println(CollectionUtils.isNotEmpty(list2)); //prints false
System.out.println(list1.size()); //prints 1
System.out.println(list2.size()); //prints 0
Run Code Online (Sandbox Code Playgroud)
做一些研究,并查看每种方法的实现,您可以确定这些差异的原因,但仍然不明白为什么区分这些列表是有效的或有用的.
提前致谢!!!
编辑:
我大多同意答案,但我还没有说服所有人.这是删除方法的实现:
/**
* Removes the first occurrence of the specified element from this list,
* if it …Run Code Online (Sandbox Code Playgroud) 我有两个分支:
public class A {
//attibutes and methods
}
public class B extends A {
//atributes and methods
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个服务返回一个包含A类元素的List.让我们称之为generateAElements() ;
我想调用那个方法,过滤得到的List只保留B类元素,它们也是A类.
List<A> listA = generateAElements();
List<A> listAA = listA.filter( p -> p instanceof B).collect(Collectors.toList());
List<B> listB = new ArrayList<>();
// need to create a new list, iterate overListA and add elements of type B?
for (A itemA : listA) {
listB.add((B) itemA);
}
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法来做到这一点?
重要提示:列表可能包含大量元素.
我打开一个新标签来显示PDF文件.我在bytearray,base 64上有PDF文件数据.
我能够获取数据,并显示它:
downloadFile(strData, name) {
var newdata = "data:" + "application/pdf" + ";base64," + (strData);
var newWindow = window.open(newdata, "_blank");
newWindow.onload = function() { document.title = "My title"; }
return true;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我无法将标题设置为打开的新选项卡.
我想设置一个标题,如"PDF文件"或只是文档的名称(我分别获取文件数据和文件名,并将其传递给我的downloadFile函数.
有没有办法将标题设置为此选项卡?提前致谢!
java ×2
list ×2
arraylist ×1
casting ×1
collections ×1
html ×1
java-8 ×1
java-stream ×1
javascript ×1
null ×1
pdf ×1
window.open ×1