我有这个代码:
List<String> strings = Arrays.asList("a", "b", "cc");
for (String s : strings) {
if (s.length() == 2)
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
我想用过滤器和lambda来编写它:
for (String s : strings.stream().filter(s->s.length() == 2)) {
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
我得到Can only iterate over an array or an instance of java.lang.Iterable.
我尝试:
for (String s : strings.stream().filter(s->s.length() == 2).iterator()) {
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
我得到了同样的错误.这甚至可能吗?我真的不想做stream.forEach()并传递消费者.
编辑:对我来说重要的是不要复制元素.
我使用protobuf的枚举来共享C++应用程序和Java应用程序之间的值.这样,int在语言之间共享相同()值,并且值在编译时可用.通过以某种方式在公共.proto文件中定义它,我可以用字符串做类似的事情吗?
我在C++ 11中工作,包括用C++ 03实现的h文件.在h文件中我包含了枚举的枚举Foo.我想申报一个转发code.h并将其用于code.cpp:
header.h:
enum Foo {A=1};
Run Code Online (Sandbox Code Playgroud)
code.h:
enum Foo : int; // also tried : unsigned int, long, short, unsigned short, char, unsigned char
void bar(Foo foo);
Run Code Online (Sandbox Code Playgroud)
code.cpp:
#include header.h
void bar(Foo foo) { }
Run Code Online (Sandbox Code Playgroud)
这是我编译时得到的错误(测试g ++ 4.8.5和g ++ 5.3.1):
In file included from code.cpp:2:0:
header.h:1:6: error: underlying type mismatch in enum ‘enum Foo’
enum Foo {A=1};
^
In file included from code.cpp:1:0:
code.h:3:12: error: previous definition here
enum Foo : int;
Run Code Online (Sandbox Code Playgroud)
如果我将header.h更改为:我可以修复此错误: …
假设我有这段代码:
object o1 = new Object();
object o2 = o1;
Run Code Online (Sandbox Code Playgroud)
获取o1锁定与获取o2上的锁定相同?(如果o1被锁定,将锁定o2阻塞直到o1被释放?)
我想优化我对 HBase 的使用以加快写入速度。我有一个任务从 Kafka 主题中读取,然后基于该主题写入 HBase。由于 Kafka 将记录所有要写入的内容,因此很容易从中恢复。我正在阅读“HBase High Perormance Cookbook”,并有以下说明:
请注意,这带来了关于何时使用 WAL 以及何时不使用的有趣想法。默认情况下,WAL 写入处于开启状态,并且数据始终写入 WAL。但是,如果您确定可以重写数据或少量丢失不会影响处理的整体结果,则可以禁用对 WAL 的写入。WAL 提供了简单而明确的恢复。这是默认情况下始终启用它的根本原因。在无法预料数据丢失的情况下,您应该将其保留为默认设置;否则,将其更改为使用 memstore。或者,您可以计划 DR(灾难恢复)
如何将此恢复配置为自动?我看到两个选项:
我该怎么做?
我想打印此输出
+------------------------------------------------+--+
| tab_name |
+------------------------------------------------+--+
| table1 |
| table2 |
| table3 |
| wt |
| wa |
| wal |
+------------------------------------------------+--+
Run Code Online (Sandbox Code Playgroud)
AS没有标题,没有表格格式的直线
table1
table2 table3 wt
wa
wal
我有一个shared_ptr<MyProto>我经过的地方.最终,在某些情况下,我想将原始指针传递给一个函数,然后该函数成为内存所有者.在这些情况下,shared_ptr由于我调用的函数取得了所有权,因此不再负责释放内存.如何让shared_ptr失去所有权?
我想拥有shared_ptr失去所有权的原因是我想使用协议缓冲区的AddAllocated功能,它接受已经分配的指针并承担它的所有权.
例:
shared_ptr<MyProto> myProtoSharedPtr = // by this point this is the last reference to the heap allocated MyProto
// I want to add it to a collection and serialize the collection without copying
CollectionProto collectionProto;
collectionProto.mutable_my_proto().AddAllocated(myProtoSharedPtr.get()); // at this point collectionProto took ownership of the memory
std::string serialization = collectionProto.SerializeAsString();
// bad: myProtoSharedPtr.get() will be freed twice
Run Code Online (Sandbox Code Playgroud) 我正在为Windows Phone 7编写一个增强现实应用程序作为学校项目.我想获取相机输出,然后在其上添加一层数据.有没有办法让相机输出显示在面板中?
我想对 RabbitMQ 中的队列进行此约束:
在确认前一条消息(正在处理的消息)之前,队列中的下一条消息无法出队。
通过这个我将实现事件的有序处理和跨多个队列的并行处理。我该如何配置 RabbitMQ?
编辑(澄清):将会有许多消费者都试图从所有队列中获取工作,并且由于他们无法从正在处理未确认的事件的队列中获取工作 - 维持有序处理。
在内部,数组不保留除其包含的元素之外的任何数据(甚至不是它的大小,这是一个模板参数,在编译时固定).
我理解这意味着使用array类似于使用int[]和sizeof在相同的范围内.但这段代码是有效还是依赖于未定义的行为?
class A {
array<int, 10> arr;
void setArr() {
for (int& i : arr)
i = 42;
}
void printArr() {
for (int i : arr)
cout << i << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
编译器如何知道何时停止foreach而不将数组大小存储在堆或堆栈上?我运行它,代码工作.