在java-8新的注释中@sun.misc.Contended出现.
有几篇写得很好的文章,解释了它的作用以及如何使用它:
但是在任何地方都没有解释,value这个注释是什么?我的意思是,例如在java.lang.Thread它中使用如下:
@sun.misc.Contended("tlr")
int threadLocalRandomProbe;
Run Code Online (Sandbox Code Playgroud)
这个"tlr"价值是多少?它有什么影响?如果这value是默认(空)会发生什么?
我想使用Java 8中的函数映射将a转换Map<String, Integer>为另一个Map<String, Long>映射,在两个映射中将数据与按键匹配的函数进行匹配.您可以假设两个地图都具有相同的键.
我尝试了以下方法:
Map<String, Integer> inputData = new HashMap<>();
inputData.put("A",8);
inputData.put("B",7);
inputData.put("C",6);
Map<String, Function<Integer, Long>> transformers = new HashMap<>();
transformers.put("A", x -> x*2L);
transformers.put("B", x -> x+3L);
transformers.put("C", x -> x+11L);
Map<String, Long> mappedData = inputData.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<>(
entry.getKey(),
transformers.get(entry.getKey()).apply(entry.getValue())))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
Run Code Online (Sandbox Code Playgroud)
预期结果:{A = 16,B = 10,C = 17}.
在Java Streams API中是否有更简单的方法表达" 将变换器的映射应用于按键匹配的inputData映射 "?
我想将字符串列表转换为大写.
这是我的代码:
List<String> list = Arrays.asList("abc", "def", "ghi");
List<String> upped = list.stream().map(String::toUpperCase).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
有没有更简单/更好的方法来做到这一点?
假设您有一个这样的方法来计算Collection某些方法的最大值ToIntFunction:
static <T> void foo1(Collection<? extends T> collection, ToIntFunction<? super T> function) {
if (collection.isEmpty())
throw new NoSuchElementException();
int max = Integer.MIN_VALUE;
T maxT = null;
for (T t : collection) {
int result = function.applyAsInt(t);
if (result >= max) {
max = result;
maxT = t;
}
}
// do something with maxT
}
Run Code Online (Sandbox Code Playgroud)
使用Java 8,可以将其转换为
static <T> void foo2(Collection<? extends T> collection, ToIntFunction<? super T> function) {
T maxT = collection.stream()
.max(Comparator.comparingInt(function))
.get();
// …Run Code Online (Sandbox Code Playgroud) 试图建立一个alexa(亚马逊:回声)技能组合.同时,尝试使用这种经验作为通过匕首2依赖注入的学习测试平台.但是,使用maven-2 cmd构建包:
mvn assembly:assembly -DdescriptorId=jar-with-dependencies package'.
Run Code Online (Sandbox Code Playgroud)
生成具有完整依赖项的zip jar会产生以下异常跟踪:
[INFO] ------------------------------------------------------------------------
[INFO] Building Echo Device Client 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ echo-device-client ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/apil.tamang/Dropbox/Git/echo-device-client/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ echo-device-client ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling …Run Code Online (Sandbox Code Playgroud) 我最近安装了jdk 1.8 u92,之后Android Studio表示api24版本需要它.但是,我现在遇到了这个问题.
我查看了一些其他解决方案并卸载了jdk 1.7,为JDK_HOME,JAVA_HOME和JAVA8_HOME添加了环境变量.问题仍然存在.
每次我尝试构建应用程序时,gradle sync都会失败,Android studio会要求我选择一个有效的JDK目录.然后我转到Project Structure => JDK Location并将其更新到1.8文件夹.尝试再次构建会产生相同的错误,当我检查项目结构时,路径将自动重新设置为旧的JDK 1.7.
这个问题有什么问题吗?
给定一个具有属性的项目列表,我试图让最后一个项目显示所述属性的最大值.
例如,对于以下对象列表:
t i
A: 3
D: 7 *
F: 4
C: 5
X: 7 *
M: 6
Run Code Online (Sandbox Code Playgroud)
我可以得到最高的东西之一i:
Thing t = items.stream()
.max(Comparator.comparingLong(Thing::getI))
.orElse(null);
Run Code Online (Sandbox Code Playgroud)
但是,这会得到我Thing t = D.是否有一种干净而优雅的方式来获取最后一项,即X在这种情况下?
一种可能的解决方案是使用该reduce功能.但是,该属性是动态计算的,它看起来更像是:
Thing t = items.stream()
.reduce((left, right) -> {
long leftValue = valueFunction.apply(left);
long rightValue = valueFunction.apply(right);
return leftValue > rightValue ? left : right;
})
.orElse(null);
Run Code Online (Sandbox Code Playgroud)
在valueFunction现在需要将近一倍,而调用.
其他明显的迂回解决方案是:
考虑以下代码:
public static void main(String[] args) {
Stream.iterate(1, i -> i + 1)
.flatMap(i -> Stream.of(i, i, i))
.peek(System.out::println)
.limit(4)
.forEach(i -> {});
}
Run Code Online (Sandbox Code Playgroud)
Java 8的输出:
1
1
1
2
2
2
Run Code Online (Sandbox Code Playgroud)
在Java 11中:
1
1
1
2
Run Code Online (Sandbox Code Playgroud)
这是Java 8中的错误或预期的行为,在11中进行了更改吗?
上面的代码只是演示不同行为的示例,但是更重要的含义是,以下代码在Java 11中显示1,2,3,但在Java 8中进入了无限循环:
Stream.iterate(0, i -> i + 10)
.flatMap(i -> Stream.iterate(i + 1, j -> j + 1))
.limit(3)
.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud) 我如何使用收集器将DAO列表转换为 Map<String, List<Pojo>>
daoList看起来像这样:
[0] : id = "34234", team = "gools", name = "bob", type = "old"
[1] : id = "23423", team = "fool" , name = "sam", type = "new"
[2] : id = "34342", team = "gools" , name = "dan", type = "new"
Run Code Online (Sandbox Code Playgroud)
我想对“团队”属性进行分组,并为每个团队提供一个列表,如下所示:
"gools":
["id": 34234, "name": "bob", "type": "old"],
["id": 34342, "name": "dan", "type": "new"]
"fool":
["id": 23423, "name": "sam", "type": "new"]
Run Code Online (Sandbox Code Playgroud)
Pojo看起来像这样:
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class Pojo{
private String …Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×9
java-stream ×6
android ×1
annotations ×1
collectors ×1
dagger-2 ×1
flatmap ×1
generics ×1
java-11 ×1
maven-2 ×1