__goku__'s blog

By __goku__, history, 4 years ago, In English

I have written an article on how to implement lower_bound() function of C++ in Java by creating our own method and also using java inbuilt functions. Feel free to comment if you have any query.

Link: https://medium.com/@ssrivastava990/alternative-implementation-of-lower-bound-in-java-23ec40a825ec

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +15 Vote: I do not like it

This all seems rather unnecessary to me. You already have treeset.higher(), .lower(), .floor(), and .ceil(). In my experience (as a java main), no one seriously wants to convert from C++ to Java. Even if they did, the names of the methods in Java are super self-explanatory. i. e. "I want the first thing higher than x" "Okay, then use .higher(x)".

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    He talks about arrays, not sets. Arrays.binarySearch() is stupid returning random occurence, and you have to use pairs (value, index) to find first/last occurence or write it manually

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

If I were a Java user, I would use generic types so that any that the methods work for any class that implements the Comparable interface. Also I would make it return the end of the range if the search fails to be consistent with C++. Also in my experience from the Kotlin round, ArrayList is much slower than int[], so it would be worth it to implement the methods separately for each primitive type or find some way to convert a primitive array to an ArrayList in O(1).