我感觉不到Java 8 Streams map()和mapToObj()方法之间的区别.在两者中我们都可以创建对象并将对象返回到流,所以为什么这些方法存在为两个,而不仅仅是一个.
你能用例子给我解释一下吗?
在实例化ArrayLists时,我习惯于看到这样的代码
ArrayList<Type> arr = new ArrayList<Type>();
Run Code Online (Sandbox Code Playgroud)
要么
ArrayList<Type> arr = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
但是今天我遇到了一个如下所示的ArrayList实例:
ArrayList<Type> arr = new <Type>ArrayList();
Run Code Online (Sandbox Code Playgroud)
发生了什么,为什么会给出"不安全的操作"编译警告?
我最近注意到,当涉及转义字符"\"(斜杠)时,String.replaceAll(正则表达式,替换)表现得非常奇怪.
例如,考虑有一个与文件路径字符串- String text = "E:\\dummypath"
我们要替换的"\\"用"/".
text.replace("\\","/")给出输出"E:/dummypath",然后text.replaceAll("\\","/")引发异常java.util.regex.PatternSyntaxException.
如果我们想要实现相同的功能,replaceAll()我们需要将其编写为,
text.replaceAll("\\\\","/")
一个值得注意的区别是replaceAll()它的参数是reg-ex而replace()有参数字符序列!
但text.replaceAll("\n","/")其作用与其char序列完全相同text.replace("\n","/")
深入挖掘: 当我们尝试其他一些输入时,可以观察到更奇怪的行为.
让我们分配 text="Hello\nWorld\n"
现在
text.replaceAll("\n","/"),text.replaceAll("\\n","/"),text.replaceAll("\\\n","/")这三个提供同样的输出Hello/World/
Java以我认为最好的方式搞砸了reg-ex!没有其他语言似乎在reg-ex中具有这些有趣的行为.任何特定的原因,为什么Java搞砸了这样?
在这个问题中,用户@Holger提供了一个答案,显示了匿名类的不常见用法,我不知道.
该答案使用流,但这个问题不是关于流,因为这个匿名类型构造可以在其他上下文中使用,即:
String s = "Digging into Java's intricacies";
Optional.of(new Object() { String field = s; })
.map(anonymous -> anonymous.field) // anonymous implied type
.ifPresent(System.out::println);
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,这会编译并打印预期的输出.
注意:我很清楚,自古以来,可以构造一个匿名内部类并使用其成员,如下所示:
int result = new Object() { int incr(int i) {return i + 1; } }.incr(3);
System.out.println(result); // 4
Run Code Online (Sandbox Code Playgroud)
但是,这不是我在这里问的问题.我的情况不同,因为匿名类型是通过Optional方法链传播的.
现在,我可以想象这个功能的一个非常有用的用法......很多时候,我需要map在Stream管道上发出一些操作,同时保留原始元素,即假设我有一个人员列表:
public class Person {
Long id;
String name, lastName;
// getters, setters, hashCode, equals...
}
List<Person> people = ...;
Run Code Online (Sandbox Code Playgroud)
而且我需要Person在某些存储库中存储我的实例的JSON表示,为此我需要每个Person …
我有一个我已经使用了一段时间的自定义界面,看起来像这样:
public interface Function<T, R> {
R call(T input);
}
Run Code Online (Sandbox Code Playgroud)
我想用Java Function和Guava 来改进这个接口Function,同时保持它FunctionalInterface.我以为我有完美的安排:
@FunctionalInterface
public interface Function<T, R> extends
java.util.function.Function<T, R>,
com.google.common.base.Function<T, R> {
R call(T input);
@Override
default R apply(T input) {
return call(input);
}
}
Run Code Online (Sandbox Code Playgroud)
两个超级apply()接口都声明了相同的方法,该方法已在我的界面中实现,只留下抽象call()方法.奇怪的是,它不会编译,告诉我
"@FunctionalInterface"注释无效; 函数<T,R>不是功能接口
更奇怪的是,以下变化编译得很好:
@FunctionalInterface
public interface Function<T, R> extends
java.util.function.Function<T, R> {
R call(T input);
@Override
default R apply(T input) {
return call(input);
}
}
Run Code Online (Sandbox Code Playgroud)
@FunctionalInterface
public interface Function<T, R> extends
com.google.common.base.Function<T, R> …Run Code Online (Sandbox Code Playgroud) 尽管重用字符串常量和字面值,以下代码段会打印4个不同的哈希码.为什么字符串值没有插入注释元素?
public class Foo {
@Retention(RetentionPolicy.RUNTIME)
@interface Bar {
String CONSTANT = "foo";
String value() default CONSTANT;
}
public static void main(String[] args) throws Exception {
System.out.println(System.identityHashCode(Bar.CONSTANT));
System.out.println(System.identityHashCode(Foo.class.getMethod("test1").getAnnotation(Bar.class).value()));
System.out.println(System.identityHashCode(Foo.class.getMethod("test2").getAnnotation(Bar.class).value()));
System.out.println(System.identityHashCode(Foo.class.getMethod("test3").getAnnotation(Bar.class).value()));
}
@Bar
public void test1() {}
@Bar("foo")
public void test2() {}
@Bar(Bar.CONSTANT)
public void test3() {}
}
Run Code Online (Sandbox Code Playgroud) 我想知道以下是否有一个有效的用例:
class Base {}
class A implements Comparable<Base> {
//...
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个常见的模式(见集合了大量的实例)接受类型的集合T,其中T extends Comparable<? super T>.
但是在技术上似乎不可能compareTo()在与基类进行比较时履行合同,因为没有办法确保另一个类不会通过相互矛盾的比较扩展基础.请考虑以下示例:
class Base {
final int foo;
Base(int foo) {
this.foo = foo;
}
}
class A extends Base implements Comparable<Base> {
A(int foo) {
super(foo);
}
public int compareTo(Base that) {
return Integer.compare(this.foo, that.foo); // sort by foo ascending
}
}
class B extends Base implements Comparable<Base> {
B(int foo) {
super(foo);
}
public int …Run Code Online (Sandbox Code Playgroud) 是否有可能使用Twitter4j API获得超过100条推文?
如果是这样,任何人都可以指出这样做的方法吗?
是否存在AtomicInteger.accumulateAndGet()无法替换的场景AtomicInteger.updateAndGet(),或者仅仅是方法引用的便利?
这是一个简单的例子,我没有看到任何功能差异:
AtomicInteger i = new AtomicInteger();
i.accumulateAndGet(5, Math::max);
i.updateAndGet(x -> Math.max(x, 5));
Run Code Online (Sandbox Code Playgroud)
显然,同样也适用于getAndUpdate()和getAndAccumulate().
java ×10
java-8 ×4
lambda ×3
annotations ×1
arraylist ×1
atomic ×1
charsequence ×1
comparable ×1
constants ×1
escaping ×1
generics ×1
java-stream ×1
regex ×1
string ×1
swing ×1
syntax ×1
twitter4j ×1