标签: collect

使用collect闭包方法在groovy中填充HashMap

我试图从列表填充地图.这就是我在做什么.

itemNoList = [1,2,3,4]
bookMap = [:]
bookMap = itemNoList.collect{ [ (it) : it+1 ] }
Run Code Online (Sandbox Code Playgroud)

当我这样做时,bookMap变为ArrayList类型,现在有一个HashMap列表.

bookMap现在是[{1=2}, {2=3}, {3=4}, {4=5}],即地图列表.

我怎样才能使用collect方法从ArrayList获取HashMap ?通过使用each替代方法很容易解决这个问题collect,但我只是好奇它是否可以使用collect.

groovy closures arraylist hashmap collect

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

Java 8收集vs reduce

众所周知,在进行累积时,"reduce"总是返回一个新的不可变对象,而"collect"将对可变对象进行更改.

但是,当我不小心为reduce和collect方法分配一个方法引用时,它编译时没有任何错误.为什么?

看看下面的代码:

public class Test {
    @Test
    public void testReduce() {

        BiFunction<MutableContainer,Long,MutableContainer> func =
            MutableContainer::reduce;

        // Why this can compile?
        BiConsumer<MutableContainer,Long> consume =
            MutableContainer::reduce;

        // correct way:
        //BiConsumer<MutableContainer,Long> consume =
        //  MutableContainer::collect;


        long param=10;

        MutableContainer container = new MutableContainer(0);


        consume.accept(container, param);
        // here prints "0",incorrect result,
        // because here we expect a mutable change instead of returning a immutable value
        System.out.println(container.getSum());

        MutableContainer newContainer = func.apply(container, param);
        System.out.println(newContainer.getSum());
    }
}

