代码之家  ›  专栏  ›  技术社区  ›  Metadata

是否有一个Scala集合来维护insert的顺序?

  •  0
  • Metadata  · 技术社区  · 6 年前

    List:hdtList

    forecast_id bigint,period_year bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_system_name string,source_record_type string,gl_source_name string,gl_source_system_name string,year string
    

    我有一个 List: partition_columns source_system_name, period_year

    使用 列表:分区列 ,我正在尝试匹配它们并在中移动相应的列 List: hdtList 结束语如下:

      val (pc, notPc) = hdtList.partition(c => partition_columns.contains(c.takeWhile(x => x != ' ')))
    

    但当我把它们打印成: println(notPc.mkString(",") + "," + pc.mkString(","))

     forecast_id bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_record_type string,gl_source_name string,gl_source_system_name string,year string,period string,period_year bigint,source_system_name string
    

    period_year 先到先得 source_system_name 最后一个。是否有任何我可以使数据如下,以便列的顺序在 保持。

     forecast_id bigint,period_num bigint,period_name string,drm_org string,ledger_id bigint,currency_code string,source_record_type string,gl_source_name string,gl_source_system_name string,year string,period string,source_system_name string,period_year bigint
    

    1 回复  |  直到 6 年前
        1
  •  2
  •   Alexey Romanov    6 年前

    你使用哪一个集合并不重要;你只使用 partition_columns 打电话 contains

    但你的代码确实维持了秩序:它只是 hdtList 的。

    像这样的

    // get is ugly, but safe here
    val pc1 = partition_columns.map(x => pc.find(y => y.startsWith(x)).get)