小编gho*_*der的帖子

为什么 Arrays.stream() 不支持 char[] 唯一的数组?

在经历将原始数组转换为 Streams 的方法时,我发现char[]不支持,而支持其他原始数组类型。有什么特别的理由将它们排除在流中吗?

java java-8

43
推荐指数
4
解决办法
3166
查看次数

迭代和复制HashMap值的有效方法

我想转换:

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)

java collections java-8 java-stream

9
推荐指数
1
解决办法
233
查看次数

SynchronizedMap ConcurrentModificationException

我试图了解 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)

java concurrency multithreading synchronization

6
推荐指数
1
解决办法
1439
查看次数

Difference between estimatedSize and getExactSizeIfKnown in Spliterator

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)

java java-8 spliterator

5
推荐指数
1
解决办法
99
查看次数

java中的char范围

以下Java代码行产生错误.
即使java中的数据类型已签名?

    char c = -128;  
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
2
解决办法
2万
查看次数

流API map()和filter()

我正在尝试学习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)

有人可以解释这里的区别吗?

java lambda java-8 java-stream

3
推荐指数
1
解决办法
246
查看次数

不建议在 ExecutorService 中使用 wait(),notify() 方法

我是多线程的初学者,在 OCJP7 版本中遇到以下问题:

避免在提交给 Executor 或 ExecutorService 的任务(Runnable 和 Callable 实例)中使用 Object.wait、Object.notify 和 Object .notifyAll 等方法。

有人可以解释一下为什么会这样吗?

java multithreading threadpool

2
推荐指数
1
解决办法
458
查看次数

为什么函数没有静态参数?

以下是c中的代码.

     fact(2);
     void fact(static int i)
     {..}
Run Code Online (Sandbox Code Playgroud)

输出:错误不能有静态参数
那么为什么我们不能在函数中有静态参数?

c

1
推荐指数
2
解决办法
7779
查看次数

JUNIT5 中的 AssertThrows

我努力学习assertThrowsjunit5,它需要Executable作为具有第二ARGvoid execute()方法。但是,在下面的示例中,我们正在通过相同的示例传递它,LAMBDA它返回一个带有方法的双精度值double divide(int a , int b)。现在,如果下面的 lambdaexecuteExcecutable. 应该给compile error吧?

assertThrows(ArithmeticException.class,() -> m.divide(1, 0),"Failed");
Run Code Online (Sandbox Code Playgroud)

java junit java-8

1
推荐指数
1
解决办法
95
查看次数

用户定义的函数只能有select语句

UDF和SP之间的主要区别之一是UDF只能在其中包含select语句而不能插入/更新/删除语句.有人可以解释一下这背后的原因吗?以下功能:

create function test(..)
...
BEGIN 
insert into EMPLOYEE('22',12000,'john');
return 0;
END
Run Code Online (Sandbox Code Playgroud)

无效.但为什么会这样呢?

sql oracle plsql stored-procedures user-defined-functions

0
推荐指数
1
解决办法
452
查看次数