0

I have a method which stores the value from array into a linkedhashmap.Map stores key as 10,20 30 now I want to reverse the order of the storage of map i.e to 30,20..so on.Is there any function which can do so?? My code is-

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

    int row_id ;
    int bIdx = 0;

    //double[] vals = new double[47];
    double[] vals=null;
    int rowIndex = 0 ;
    int i=0;

    try
            { 
              con = getConnection();
              stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String sql="select distinct beam_current from INDUS2_BDS.dbo.DCCT where logtime between '"+start+"' and '"+end+"'"+
                      "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 FindClosestToMultiplesOfTen is"+sql);
              stmt.executeQuery(sql);
              rs = stmt.getResultSet();

             rs.last();
                int row_cnt=rs.getRow();
                System.out.println("row_count of closest " +row_cnt);
                 vals = new double[row_cnt];
                rs.beforeFirst();
       while(rs.next()) 
        {
           for(int j=0; j<1; j++)
             {
               vals[i]  = rs.getDouble(1);
               System.out.println("value of beam_current at closest is "+vals[i]);
             }
            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];

    System.out.println("bucketcount in closese"+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];
         double rowValue = bucket[2];
        DecimalFormat twoDForm = new DecimalFormat("#.##"); 
         System.out.println("row index closeset "+rowIndex+ "value is closest "+rowValue);
        rs.absolute(rowIndex+1);
         user_current_map.put(java.lang.Double.valueOf(twoDForm.format(rs.getDouble(1))),"");

     }
System.out.println("user_current_map "+user_current_map);

return user_current_map;
}

This user_current_map has to be reversed.How to do so.It is defined as

   **LinkedHashMap<Double,String> user_current_map = new LinkedHashMap<Double,String>();**
tiddi rastogi
  • 526
  • 3
  • 10
  • 33
  • You could use the key set of the map, sort it backwards and then iterate over it to use the keys in backward order. – Rene M. Mar 24 '15 at 12:12
  • Please clarify your requirements: do you assume/expect that your keys could be sorted; and you want to process the reverse of the sorted keys? Or do you want to ensure that you reverse the order in which the keys were added? – GhostCat Mar 24 '15 at 12:16
  • Just like @EddyG said....clarify whether you want the insertion order to be reversed or the natural sorting order. – ha9u63a7 Mar 24 '15 at 12:19
  • Possibly similar to this one ? - http://stackoverflow.com/questions/20412354/reverse-hashmap-keys-and-values-in-java – ha9u63a7 Mar 24 '15 at 12:20
  • I don't get it. You iterate the result set from last element to first, make some calculations with every value, put this calculated value in the `LinkedHashMap` and then want the keys reversed. Why don't you iterate starting from the first element of the result set? Disclaimer: it's very likely that I've got lost somewhere while reading your code. – fps Mar 24 '15 at 13:41

1 Answers1

1

If you create a LinkedHashMap ordered by access order (new LinkedHashMap(initialCapacity,loadFactor,true)), iterating over the keys of the map and calling get for each key would reverse the order, since each time you would call get(key), that key would be moved to the head of the entries linked list.

Therefore, this loop will reverse the order of the keys :

LinkedHashMap<Double,String> user_current_map = new LinkedHashMap<Double,String>(16,0.75f,true);
....
for (Double key : user_current_map.keySet()) {
    String value = map.get(key);
}

EDIT : actually this doesn't work. It throws ConcurrentModificationException, since you can't modify the Map while iterating over it.

Here's something that does seem to work :

LinkedHashMap<Double,String> map = new LinkedHashMap<Double, String> (16,0.75f,true);
map.put (0.5, "a");
map.put (0.6, "b");
map.put (0.7, "c");
Set<Double> keys = new HashSet(map.keySet ());
System.out.println();
for (Double d : keys ) {
  String v = map.get (d);
  System.out.println (d+","+v);
}
keys = new HashSet(map.keySet ());
System.out.println();
for (Double d : keys) {
  String v = map.get (d);
  System.out.println (d+","+v);
}
keys = new HashSet(map.keySet ());
System.out.println();
for (Double d : keys) {
  String v = map.get (d);
  System.out.println (d+","+v);
}

Output :

0.7,c
0.6,b
0.5,a

0.5,a
0.6,b
0.7,c

0.7,c
0.6,b
0.5,a
Eran
  • 387,369
  • 54
  • 702
  • 768