我正在尝试获取一个数字列表,过滤掉偶数,然后对这些偶数进行平方,以便原始列表中的偶数是平方的.这是我的代码:
ArrayList<Long> nums2 = new ArrayList<Long>();
for(long i = 0; i < 100000; i++)
nums2.add(i);
nums2.stream().filter(p -> p%2 == 0).forEach(p -> p = (long)Math.pow(p, 2));
Run Code Online (Sandbox Code Playgroud)
尝试了其他一些事情,但这是我到目前为止的地方
我是Java 8 Streams的新手.请指教,如何将流转换Stream<HashMap<String, Object>>
为HashMap数组HashMap<String, Object>[]
?
例如,我在代码中有一些流:
Stream<String> previewImagesURLsList = fileNames.stream();
Stream<HashMap<String, Object>> imagesStream = previewImagesURLsList
.map(new Function<String, HashMap<String, Object>>() {
@Override
public HashMap<String, Object> apply(String person) {
HashMap<String, Object> m = new HashMap<>();
m.put("dfsd", person);
return m;
}
});
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做
HashMap<String, Object>[] arr = imagesStream.toArray();
Run Code Online (Sandbox Code Playgroud)
?
抱歉,我的英语不好.
我有一个类似的课程
public class Person {
public Person(String firstName) {...}
public String getFirstname() {...}
// ... some other fields
}
Run Code Online (Sandbox Code Playgroud)
以及这些类的对象列表:
List<Person > objList = new ArrayList<>();
objList.add(new Person("Peter"));
objList.add(new Person("James"));
objList.add(new Person("Bart"));
Run Code Online (Sandbox Code Playgroud)
现在我需要一个以逗号分隔的列表,列出这个对象列表的第一个名字,比如"Peter,James,Bart"
.
我怎么能用lambdas和Java 8流做到这一点?TIA!
我试着编写一个可以进行多线程读写的程序.它可以在一次读取和一次写入时正常工作,但在使用两次读取和一次写入时,它以死锁结束.
谁能帮忙检查一下?
const int BUF_SIZE = 10;
const int ITERS = 100;
boost::mutex io_mutex;
class buffer
{
public:
typedef boost::mutex::scoped_lock scoped_lock;
buffer() : p(0), c(0), full(0)
{}
void put(int m)
{
scoped_lock lock(mutex);
if (full == BUF_SIZE)
{
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << "Buffer is full. Waiting..." << std::endl;
}
while (full == BUF_SIZE)
cond.wait(lock);
}
buf[p] = m;
p = (p+1) % BUF_SIZE;
++full;
cond.notify_all();
}
int get()
{
scoped_lock lk(mutex);
if (full == 0)
{
{
boost::mutex::scoped_lock lock(io_mutex); …
Run Code Online (Sandbox Code Playgroud)