2

Given the following code:

     for (int i = 0; i < noRooms; i++) {
        String roomPrice = response.xmlPath().getString("ABC_AvailRS.AccommodationSearchResponse.Accommodations.AccommodationSegment[0].AccommodationUnits.AccommodationUnit[" + i + "].RoomRate.@Amount");
        roomPrices.add(roomPrice);
        List<Float> floatRoomPrices = roomPrices.stream()
                .map(Float::parseFloat)
                .collect(Collectors.toList());

        float min = floatRoomPrices.stream().min(Float::compare).get();
        float max = floatRoomPrices.stream().max(Float::compare).get();

    }

...in which I can grab a list of amounts as Strings Amount="92.65", convert them to floats and add to a List, I can clearly find the highest and lowest amount.

  1. how can I get the mid point value of floatRoomPrices?
  2. how can I find it's position within the List e.g. [4] if it was the mid range of 10 float values?
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Steerpike
  • 1,712
  • 6
  • 38
  • 71
  • Sort the list. Then you know exactly where your min, max, and mid are. – Dewick47 Mar 16 '18 at 15:52
  • usually a midpoint in array or list of length n is located at `n/2`. so just sort your floating like `floatRoomPrices.sort(Float::compare)`; and then `midPoint = floatRoomPrices.get(floatRoomPrices.size()/2)`; – arthur Mar 16 '18 at 15:56
  • 1
    Why are you repeating that operation in every loop iteration? – Holger Mar 16 '18 at 17:13

1 Answers1

1

You can quickly find this information with a sorted list. Here's a simple example.

    public static void main(String[] args) {
         List<Float> floats = new ArrayList<>();
         floats.add(7.85f);
         floats.add(0.85f);
         floats.add(9.01f);
         floats.add(2.78f);
         Collections.sort(floats); // in ascending order, adjust sorting method as preferred - maybe you can utilize stream().sorted

         float min = floats.get(0);
         float max = floats.get(floats.size() - 1);
         int midIndex = floats.size() / 2; // Answer to #2
         float midValue = floats.get(midIndex); // Answer to #1

         System.out.println("Min: " + min + "\nMax: " + max + "\nMid index: " + midIndex + "\nMid value: " + midValue);
    }

This gives the output:

Min: 0.85
Max: 9.01
Mid index: 2
Mid value: 7.85

Dewick47
  • 171
  • 11
  • Thanks, that's great - but how do I also get its index value within the collection? – Steerpike Mar 16 '18 at 16:22
  • Mid index is the index value. [0.85, 2.78, 7.85, 9.01] -> 2 is the index where the mid value sits. If this isn't what you're looking for, can you rephrase? – Dewick47 Mar 16 '18 at 16:28
  • sorry, I was being stupid and rushing - that answers perfectly - thank you – Steerpike Mar 16 '18 at 16:29
  • 2
    [When to use LinkedList over ArrayList?](https://stackoverflow.com/q/322715/2711488) (Spoiler: almost never). Though, in this specific use case, there is no reason for a `List` at all. Use `double[] doubleRoomPrices = roomPrices.stream() .mapToDouble(Double::parseDouble).toArray();`; you may cast the final result values to `float`, if you wish. Performing the calculation in `float` has no advantage, sometimes it’s even slower. In case of boxed `Float`, it’s slower most of the time… – Holger Mar 16 '18 at 17:17
  • @Holger Not sure why I used a LinkedList. Time for a coffee break apparently. Thank you for pointing that out. – Dewick47 Mar 16 '18 at 17:49
  • @Holger shipilev once said - who said LinkedList is bad? It's the best thing ever to test your GC implementation (he was referring to shenandoah) – Eugene Mar 16 '18 at 20:23