【问题标题】:Filter map to one row per multiple of ten,based on difference in decreasing order in java根据java中降序的差异,将映射过滤到每十的倍数中的一行
【发布时间】:2015-03-23 05:06:22
【问题描述】:

我有一种方法可以过滤每十的倍数的行,即我可以按升序过滤最接近十的倍数的行,例如 10、20、30 等。现在我想在降序。

请参考以下链接-Filter array to one row per multiple of ten, based on difference?

在上面提到的链接中,相同的过程是按升序完成的,我想按降序执行此操作并将值存储在地图中。但我做不到。

我正在使用以下代码以递增的顺序检索 beam_current 是 10 的倍数的行-

public static  LinkedHashMap<Double, String> ClosestToMultiplesOfTen_User() throws SQLException {

    int row_id ;
    int bIdx = 0;
    double[] vals = new double[34];
   // double[] bucket =new double[bucketCount];
    int rowIndex = 0 ;
    int i=0;

    try
            { 
              con = getConnection();
              stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
           //   String sql="select logtime,beam_current from INDUS2_BDS.dbo.DCCT where logtime between '"+name+" 00:00:00' and '"+name+" 23:59:59'"+
            //  "and (beam_current like '%9.96' or beam_current like '%9.97' or beam_current like '%9.98' or  beam_current like '%9.99'  or beam_current like '%0' or beam_current like '%_0.01' or beam_current like '%_0.02' or beam_current like '%_0.03' or beam_current like '%_0.04' or beam_current like '%_0.05' or beam_current like '%_0.06') and beam_energy between '550' and '552'";

              String sql="select logtime,beam_current from INDUS2_BDS.dbo.DCCT where logtime between '2014-10-10 08:50:00' and '2014-10-10 12:50:00'"+
                      "and (beam_current like '%9.96' or beam_current like '%9.97' or beam_current like '%9.98' or  beam_current like '%9.99'  or beam_current like '%0' or beam_current like '%_0.01' or beam_current like '%_0.02' or beam_current like '%_0.03' or beam_current like '%_0.04' or beam_current like '%_0.05' or beam_current like '%_0.06')";

              System.out.println("Value of sql of ClosestToMultiplesOfTen_User is"+sql);
              stmt.executeQuery(sql);
              rs = stmt.getResultSet();
       while(rs.next()) 
        {
           for(int j=0; j<1; j++)
             {
               vals[i]  = rs.getDouble(2);
             }
            i++;
         }
        }
     catch( Exception e )
        {
            System.out.println("\nException "+e);
        }
    //  get the max value, and its multiple of ten to get the number of buckets
    double max = java.lang.Double.MIN_VALUE;
    for (double v : vals) max = Math.max(max, v);
    int bucketCount = 1 + (int)(max/10);
    double[] bucket =new double[bucketCount];

    //  initialise the buckets array to store the closest values
   double[][] buckets = new double[bucketCount][3];
 for (int i1 = 0; i1 < bucketCount; i1++){
        // store the current smallest delta in the first element
        buckets[i1][0] = java.lang.Double.MAX_VALUE; 
        // store the current "closest" index in the second element
        buckets[i1][1] = -1d;
        // store the current "closest" value in the third element
        buckets[i1][2] = java.lang.Double.MAX_VALUE;
    }

    //  iterate the rows
    for (row_id=1 ; row_id < vals.length; row_id++)
    {
        //  get the value from the row
        double v = vals[row_id];
        //  get the closest multiple of ten to v
        double mult = getMultipleOfTen(v);
        //  get the absolute distance of v from the multiple of ten
        double delta = Math.abs(mult - v);
        //  get the bucket index based on the value of `mult`
       bIdx = (int)(mult / 10d);
      // System.out.println("value of bidx for bucket index is"+bIdx);
        //    test the last known "smallest delta" for this bucket
        if (buckets[bIdx][0] > delta)
        {
         //  this is closer than the last known "smallest delta"
          buckets[bIdx][0] = delta;
          buckets[bIdx][1] = row_id;
          buckets[bIdx][2] = v;

        }
     }  
   //   print out the result
 for (int i1 =1; i1 <buckets.length; i1++)
   {
         bucket = buckets[i1];
         rowIndex = (int) bucket[1];
         int row_no=rowIndex+1;
         double rowValue = bucket[2];
         System.out.println("row index "+row_no+ "value is "+rowValue);
         DecimalFormat twoDForm = new DecimalFormat("#.##"); 

         rs.absolute(rowIndex);
         user_current_map.put(java.lang.Double.valueOf(twoDForm.format(rs.getDouble(2))),(rs.getString(1)));
        // map1.put(rs.getString(2),(rs.getString(1)));
         //l.add(map1);
     }
System.out.println("user_current_map "+user_current_map);

return user_current_map;
}

public static  double getMultipleOfTen(double v)
{
     System.out.println(10d * Math.round(v / 10d));
    return 10d * Math.round(v / 10d);
}

现在我只想颠倒顺序,即现在我想要 beam_current 的降序,即 210,22,190 等。

【问题讨论】:

  • 您想对数组中的值进行排序并将它们放入地图中吗?地图没有内在的秩序感。我不确定你想在这里做什么。
  • 我将使用链接哈希 Map。它按顺序存储值。
  • 啊。一个快速而肮脏的解决方案是改变您将值放入地图的顺序。从列表的后面而不是前面迭代,例如 for (int i = list.size(); i&gt;0; i--)
  • @47.5,当我申请 ** for (int i1 = buckets.length;i1>0; i1--) ** 在最后一个 for 循环的地方,然后我得到异常 java.lang.ArrayIndexOutOfBoundsException: 在第 ** 行 bucket = buckets[i1];**
  • 啊,哎呀,试试for (int i = list.size()-1; i&gt;=0; i--)

标签: java linkedhashmap


【解决方案1】:

要以相反的顺序表示它,请在您的 sql 查询中按时间排序并将存储桶的大小更改为

 for (double v : vals) max = Math.max(max, v);
Arrays.sort(vals);
System.out.println("value at vals[0] c "+vals[0]);
double min=vals[0];
int m2=(int) Math.round(min);
int m3=(int) Math.round(max);

**int bucketCount = 1+((m3-m2)/10);
double[] bucket =new double[bucketCount];
double[][] buckets = new double[bucketCount][3];**

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2014-04-17
    • 1970-01-01
    • 2018-11-02
    相关资源
    最近更新 更多