在Java 8中,您可以使用方法引用来过滤流,例如:
Stream<String> s = ...;
long emptyStrings = s.filter(String::isEmpty).count();
Run Code Online (Sandbox Code Playgroud)
有没有办法创建一个方法引用,它是对现有方法的否定,即:
long nonEmptyStrings = s.filter(not(String::isEmpty)).count();
Run Code Online (Sandbox Code Playgroud)
我可以创建not如下所示的方法,但我想知道JDK是否提供了类似的东西.
static <T> Predicate<T> not(Predicate<T> p) { return o -> !p.test(o); }
Run Code Online (Sandbox Code Playgroud) 我目前正在使用jconsole监视Java应用程序.内存选项卡允许您选择:
Heap Memory Usage
Non-Heap Memory Usage
Memory Pool “Eden Space”
Memory Pool “Survivor Space”
Memory Pool “Tenured Gen”
Memory Pool “Code Cache”
Memory Pool “Perm Gen”
Run Code Online (Sandbox Code Playgroud)
他们之间有什么区别?
我怎样才能优雅地序列化lambda?
例如,下面的代码抛出一个NotSerializableException.如何在不创建SerializableRunnable"虚拟"界面的情况下修复它?
public static void main(String[] args) throws Exception {
File file = Files.createTempFile("lambda", "ser").toFile();
try (ObjectOutput oo = new ObjectOutputStream(new FileOutputStream(file))) {
Runnable r = () -> System.out.println("Can I be serialized?");
oo.writeObject(r);
}
try (ObjectInput oi = new ObjectInputStream(new FileInputStream(file))) {
Runnable r = (Runnable) oi.readObject();
r.run();
}
}
Run Code Online (Sandbox Code Playgroud) 在Java 8中,如何将Timestamp(in java.sql)转换为LocalDate(in java.time)?
我有一个Map<String,String>具有大量键值对.现在我想从中删除选定的键Map.以下代码显示了我为实现这一点所做的工作.
Set keySet = new HashSet(); //I added keys to keySet which I want to remove.
Run Code Online (Sandbox Code Playgroud)
然后 :
Iterator entriesIterator = keySet.iterator();
while (entriesIterator.hasNext()) {
map.remove( entriesIterator.next().toString());
}
Run Code Online (Sandbox Code Playgroud)
这很有效.我只是想知道,实现我的要求会有什么更好的方法?
在ArrayBlockingQueue,所有需要锁的方法final在调用之前将其复制到局部变量lock().
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final ReentrantLock lock = this.lock;
lock.lock();
try {
if (count == items.length)
return false;
else {
insert(e);
return true;
}
} finally {
lock.unlock();
}
}
Run Code Online (Sandbox Code Playgroud)
当字段是什么时,有没有理由复制this.lock到局部变量?lockthis.lockfinal
此外,它还在使用E[]之前使用本地副本:
private E extract() {
final E[] items = this.items;
E x = items[takeIndex];
items[takeIndex] = null;
takeIndex = inc(takeIndex);
--count;
notFull.signal();
return x;
}
Run Code Online (Sandbox Code Playgroud)
有没有理由将最终字段复制到本地最终变量?
尝试转换为时java.util.Date,我收到以下异常java.time.LocalDate.
java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2014-08-19T05:28:16.768Z of type java.time.Instant
Run Code Online (Sandbox Code Playgroud)
代码如下:
public static Date getNearestQuarterStartDate(Date calculateFromDate){
int[] quaterStartMonths={1,4,7,10};
Date startDate=null;
ZonedDateTime d=ZonedDateTime.from(calculateFromDate.toInstant());
int frmDateMonth=d.getMonth().getValue();
Run Code Online (Sandbox Code Playgroud)
我使用ZonedDateTime课程的方式有问题吗?
根据文档,这应该将java.util.Date对象转换为ZonedDateTime.上面的日期格式是标准日期?
我是否必须回到Joda时间?
如果有人可以提供一些建议,那就太好了.
我无法在listview模板上修复此问题:我的帖子标题中有错误,但我不会将imageview转换为textview.这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_weight="1"
android:paddingBottom="10dip" >
<TextView
android:id="@+id/mq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Metri quadri"
android:textColor="#33b5e5"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/citta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/mq"
android:layout_alignBottom="@+id/mq"
android:layout_centerHorizontal="true"
android:text="Citta"
android:textColor="#33b5e5"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/prezzo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Prezzo"
android:textColor="#e1e1e1"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="5dip"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/testolungo"
android:textColor="#e1e1e1"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/foto"
android:layout_width="80dip"
android:layout_height="60dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="Release"
android:padding="5dip"
android:scaleType="centerCrop"
android:src="@drawable/stub" />
<TextView
android:id="@+id/descsplash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Small Text"
android:textSize="16sp" …Run Code Online (Sandbox Code Playgroud) 如何检查列表是否为空?如果是这样,系统必须给出一条消息,说List是空的.如果没有,系统必须给出一条消息,说明列表不为空.用户可以输入数字,-1以停止该程序.这是我现在的代码,但这不起作用,它总是说'List is not empty'.
import java.util.*;
import javax.swing.JOptionPane;
public class ArrayListEmpty
{
public static void main(String[] args)
{
List<Integer> numbers = new ArrayList<Integer>();
int number;
do {
number = Integer.parseInt(JOptionPane.showInputDialog("Enter a number (-1 to stop)"));
numbers.add(number);
} while (number != -1);
giveList(numbers);
}
public static void giveList(List<Integer> numbers)
{
if (numbers != null)
JOptionPane.showMessageDialog(null, "List isn't empty");
else
JOptionPane.showMessageDialog(null, "List is empty!");
}
}
Run Code Online (Sandbox Code Playgroud)