我需要在ArrayList
队列中添加元素,但是当我调用函数添加元素时,我希望它在数组的开头添加元素(因此它具有最低的索引)并且如果数组有10个元素添加删除最旧元素(索引最高的元素)的新结果.
有没有人有什么建议?
假设我们有一个简单的方法,它应该连接Person集合的所有名称并返回结果字符串.
public String concantAndReturnNames(final Collection<Person> persons) {
String result = "";
for (Person person : persons) {
result += person.getName();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法用1行的新流API forEach函数编写这段代码?
我正在学习Java泛型,我问自己这个问题.
这两个方法声明有什么区别?
public static void someMethod(List<? extends Number> numberList);
Run Code Online (Sandbox Code Playgroud)
和
public static <E extends Number> void someMethod(List<E> numberList);
Run Code Online (Sandbox Code Playgroud) 我正在重构我编写的一些旧代码,并且我在这段代码上做了标记:
List<OcmImageData> fullImagePool = new ArrayList<>();
if (CollectionUtils.isNotEmpty(style.getTestMH())) {
fullImagePool.addAll(style.getTestMH());
}
if (CollectionUtils.isNotEmpty(style.getTrousers())) {
fullImagePool.addAll(style.getTrousers());
}
if (CollectionUtils.isNotEmpty(style.getDetailRevers())) {
fullImagePool.addAll(style.getDetailRevers());
}
if (CollectionUtils.isNotEmpty(style.getDetailCuffs())) {
fullImagePool.addAll(style.getDetailCuffs());
}
if (CollectionUtils.isNotEmpty(style.getDetailInner())) {
fullImagePool.addAll(style.getDetailInner());
}
if (CollectionUtils.isNotEmpty(style.getDetailMaterial())) {
fullImagePool.addAll(style.getDetailMaterial());
}
if (CollectionUtils.isNotEmpty(style.getComposing())) {
fullImagePool.addAll(style.getComposing());
}
...
Run Code Online (Sandbox Code Playgroud)
所以基本上我需要创建一个ArrayList,其中包含这里引用的所有列表,因为它们可以为null(它们是从一个封闭的源代码框架中提取出来的数据库,不幸的是如果他没有找到任何东西它就是null),我需要每次检查集合是否为null时将它们添加到此池中,这看起来很奇怪.
是否有库或Collection-Framework实用程序类,使我能够在不执行空安全检查的情况下将集合添加到另一个集合中?
我见过类似的问题,但他们没有多大帮助.
例如,我有这个通用类:
public class ContainerTest<T>
{
public void doSomething()
{
//I want here to determinate the Class of the type argument (In this case String)
}
}
Run Code Online (Sandbox Code Playgroud)
和另一个使用此容器类的类
public class TestCase
{
private ContainerTest<String> containerTest;
public void someMethod()
{
containerTest.doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在方法doSomething()中确定类型参数的类而没有在ContainerTest类中使用显式类型变量/字段或任何构造函数?
更新:更改ContainerTest类的格式
什么是提取具有多种用途的谓词的更简洁方法.方法或类字段?
这两个例子:
1.Class Field
void someMethod() {
IntStream.range(1, 100)
.filter(isOverFifty)
.forEach(System.out::println);
}
private IntPredicate isOverFifty = number -> number > 50;
Run Code Online (Sandbox Code Playgroud)
2.Method
void someMethod() {
IntStream.range(1, 100)
.filter(isOverFifty())
.forEach(System.out::println);
}
private IntPredicate isOverFifty() {
return number -> number > 50;
}
Run Code Online (Sandbox Code Playgroud)
对我来说,现场方式看起来更好一点,但这是正确的方法吗?我有疑虑.
我想创建一个触发onkeyup事件的javascript函数,它的任务是调用一个主解析器函数,但只有在没有键被触发至少x毫秒时,其中x是函数参数.
例如:
我们有HTML代码
<body>
<input type="text" id="suggestion" onkeyup="callMe(200);"/>
</body>
Run Code Online (Sandbox Code Playgroud)
和javascript类似:
<script type="text/javascript">
function callMe(ms)
{
//wait at least x ms then call main logic function
// e.g. doMain();
alert("I've been called after timeout"); //for testing purposes
}
</script>
Run Code Online (Sandbox Code Playgroud)
因此,在我输入警报时,除非您至少输入x ms,否则不会调用警报.
目前我正在开发一个具有内存密集检查过程的程序.在某些时候,代码看起来像这样.
if(isvalid() && false) //this false is acctually another method which will at this given
//point always return false
{
//rest ommited
}
Run Code Online (Sandbox Code Playgroud)
JVM是否总是检查第一个方法(isValid()),因为x && false是假的;
我不确定,因为调试调试器时每次迭代都跳转到isValid()方法.
我的完整变量名称是唯一代码,但是我希望这个变量是"ucode"-ish.
那么什么是正确的Java命名约定?
假设我有这个:
<body>
<input type="text" onblur="myFunction()" />
<... rest of page ...>
</body>
<script type="text/javascript">
function myFunction()
{
//this function alerts over what element your mouse is when the input loses focus
}
</script>
Run Code Online (Sandbox Code Playgroud)
有人知道如何实现这个吗?
java ×7
arraylist ×2
generics ×2
java-8 ×2
java-stream ×2
javascript ×2
arrays ×1
collections ×1
events ×1
jquery ×1
jvm ×1
naming ×1
performance ×1
predicates ×1
stack ×1
timer ×1
type-erasure ×1
types ×1
variables ×1