我有一个返回不可变列表的方法。我想向其中添加元素,这就是为什么必须将其转换为可变列表的原因。目前,我正在从不可变列表中创建一个新的ArrayList,如下所示:
final List<someDTO> mutableList = new ArrayList<>(someDTO.getImmutableList());
是否有更好的方法,例如使用某些集合复制方法,java流或类似方法?
答案 0 :(得分:5)
说实话,这是最好的,但是另一个变种是:
scala> val df = Seq(("ServiceCent4","AP-1-IOO-PPP","241.206.155.172","06-12-18:17:42:34",162,53,1544098354885L)).toDF("COL1","COL2","COL3","EventTime","COL4","COL5","COL6")
df: org.apache.spark.sql.DataFrame = [COL1: string, COL2: string ... 5 more fields]
scala> df.show(1,false)
+------------+------------+---------------+-----------------+----+----+-------------+
|COL1 |COL2 |COL3 |EventTime |COL4|COL5|COL6 |
+------------+------------+---------------+-----------------+----+----+-------------+
|ServiceCent4|AP-1-IOO-PPP|241.206.155.172|06-12-18:17:42:34|162 |53 |1544098354885|
+------------+------------+---------------+-----------------+----+----+-------------+
only showing top 1 row
scala> df.printSchema
root
|-- COL1: string (nullable = true)
|-- COL2: string (nullable = true)
|-- COL3: string (nullable = true)
|-- EventTime: string (nullable = true)
|-- COL4: integer (nullable = false)
|-- COL5: integer (nullable = false)
|-- COL6: long (nullable = false)
scala> val schema = df.schema
schema: org.apache.spark.sql.types.StructType = StructType(StructField(COL1,StringType,true), StructField(COL2,StringType,true), StructField(COL3,StringType,true), StructField(EventTime,StringType,true), StructField(COL4,IntegerType,false), StructField(COL5,IntegerType,false), StructField(COL6,LongType,false))
scala> val df2 = df.columns.foldLeft(df){ (acc,r) => acc.withColumn(r,col(r).cast("string")) }
df2: org.apache.spark.sql.DataFrame = [COL1: string, COL2: string ... 5 more fields]
scala> df2.printSchema
root
|-- COL1: string (nullable = true)
|-- COL2: string (nullable = true)
|-- COL3: string (nullable = true)
|-- EventTime: string (nullable = true)
|-- COL4: string (nullable = false)
|-- COL5: string (nullable = false)
|-- COL6: string (nullable = false)
scala> val x = df2.flatMap(r => (0 until schema.length).map { idx => ((idx, r.getString(idx)), 1l) } )
x: org.apache.spark.sql.Dataset[((Int, String), Long)] = [_1: struct<_1: int, _2: string>, _2: bigint]
scala> x.show(5,false)
+---------------------+---+
|_1 |_2 |
+---------------------+---+
|[0,ServiceCent4] |1 |
|[1,AP-1-IOO-PPP] |1 |
|[2,241.206.155.172] |1 |
|[3,06-12-18:17:42:34]|1 |
|[4,162] |1 |
+---------------------+---+
only showing top 5 rows
scala>
答案 1 :(得分:3)
实际上没有更好的方法。
有人认为Collectors.toList
带有静态导入的流使其看起来更好,但这纯粹是美观的。
List<someDTO> mutableList = someDTO.getImmutableList().stream().collect(toList());