class MutableContainer {
    public MutableContainer(long sum) {
        this.sum = sum; …
Run Code Online (Sandbox Code Playgroud)

java reduce collect java-8

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

java 8 groupingBy列表列表

假设我们有一个对象Draw:

class Draw {

private int num1, num2, num3;

public Draw (int num1, int num2, int num3) {
    this.num1 = num1;
    this.num2 = num2;
    this.num3 = num3;
}

public int getNum1() {
    return num1;
}

public void setNum1(int num1) {
    this.num1 = num1;
}

public int getNum2() {
    return num2;
}

public void setNum2(int num2) {
    this.num2 = num2;
}

public int getNum3() {
    return num3;
}

public void setNum3(int num3) {
    this.num3 = num3;
}

public List<Integer> getResultAsList() { …
Run Code Online (Sandbox Code Playgroud)

mapping grouping collect java-8 java-stream

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

Pypsark-使用collect_list时保留空值

按照接受的答案pyspark collect_set或GROUPBY collect_list,当你做一个collect_list特定列,在null此列值将被删除。我已经检查过了,这是真的。

但就我而言,我需要保留null列-如何实现此目的?

我没有找到有关此类collect_list功能变体的任何信息。


解释我为什么要空值的背景上下文:

我有一个数据框df如下:

cId   |  eId  |  amount  |  city
1     |  2    |   20.0   |  Paris
1     |  2    |   30.0   |  Seoul
1     |  3    |   10.0   |  Phoenix
1     |  3    |   5.0    |  null
Run Code Online (Sandbox Code Playgroud)

我想使用以下映射将其写入Elasticsearch索引:

"mappings": {
    "doc": {
        "properties": {
            "eId": { "type": "keyword" },
            "cId": { "type": "keyword" },
            "transactions": {
                "type": "nested", 
                "properties": {
                    "amount": { "type": …
Run Code Online (Sandbox Code Playgroud)

nested collect elasticsearch-mapping elasticsearch-hadoop pyspark-sql

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

slurp 选项的 jq 等价物是什么?

这会生成一个数组列表:

$ echo -e "a 1\nb 2" | jq -R 'split(" ")'
[
  "a",
  "1"
]
[
  "b",
  "2"
]
Run Code Online (Sandbox Code Playgroud)

当我吸食输入时,我得到一个数组:

$ echo -e "a 1\nb 2" | jq -R 'split(" ")' | jq -s .
[
  [
    "a",
    "1"
  ],
  [
    "b",
    "2"
  ]
]
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将列表转换为数组而不使用它时,我得到一个数组列表而不是单个数组:

$ echo -e "a 1\nb 2" | jq -R '[split(" ")]'
[
  [
    "a",
    "1"
  ]
]
[
  [
    "b",
    "2"
  ]
]
Run Code Online (Sandbox Code Playgroud)

是否可以在split不将结果通过管道传输到 的新实例中的情况下获取 的结果jq

collect jq

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

Java 8流,如何在reduce或collect中“中断”而不抛出运行时异常?

这个问题是在 的背景下提出的forEach

评论(答案被接受后):我接受了@nullpointer的答案,但它仅在我的代码示例的上下文中才是正确的,而不是在关于reduce的可破坏性的一般问题中。

问题:

但是有没有一种方法可以reducecollect不遍历所有流元素的情况下提前“中断”?(这意味着我需要在迭代时累积状态,所以我使用reducecollect)。

简而言之:我需要迭代流的所有元素(元素是整数并从小到大排序),但是查看 2 个相邻元素并比较它们,如果它们之间的差异大于 1,我需要“中断”并停止“累积状态”,我需要返回最后传递的元素。

抛出 a 的变体RuntimeException和传递外部状态的变体 - 对我来说不好。

带注释的代码示例:

public class Solution {

public int solution(int[] A) {

    Supplier<int[]> supplier = new Supplier<int[]>() {
        @Override
        public int[] get() {
            //the array describes the accumulated state:
            //first element in the array , if set > 0, means  - the result is achieved, we can stop iterate over the rest elements
            //second element in …
Run Code Online (Sandbox Code Playgroud)

java reduce collect java-8 java-stream

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

Cypher COLLECT 使 UNWIND 以错误的顺序展开

图要点:http://gist.neo4j.org/?6182d024325343760cb4

我想按顺序获得一条(最长的)路径,并且它会按预期工作,直到我添加 COLLECT 语句,是否有关于 Cypher 和 COLLECT 的内容我不明白,或者这是一个错误?

此查询按预期工作,以正确的顺序返回路径中的节点:

MATCH (n:Cable { name: 'Cable3' })-[:Connected_to*]-(port:Port)
OPTIONAL MATCH path=(port)-[:Connected_to*]-()
WITH nodes(path) AS parts, length(path) AS len
ORDER BY len DESC 
LIMIT 1 UNWIND parts AS part
RETURN part
Run Code Online (Sandbox Code Playgroud)

这个没有 COLLECT 语句,会按正确的顺序返回节点,还会返回部件和父级之间的节点(如预期)。

MATCH (n:Cable { name: 'Cable3' })-[:Connected_to*]-(port:Port)
OPTIONAL MATCH path=(port)-[:Connected_to*]-()
WITH nodes(path) AS parts, length(path) AS len
ORDER BY len DESC
LIMIT 1 UNWIND parts AS part
OPTIONAL MATCH (part)<-[:Has*1..10]-(parent)
RETURN part, parent
Run Code Online (Sandbox Code Playgroud)

此查询未按预期工作,以另一种顺序返回路径中的节点:

MATCH (n:Cable { name: 'Cable3' })-[:Connected_to*]-(port:Port) …
Run Code Online (Sandbox Code Playgroud)

collect neo4j longest-path cypher

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

numpy - 返回索引如果 3d 数组之一内的值

如何在 Numpy 中执行此操作:谢谢!

输入 :

A = np.array([0, 1, 2, 3]) 

B = np.array([[3, 2, 0], [0, 2, 1], [2, 3, 1], [3, 0, 1]]) 
Run Code Online (Sandbox Code Playgroud)

输出 :

result = [[0, 1, 3], [1, 2, 3], [0, 1, 2], [0, 2, 3]]
Run Code Online (Sandbox Code Playgroud)

在Python中:

A = np.array([0 ,1 ,2 ,3]) 
B = np.array([[3 ,2 ,0], [0 ,2 ,1], [2 ,3 ,1], [3 ,0 ,1]]) 
result = []
for x ,  valA in enumerate (A) :
    inArray = []
    for y , valB …
Run Code Online (Sandbox Code Playgroud)

python arrays indexing numpy collect

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

oracle sql:收集聚合

我想按属性对我的数据库条目进行分组,并同时知道每个组中有哪些条目。我使用 Oracle COLLECT 函数COLLECT 函数收集分组条目的 ID

DECLARE
  TYPE ids_type IS TABLE OF number(19, 0);   
  ids ids_type;
BEGIN 
  select cast(collect(r.id) as ids_type) into ids from rechnungsdaten r group by r.status;
END;
Run Code Online (Sandbox Code Playgroud)

但后来我得到了错误:

错误报告 -
ORA-06550:第 5 行,第 44 列:
PL/SQL:ORA-00902:无效数据类型
ORA-06550:第 5 行,第 5 列:
PL/SQL:SQL 语句被忽略
06550。00000 -“行 %s,列%s:\n%s"
*原因:通常是 PL/SQL 编译错误。
*行动:

这里有什么问题?

oracle plsql group-by collect

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

Function::identity 在 Collectors.toMap 中不起作用

我试图将 a 转换List<String>为 a Map<T, String>,映射的值是前一个 中包含的元素List<String>,键是该元素的某个属性String(例如, 的长度String,在这种情况下,T实际上是 an Integer)。

我首先尝试这样做。这是我上面提到的示例的实现。我希望 的长度String成为键,而其String本身成为值。我想使用该Function::identity函数来明确指定该值是其String本身。

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SOQ_ME_20220522_fail
{

   public static void main(String[] args)
   {
   
      final List<String> list = List.of("apple", "banana", "coconut");
      
      var result = 
         list.stream()
            .collect(
               Collectors.toMap(
                  each -> each.length(),
                  Function::identity
               )
            )
         ;

      System.out.println(result);
   
   }
   
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试编译它时,出现以下编译错误。

SOQ_ME_20220522_fail.java:15: error: no suitable method found for toMap((each)->ea[...]gth(),Function::identity) …
Run Code Online (Sandbox Code Playgroud)

java dictionary collect java-stream collectors

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