在经历将原始数组转换为 Streams 的方法时,我发现char[]不支持,而支持其他原始数组类型。有什么特别的理由将它们排除在流中吗?
我想转换:
Map<String, Map<String, List<Map<String, String>>>> inputMap
Run Code Online (Sandbox Code Playgroud)
到:
Map<String, Map<String, CustomObject>> customMap
Run Code Online (Sandbox Code Playgroud)
inputMap在配置中提供并准备就绪,但我需要customMap格式化。CustomObject 将通过List<Map<String, String>>在函数中使用几行代码来派生。
我已经尝试了迭代输入映射和复制 customMap 中的键值的正常方法。有没有使用 Java 8 或其他一些快捷方式来做到这一点的有效方法?
Map<String, Map<String, List<Map<String, String>>>> configuredMap = new HashMap<>();
Map<String, Map<String, CustomObj>> finalMap = new HashMap<>();
for (Map.Entry<String, Map<String, List<Map<String, String>>>> attributeEntry : configuredMap.entrySet()) {
Map<String, CustomObj> innerMap = new HashMap<>();
for (Map.Entry<String, List<Map<String, String>>> valueEntry : attributeEntry.getValue().entrySet()) {
innerMap.put(valueEntry.getKey(), getCustomeObj(valueEntry.getValue()));
}
finalMap.put(attributeEntry.getKey(), innerMap);
}
private CustomObj getCustomeObj(List<Map<String, String>> list) {
return new CustomObj();
}
Run Code Online (Sandbox Code Playgroud) 我试图了解 SynchronizedMap 并运行以下代码。我得到以下输出,但有一个例外。根据我的理解,当线程仍在对映射执行写入或线程处于睡眠状态时,get() 方法尝试访问同步映射时会导致异常。我的理解是正确的还是我遗漏了什么?
class MapHelper1 implements Runnable {
Map<String, Integer> map;
public MapHelper1(Map<String, Integer> map) {
this.map = map;
new Thread(this, "MapHelper1").start();
}
public void run() {
map.put("One", 1);
try {
System.out.println("MapHelper1 sleeping");
Thread.sleep(100);
} catch (Exception e) {
System.out.println(e);
}
}
}
class MapHelper2 implements Runnable {
Map<String, Integer> map;
public MapHelper2(Map<String, Integer> map) {
this.map = map;
new Thread(this, "MapHelper3").start();
}
public void run() {
map.put("two", 1);
try {
System.out.println("MapHelper2 sleeping");
Thread.sleep(100);
} catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) I am trying to understand the features of Spliterator and came across these 2 methods estimatedSize and getExactSizeIfKnown I could figure out what is estimatedSize but not sure exactly what doesgetExactSizeIfKnowndo. Can someone please give an example explaining the difference between the two.
EDIT: I tried the following example in which both of them are the same. In which cases would they be different?
public static void main(String[] args) {
List<Integer> l = new ArrayList<>();
l.add(1);
l.add(2);
l.add(3); …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习Stream API,我遇到了以下代码:
public class Test{
public static void main (String[] args)
{
List <Integer> a = Arrays.asList(2,3,4);
System.out.println(a.stream().filter(i -> i < 10).average());
}
}
Run Code Online (Sandbox Code Playgroud)
它给了一个
符号"average()"未找到错误.
但是,当我用下面的更改sysout时,它按预期运行正常
System.out.println(a.stream().filter(i -> i < 10).mapToInt(i -> i).average());
Run Code Online (Sandbox Code Playgroud)
有人可以解释这里的区别吗?
我是多线程的初学者,在 OCJP7 版本中遇到以下问题:
避免在提交给 Executor 或 ExecutorService 的任务(Runnable 和 Callable 实例)中使用 Object.wait、Object.notify 和 Object .notifyAll 等方法。
有人可以解释一下为什么会这样吗?
以下是c中的代码.
fact(2);
void fact(static int i)
{..}
Run Code Online (Sandbox Code Playgroud)
输出:错误不能有静态参数
那么为什么我们不能在函数中有静态参数?
我努力学习assertThrows的junit5,它需要Executable作为具有第二ARGvoid execute()方法。但是,在下面的示例中,我们正在通过相同的示例传递它,LAMBDA它返回一个带有方法的双精度值double divide(int a , int b)。现在,如果下面的 lambdaexecute与Excecutable. 应该给compile error吧?
assertThrows(ArithmeticException.class,() -> m.divide(1, 0),"Failed");
Run Code Online (Sandbox Code Playgroud) UDF和SP之间的主要区别之一是UDF只能在其中包含select语句而不能插入/更新/删除语句.有人可以解释一下这背后的原因吗?以下功能:
create function test(..)
...
BEGIN
insert into EMPLOYEE('22',12000,'john');
return 0;
END
Run Code Online (Sandbox Code Playgroud)
无效.但为什么会这样呢?
java ×8
java-8 ×5
java-stream ×2
c ×1
collections ×1
concurrency ×1
junit ×1
lambda ×1
oracle ×1
plsql ×1
spliterator ×1
sql ×1
threadpool ×1