• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Match the most closest number

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a list of number, types is Double. I have a method to pass in the Double as params. I want to compare the input params with each of the values in the collection, if it matches the most closest value then return that value from the collection.

Collections<Double> list = new ArrayList<Double>();
list.add(89000.00);
list.add(78900.00);
list.add(56999.12);

so the method is like this

private Double getTheMatch(Double param) {
pseudo code:
for (Double d : list) {
if (param Match list.getNumber) {
return list.getNumber;
}
}
}

so if passing 88908.00 --> value will return will be 89000.00 , the most match value from the list

What API java class should I use? Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a NavigableSet for this. Class TreeSet is an implementation of class NavigableSet. Example:

Note that the method ceiling returns the least element in the set greater than or equal to the given element (or null if there is no such element). There are other methods in the interface, such as floor, higher and lower. You'll have to read the documentation and decide which of these is appropriate for your application.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic