Home > Back-end >  Why Java defaults to not include last value of a given range?
Why Java defaults to not include last value of a given range?

Time:07-23

I am going through official TreeMap documentation. I see subMap() prototype as:

public SortedMap<K,V> subMap(K fromKey, K toKey) is equivalent to subMap(fromKey, true, toKey, false).

I have seen everywhere, Java does not include the last value of a given range by default? Why was this decision made that default inclusion value for toKey will be false (and not true)?

CodePudding user response:

in java almost every method to get a sub part of a data structure it goes form the fromkey (included) to the tokey (not included) so what you have to do is do tokey 1 instead of just tokey. As for example, if we do map.subMap(a,b); we will get the values from indexes a to b-1. Hope this helps.

CodePudding user response:

this is general in almost all programming languages I guess! like I see in 'python' and 'C'.

This is kind of a rule of sets(collections) in mathematics. you can imagine this as below:

fromKey <= x < toKey

eg:

0 <= x < 10

anyway, if you want to include toKey itself in any range, you should do:

toKey   1

or

toKey   

CodePudding user response:

Indices in most programming languages start at 0. Thus, the last element of any linear collection is size - 1.

Now, any range is defined as start (inclusive) - end (exclusive), since it makes the code more readable.

Example

Let's say you have "Java" and you want to split it at the "v".

With this definition, it is very easy:

String java = "Java"; // length: 4
int index = java.indexOf("v"); // 2

String start = java.substring(0, index); // from 0 (inclusive) to 2 (exclusive)
String end = java.substring(index); // from 2 (inclusive) to length (exclusive, implicit)

System.out.println(start   ", "   end); // Ja, va

CodePudding user response:

This has been "answered" for String#substring here:

https://stackoverflow.com/a/26631968/1571268

The question about the "Why" may be considered as philosophical or academic, and provoke answers along the line of "That's just the way it is".

[...]

However, regardless of which choice is made, and regardless how it is justified: It is important to be consistent throughout the whole API.

CodePudding user response:

If you are going to specify ranges by specifying end-points it's generally best to specify the range as B<=x<E. At the very least it means you can specify the empty range by B=E.

But when it comes to 'infinite' value spaces it becomes essential.

Suppose I want to specify every string beginning with C? In the 'end exclusive' model that's B="C" and E="D". In the 'end inclusive' model that's B="C" and E="CZZZZZZZZZZZZZZ..." where E is some string we've decided is longer than any string we're going to practically encounter.

It also makes it practical to define non-overlapping coverages as [B,E),[E,F) and so on.

NB: Mathematical convention is that [ indicates >= and ) indicates <

Some people argue it's a matter of taste. But in practice you can create a partition from ordered values A,B,C,D,E... as [A,B),[B,C),[C,D) without any fiddling around trying to identify (the potentially non-existent or unconstructable) value immediately before B.

It's somewhere between messy and impossible to construct the partition [A,B-e1],[B,C-e2]. Just duck the issue and use half-inclusive intervals! It normally makes sense to use inclusive-exclusive intervals but the approach works the other way.

  • Related