小编Boe*_*dal的帖子

使用重复键将对象列表转换为映射

我有一个看起来像这样的课程:

case class Person(id : String, name : String, refId : String) {}
Run Code Online (Sandbox Code Playgroud)

我有一份人员名单.

我想要一个带有
key = refId
value = List [Person] 的地图,它具有相同的refId (重复键)

我做了什么:

val persons = getPersons() // get the List from somewhere
val refMap = new mutable.HashMap[String,Seq[Person]]()
for (person<- persons){
  refMap.put(person.refId,refMap.getOrElse(person.refId,new ArrayBuffer[Person]) :+ person)
}
Run Code Online (Sandbox Code Playgroud)

这是我的第一个想法,它的工作原理,但我想要更像Scala或更好看的东西.你有好主意吗?

我也试过这里写的:将元组列表转换为映射(并处理重复键?)

但他们使用Tuple,我也无法完成这项工作.我也尝试过将我的列表首先映射到元组但是
1.我不想在列表上迭代2次(如果没有必要)(1次创建元组,1次创建地图
.2.我试过但是我也失败了元组.

任何有关更好代码的帮助都会很好.

dictionary scala list duplicates

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

在非 spring 注入类中使用 application.properties

我有一个在此类中创建的类,new B(this);BI 希望使用 application.properties 中的值。但是因为(据我所知)因为它不是用 Spring 创建的,所以不会有任何注入(我使用注释@Value

这就是类的创建方式:

@Component
public class A {

    public B getB(){return new B(this);}

    /**
        a lot more stuff
    **/
}
Run Code Online (Sandbox Code Playgroud)

有问题的班级:

public class B {

    private A a;

    public B(A a){
        this.a = a;
    }

    @Value("${threshold}")
    private String threshold;  //this is null because it's not created with Spring

    public String getThreshold(){
        return threshold;
    }
    /**
        a lot more stuff
    **/

}
Run Code Online (Sandbox Code Playgroud)

所以我的问题如下:

1)如何使用 application.properties 文件中的值或

2)B怎么写,它是用Spring创建的?

一些背景信息:

  • 最初的代码不是我写的,所以不想改变太多,但想修改一下,以便将来可以更好地维护
  • 我没有那么多 Spring 知识,只是开始越来越熟悉它。 …

java javabeans properties-file spring-boot

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

使用数组修改数据框列

我有以下数据框:

+----------+ 
|col       | 
+----------+ 
|[1, 4, 3] | 
|[1, 5, 11]| 
|[1, 3, 3] | 
|[1, 4, 3] | 
|[1, 6, 3] | 
|[1, 1, 3] | 
+----------+
Run Code Online (Sandbox Code Playgroud)

我想要的是:

+----------+ 
|col_new   | 
+----------+ 
|[3, -1]   | 
|[4, 6]    | 
|[2, 0]    | 
|[3, -1]   | 
|[5, -3]   | 
|[0, 2]    | 
+----------+
Run Code Online (Sandbox Code Playgroud)

=> 差异运算符 arr[n+1] - arr[n]

而且我不知道该怎么做。

我想我应该用 udf 来做?我不太熟悉它,但是我尝试过。

+----------+ 
|col       | 
+----------+ 
|[1, 4, 3] | 
|[1, 5, 11]| 
|[1, 3, 3] | 
|[1, 4, 3] …
Run Code Online (Sandbox Code Playgroud)

user-defined-functions apache-spark apache-spark-sql pyspark

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