首页 > 解决方案 > 在Java中的对象初始化期间根据构造函数变量值更改键?

问题描述

即使我做了几年程序员,我仍然总是试图找到最好的方法,

这是我的挑战之一,我想以最好的方式做到这一点,

MutableEventInstance mutableEventInstance = new MutableEventInstance(key, start, stop, value);

类定义

public class MutableEventInstance {

   private int key;
   private int start;
   private int stop;
   private double value;

   /*'***************************************************************************************
   *   Class members                                         
   ******************************************************************************************/

   public MutableEventInstance(int key, int start, int stop, double value) {
      this.key = key;
      this.start = start;
      this.stop = stop;
      this.value = value;
   }

   /**
    * @return the key
    */
   public int getKey() {
      return key;
   }

   /**
    * @return the start
    */
   public int getStart() {
      return start;
   }

   /**
    * @return the stop
    */
   public int getStop() {
      return stop;
   }

   /**
    * @return the value
    */
   public double getValue() {
      return value;
   }

   /**
    * @param key the key to set
    */
   public void setKey(int key) {
      this.key = key;
   }

   /**
    * @param start the start to set
    */
   public void setStart(int start) {
      this.start = start;
   }

   /**
    * @param stop the stop to set
    */
   public void setStop(int stop) {
      this.stop = stop;
   }

   /**
    * @param value the value to set
    */
   public void setValue(double value) {
      this.value = value;
   }

}

当任何开始/停止值与以前的值不同时,键值将从零开始递增。

我只能猜测,存储以前的开始和停止值,然后递增。

有什么最好的方法吗?请指教。

我需要根据 Spark 2.3 中的键聚合值

示例:当启动/停止发生变化时,键值需要增加。我该怎么做呢 ?在这些对象的初始化发生的方法上?

Key=0, Start = 3, Stop = 7, Value = -10.0
Key=0, Start = 3, Stop = 7, Value = 0.0
Key=0, Start = 3, Stop = 7, Value = 0.0
Key=0, Start = 3, Stop = 7, Value = 3.03
Key=0, Start = 3, Stop = 7, Value = 0.0
Key=0, Start = 3, Stop = 7, Value = 0.0
Key=0, Start = 3, Stop = 7, Value = 2.5

Key=1, Start = 5, Stop = 6, Value = -10.0
Key=1, Start = 5, Stop = 6, Value = 0.0
Key=1, Start = 5, Stop = 6, Value = 0.0
Key=1, Start = 5, Stop = 6, Value = 3.03
Key=1, Start = 5, Stop = 6, Value = 0.0
Key=1, Start = 5, Stop = 6, Value = 0.0
Key=1, Start = 5, Stop = 6, Value = 2.5

标签: javaapache-sparkjava-8

解决方案


推荐阅读