Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java8中更新流中的变量_Java_Java 8_Java Stream - Fatal编程技术网

在Java8中更新流中的变量

在Java8中更新流中的变量,java,java-8,java-stream,Java,Java 8,Java Stream,我只是在玩Java8。 现在我正在努力理解这条流。 到目前为止,我还没有看到一个关于在映射或列表中循环然后填充不属于映射或列表的变量的示例 以下是一个例子: class RandomObject { private int i1; private String s1; //setter //getter } // Java 7 RandomObject randomObj = new RandomObject(); Map<Integer, String&

我只是在玩Java8。 现在我正在努力理解这条流。 到目前为止,我还没有看到一个关于在映射或列表中循环然后填充不属于映射或列表的变量的示例

以下是一个例子:

class RandomObject {
    private int i1;
    private String s1;
    //setter
    //getter
}

// Java 7
RandomObject randomObj = new RandomObject();
Map<Integer, String> mappy = new HashMap<Integer, String>();
Map<Integer, String> collect = new HashMap<Integer, String>();

mappy.put(1, "good map");
mappy.put(2, "nice map");
mappy.put(3, "wow");
mappy.put(4, "mappy the best map");

for (Map.Entry<Integer, String> entry : mappy.entrySet()) {
    if (entry.getKey() == 2) {
        randomObj.seti1(entry.getKey());
        randomObj.sets1(entry.getValue());
        collect.put(entry.getKey(), entry.getValue());
    }
}

// Java 8
RandomObject randomObj = new RandomObject();
Map<Integer, String> mappy = new HashMap<Integer, String>();

mappy.put(1, "good map");
mappy.put(2, "nice map");
mappy.put(3, "wow");
mappy.put(4, "mappy the best map");

Map<Integer, String> collect = mappy.entrySet().stream()
    .filter(map -> map.getKey() == 2)
    .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
// Hmmm i don't know where to put randomObj
类随机对象{
私有int i1;
私有字符串s1;
//塞特
//吸气剂
}
//爪哇7
RandomObject randomObj=新的RandomObject();
Map mappy=新的HashMap();
Map collect=newhashmap();
mappy.put(1,“良好地图”);
mappy.put(2,“漂亮的地图”);
mappy.put(3,“哇”);
mappy.put(4,“mappy最佳地图”);
对于(Map.Entry:mappy.entrySet()){
if(entry.getKey()==2){
randomObj.seti1(entry.getKey());
randomObj.sets1(entry.getValue());
collect.put(entry.getKey(),entry.getValue());
}
}
//爪哇8
RandomObject randomObj=新的RandomObject();
Map mappy=新的HashMap();
mappy.put(1,“良好地图”);
mappy.put(2,“漂亮的地图”);
mappy.put(3,“哇”);
mappy.put(4,“mappy最佳地图”);
Map collect=mappy.entrySet().stream()
.filter(map->map.getKey()==2)
.collect(Collectors.toMap(p->p.getKey(),p->p.getValue());
//嗯,我不知道放在哪里

函数式编程依赖于不变性


您没有更新流;您正在对一个进行操作,您必须使用诸如map、reduce、filter等操作创建一个新的映射。

因为您正在使用映射,所以除了“使用流”之外,使用这样的样板代码是没有意义的,因为一个映射不能有相同的键两次

这是最好的解决方案

String s = mappy.get(2);
if (s==null) {
    throw new IllegalStateException("No value with key = 2 were present");
}
new RandomObject(2, s);
但是如果您真的不想使用流,我看到三种解决方案:

丑陋的一个(即使它更快):

更好的是:

Optional<RandomObject> randomObj = mappy.entrySet().stream()
    .filter(map -> map.getKey() == 2)
    .mapToObj(entry -> new RandomObject(entry.getKey(), entry.getValue()))
    .findFirst();

if (!randomObj.isPresent()) {
    throw new IllegalStateException("No value with key = 2 were present");
}
Optional randomObj=mappy.entrySet().stream()可选
.filter(map->map.getKey()==2)
.mapToObj(entry->new RandomObject(entry.getKey(),entry.getValue())
.findFirst();
如果(!randomObj.isPresent()){
抛出新的IllegalStateException(“不存在key=2的值”);
}

太好了。。不过我有一些问题。。我是否错误地使用了流?stream打算这么做吗?在这种情况下,我应该坚持使用Java 7实现吗?如果我将对象属性的设置更改为类似于函数调用的内容会怎么样。。在流中调用它是否正常?在某些情况下可能是这样,但这取决于您试图实现的目标。就我个人而言,这是我将尽量避免的事情,这不是构建流的目的。您应该将最后一个解决方案放在最前面。在大多数情况下,可以通过
String s=mappy.get(2)避免执行两个相同的查找(
containsKey(2)
get(2)
);如果(s==null)抛出新的IllegalStateException(“不存在键为2的值”);RandomObject randomObj=新的RandomObject(2,s)由于我们不知道附加映射的用途,因此可以通过
mappy=Collections.singletonMap(2,s)创建它如果真的需要。确切地说,要不惜一切代价避免改变流外部的东西。流的设计也很好,目的是获得一种“简单”的方法来并行化收集处理,这基本上依赖于不变性。。这是有道理的。.forEach()如何?对于所有流操作都是如此。这就是函数式编程的工作原理。
Map<Integer, String> collect = mappy.entrySet().stream()
    .filter(map -> map.getKey() == 2)
    .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

collect.entrySet().stream()
.forEach(entry -> {
    randomObj.seti1(entry.getKey());
    randomObj.sets1(entry.getValue());
});
Optional<RandomObject> randomObj = mappy.entrySet().stream()
    .filter(map -> map.getKey() == 2)
    .mapToObj(entry -> new RandomObject(entry.getKey(), entry.getValue()))
    .findFirst();

if (!randomObj.isPresent()) {
    throw new IllegalStateException("No value with key = 2 were present");
}