Generate ranges in java












-2














I am trying to generate ranges from input batchSize and batchInterval. But, I am missing some batches due to the logic issues.



I am getting a batch of records from a SQL DB. Due to the huge latency in data transfer the downstream services are having issues. Therefore, I would like to batch query the data.



select * from table WHERE RN > ? AND RN <= ?


The two questions marks are lower and upper bound in the ranges



public class Ranges {

public static void main(String args) {
int batchTotal= 605;
int batchInterval = 100;

System.out.println(generateBatches(batchTotal, batchInterval).toString());

batchTotal = 605;
batchInterval = 5;

System.out.println(generateBatches(batchTotal, batchInterval).toString());
}

public static Set<String> generateBatches(int batchTotal, int batchInterval) {
int rangeLower = 0;
int rangeUpper = 0;
Set<String> ranges = new TreeSet<String>();
int numberOfIterableBatches = 0;
if (batchTotal < batchInterval) {
ranges.add(rangeLower + " - " + batchInterval);
} else {
numberOfIterableBatches = (int) java.lang.Math.ceil(batchTotal / batchInterval);
System.out.println(numberOfIterableBatches);
for (int i = 0; i <= numberOfIterableBatches; i++) {
if (i == 0) {
rangeLower = 0;
rangeUpper = i + 1;
} else {
rangeLower = i - 1;
rangeUpper = i;
}
ranges.add(rangeLower + " - " + rangeUpper);
}
}

return ranges;
}

}


When I am using List<String> I am getting duplicates. Therefore I am using Set<String>. Here is the sample output



6
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6]
121
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6, 6 - 7, 7 - 8, 8 - 9, 9 - 10, 10 - 11, 11 - 12, 12 - 13, 13 - 14, 14 - 15, 15 - 16, 16 - 17, 17 - 18, 18 - 19, 19 - 20, 20 - 21, 21 - 22, 22 - 23, 23 - 24, 24 - 25, 25 - 26, 26 - 27, 27 - 28, 28 - 29, 29 - 30, 30 - 31, 31 - 32, 32 - 33, 33 - 34, 34 - 35, 35 - 36, 36 - 37, 37 - 38, 38 - 39, 39 - 40, 40 - 41, 41 - 42, 42 - 43, 43 - 44, 44 - 45, 45 - 46, 46 - 47, 47 - 48, 48 - 49, 49 - 50, 50 - 51, 51 - 52, 52 - 53, 53 - 54, 54 - 55, 55 - 56, 56 - 57, 57 - 58, 58 - 59, 59 - 60, 60 - 61, 61 - 62, 62 - 63, 63 - 64, 64 - 65, 65 - 66, 66 - 67, 67 - 68, 68 - 69, 69 - 70, 70 - 71, 71 - 72, 72 - 73, 73 - 74, 74 - 75, 75 - 76, 76 - 77, 77 - 78, 78 - 79, 79 - 80, 80 - 81, 81 - 82, 82 - 83, 83 - 84, 84 - 85, 85 - 86, 86 - 87, 87 - 88, 88 - 89, 89 - 90, 90 - 91, 91 - 92, 92 - 93, 93 - 94, 94 - 95, 95 - 96, 96 - 97, 97 - 98, 98 - 99, 99 - 100, 100 - 101, 101 - 102, 102 - 103, 103 - 104, 104 - 105, 105 - 106, 106 - 107, 107 - 108, 108 - 109, 109 - 110, 110 - 111, 111 - 112, 112 - 113, 113 - 114, 114 - 115, 115 - 116, 116 - 117, 117 - 118, 118 - 119, 119 - 120, 120 - 121]









share|improve this question




















  • 3




    What's the output you're getting? What's the output you were trying to achieve?
    – Mark
    Nov 14 '18 at 14:06






  • 1




    Not related to your issue but you should rename your variables. Both main method and generateBatches has a batchSize argument but they don't match. That's quite misleading.
    – jhamon
    Nov 14 '18 at 14:11












  • Much better. But then: you better drop that "is there any library" part, because that renders the question off topic. And I am still not sure what you consider "wrong" about the code snippet you provided.
    – GhostCat
    Nov 14 '18 at 15:23










  • @GhostCat If my BatchSize is 506 or 1005 i am missing some records thats my problem.
    – wandermonk
    Nov 14 '18 at 16:06










  • In Postgres you can use LIMIT and OFFSET (see postgresql.org/docs/11/queries-limit.html), does your database have something similar?
    – Robert
    Nov 14 '18 at 16:51
















-2














I am trying to generate ranges from input batchSize and batchInterval. But, I am missing some batches due to the logic issues.



I am getting a batch of records from a SQL DB. Due to the huge latency in data transfer the downstream services are having issues. Therefore, I would like to batch query the data.



select * from table WHERE RN > ? AND RN <= ?


The two questions marks are lower and upper bound in the ranges



public class Ranges {

public static void main(String args) {
int batchTotal= 605;
int batchInterval = 100;

System.out.println(generateBatches(batchTotal, batchInterval).toString());

batchTotal = 605;
batchInterval = 5;

System.out.println(generateBatches(batchTotal, batchInterval).toString());
}

public static Set<String> generateBatches(int batchTotal, int batchInterval) {
int rangeLower = 0;
int rangeUpper = 0;
Set<String> ranges = new TreeSet<String>();
int numberOfIterableBatches = 0;
if (batchTotal < batchInterval) {
ranges.add(rangeLower + " - " + batchInterval);
} else {
numberOfIterableBatches = (int) java.lang.Math.ceil(batchTotal / batchInterval);
System.out.println(numberOfIterableBatches);
for (int i = 0; i <= numberOfIterableBatches; i++) {
if (i == 0) {
rangeLower = 0;
rangeUpper = i + 1;
} else {
rangeLower = i - 1;
rangeUpper = i;
}
ranges.add(rangeLower + " - " + rangeUpper);
}
}

return ranges;
}

}


When I am using List<String> I am getting duplicates. Therefore I am using Set<String>. Here is the sample output



6
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6]
121
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6, 6 - 7, 7 - 8, 8 - 9, 9 - 10, 10 - 11, 11 - 12, 12 - 13, 13 - 14, 14 - 15, 15 - 16, 16 - 17, 17 - 18, 18 - 19, 19 - 20, 20 - 21, 21 - 22, 22 - 23, 23 - 24, 24 - 25, 25 - 26, 26 - 27, 27 - 28, 28 - 29, 29 - 30, 30 - 31, 31 - 32, 32 - 33, 33 - 34, 34 - 35, 35 - 36, 36 - 37, 37 - 38, 38 - 39, 39 - 40, 40 - 41, 41 - 42, 42 - 43, 43 - 44, 44 - 45, 45 - 46, 46 - 47, 47 - 48, 48 - 49, 49 - 50, 50 - 51, 51 - 52, 52 - 53, 53 - 54, 54 - 55, 55 - 56, 56 - 57, 57 - 58, 58 - 59, 59 - 60, 60 - 61, 61 - 62, 62 - 63, 63 - 64, 64 - 65, 65 - 66, 66 - 67, 67 - 68, 68 - 69, 69 - 70, 70 - 71, 71 - 72, 72 - 73, 73 - 74, 74 - 75, 75 - 76, 76 - 77, 77 - 78, 78 - 79, 79 - 80, 80 - 81, 81 - 82, 82 - 83, 83 - 84, 84 - 85, 85 - 86, 86 - 87, 87 - 88, 88 - 89, 89 - 90, 90 - 91, 91 - 92, 92 - 93, 93 - 94, 94 - 95, 95 - 96, 96 - 97, 97 - 98, 98 - 99, 99 - 100, 100 - 101, 101 - 102, 102 - 103, 103 - 104, 104 - 105, 105 - 106, 106 - 107, 107 - 108, 108 - 109, 109 - 110, 110 - 111, 111 - 112, 112 - 113, 113 - 114, 114 - 115, 115 - 116, 116 - 117, 117 - 118, 118 - 119, 119 - 120, 120 - 121]









share|improve this question




















  • 3




    What's the output you're getting? What's the output you were trying to achieve?
    – Mark
    Nov 14 '18 at 14:06






  • 1




    Not related to your issue but you should rename your variables. Both main method and generateBatches has a batchSize argument but they don't match. That's quite misleading.
    – jhamon
    Nov 14 '18 at 14:11












  • Much better. But then: you better drop that "is there any library" part, because that renders the question off topic. And I am still not sure what you consider "wrong" about the code snippet you provided.
    – GhostCat
    Nov 14 '18 at 15:23










  • @GhostCat If my BatchSize is 506 or 1005 i am missing some records thats my problem.
    – wandermonk
    Nov 14 '18 at 16:06










  • In Postgres you can use LIMIT and OFFSET (see postgresql.org/docs/11/queries-limit.html), does your database have something similar?
    – Robert
    Nov 14 '18 at 16:51














-2












-2








-2







I am trying to generate ranges from input batchSize and batchInterval. But, I am missing some batches due to the logic issues.



I am getting a batch of records from a SQL DB. Due to the huge latency in data transfer the downstream services are having issues. Therefore, I would like to batch query the data.



select * from table WHERE RN > ? AND RN <= ?


The two questions marks are lower and upper bound in the ranges



public class Ranges {

public static void main(String args) {
int batchTotal= 605;
int batchInterval = 100;

System.out.println(generateBatches(batchTotal, batchInterval).toString());

batchTotal = 605;
batchInterval = 5;

System.out.println(generateBatches(batchTotal, batchInterval).toString());
}

public static Set<String> generateBatches(int batchTotal, int batchInterval) {
int rangeLower = 0;
int rangeUpper = 0;
Set<String> ranges = new TreeSet<String>();
int numberOfIterableBatches = 0;
if (batchTotal < batchInterval) {
ranges.add(rangeLower + " - " + batchInterval);
} else {
numberOfIterableBatches = (int) java.lang.Math.ceil(batchTotal / batchInterval);
System.out.println(numberOfIterableBatches);
for (int i = 0; i <= numberOfIterableBatches; i++) {
if (i == 0) {
rangeLower = 0;
rangeUpper = i + 1;
} else {
rangeLower = i - 1;
rangeUpper = i;
}
ranges.add(rangeLower + " - " + rangeUpper);
}
}

return ranges;
}

}


When I am using List<String> I am getting duplicates. Therefore I am using Set<String>. Here is the sample output



6
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6]
121
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6, 6 - 7, 7 - 8, 8 - 9, 9 - 10, 10 - 11, 11 - 12, 12 - 13, 13 - 14, 14 - 15, 15 - 16, 16 - 17, 17 - 18, 18 - 19, 19 - 20, 20 - 21, 21 - 22, 22 - 23, 23 - 24, 24 - 25, 25 - 26, 26 - 27, 27 - 28, 28 - 29, 29 - 30, 30 - 31, 31 - 32, 32 - 33, 33 - 34, 34 - 35, 35 - 36, 36 - 37, 37 - 38, 38 - 39, 39 - 40, 40 - 41, 41 - 42, 42 - 43, 43 - 44, 44 - 45, 45 - 46, 46 - 47, 47 - 48, 48 - 49, 49 - 50, 50 - 51, 51 - 52, 52 - 53, 53 - 54, 54 - 55, 55 - 56, 56 - 57, 57 - 58, 58 - 59, 59 - 60, 60 - 61, 61 - 62, 62 - 63, 63 - 64, 64 - 65, 65 - 66, 66 - 67, 67 - 68, 68 - 69, 69 - 70, 70 - 71, 71 - 72, 72 - 73, 73 - 74, 74 - 75, 75 - 76, 76 - 77, 77 - 78, 78 - 79, 79 - 80, 80 - 81, 81 - 82, 82 - 83, 83 - 84, 84 - 85, 85 - 86, 86 - 87, 87 - 88, 88 - 89, 89 - 90, 90 - 91, 91 - 92, 92 - 93, 93 - 94, 94 - 95, 95 - 96, 96 - 97, 97 - 98, 98 - 99, 99 - 100, 100 - 101, 101 - 102, 102 - 103, 103 - 104, 104 - 105, 105 - 106, 106 - 107, 107 - 108, 108 - 109, 109 - 110, 110 - 111, 111 - 112, 112 - 113, 113 - 114, 114 - 115, 115 - 116, 116 - 117, 117 - 118, 118 - 119, 119 - 120, 120 - 121]









share|improve this question















I am trying to generate ranges from input batchSize and batchInterval. But, I am missing some batches due to the logic issues.



I am getting a batch of records from a SQL DB. Due to the huge latency in data transfer the downstream services are having issues. Therefore, I would like to batch query the data.



select * from table WHERE RN > ? AND RN <= ?


The two questions marks are lower and upper bound in the ranges



public class Ranges {

public static void main(String args) {
int batchTotal= 605;
int batchInterval = 100;

System.out.println(generateBatches(batchTotal, batchInterval).toString());

batchTotal = 605;
batchInterval = 5;

System.out.println(generateBatches(batchTotal, batchInterval).toString());
}

public static Set<String> generateBatches(int batchTotal, int batchInterval) {
int rangeLower = 0;
int rangeUpper = 0;
Set<String> ranges = new TreeSet<String>();
int numberOfIterableBatches = 0;
if (batchTotal < batchInterval) {
ranges.add(rangeLower + " - " + batchInterval);
} else {
numberOfIterableBatches = (int) java.lang.Math.ceil(batchTotal / batchInterval);
System.out.println(numberOfIterableBatches);
for (int i = 0; i <= numberOfIterableBatches; i++) {
if (i == 0) {
rangeLower = 0;
rangeUpper = i + 1;
} else {
rangeLower = i - 1;
rangeUpper = i;
}
ranges.add(rangeLower + " - " + rangeUpper);
}
}

return ranges;
}

}


When I am using List<String> I am getting duplicates. Therefore I am using Set<String>. Here is the sample output



6
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6]
121
[0 - 1, 0 - 1, 1 - 2, 2 - 3, 3 - 4, 4 - 5, 5 - 6, 6 - 7, 7 - 8, 8 - 9, 9 - 10, 10 - 11, 11 - 12, 12 - 13, 13 - 14, 14 - 15, 15 - 16, 16 - 17, 17 - 18, 18 - 19, 19 - 20, 20 - 21, 21 - 22, 22 - 23, 23 - 24, 24 - 25, 25 - 26, 26 - 27, 27 - 28, 28 - 29, 29 - 30, 30 - 31, 31 - 32, 32 - 33, 33 - 34, 34 - 35, 35 - 36, 36 - 37, 37 - 38, 38 - 39, 39 - 40, 40 - 41, 41 - 42, 42 - 43, 43 - 44, 44 - 45, 45 - 46, 46 - 47, 47 - 48, 48 - 49, 49 - 50, 50 - 51, 51 - 52, 52 - 53, 53 - 54, 54 - 55, 55 - 56, 56 - 57, 57 - 58, 58 - 59, 59 - 60, 60 - 61, 61 - 62, 62 - 63, 63 - 64, 64 - 65, 65 - 66, 66 - 67, 67 - 68, 68 - 69, 69 - 70, 70 - 71, 71 - 72, 72 - 73, 73 - 74, 74 - 75, 75 - 76, 76 - 77, 77 - 78, 78 - 79, 79 - 80, 80 - 81, 81 - 82, 82 - 83, 83 - 84, 84 - 85, 85 - 86, 86 - 87, 87 - 88, 88 - 89, 89 - 90, 90 - 91, 91 - 92, 92 - 93, 93 - 94, 94 - 95, 95 - 96, 96 - 97, 97 - 98, 98 - 99, 99 - 100, 100 - 101, 101 - 102, 102 - 103, 103 - 104, 104 - 105, 105 - 106, 106 - 107, 107 - 108, 108 - 109, 109 - 110, 110 - 111, 111 - 112, 112 - 113, 113 - 114, 114 - 115, 115 - 116, 116 - 117, 117 - 118, 118 - 119, 119 - 120, 120 - 121]






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:57

























asked Nov 14 '18 at 14:02









wandermonk

1,45021239




1,45021239








  • 3




    What's the output you're getting? What's the output you were trying to achieve?
    – Mark
    Nov 14 '18 at 14:06






  • 1




    Not related to your issue but you should rename your variables. Both main method and generateBatches has a batchSize argument but they don't match. That's quite misleading.
    – jhamon
    Nov 14 '18 at 14:11












  • Much better. But then: you better drop that "is there any library" part, because that renders the question off topic. And I am still not sure what you consider "wrong" about the code snippet you provided.
    – GhostCat
    Nov 14 '18 at 15:23










  • @GhostCat If my BatchSize is 506 or 1005 i am missing some records thats my problem.
    – wandermonk
    Nov 14 '18 at 16:06










  • In Postgres you can use LIMIT and OFFSET (see postgresql.org/docs/11/queries-limit.html), does your database have something similar?
    – Robert
    Nov 14 '18 at 16:51














  • 3




    What's the output you're getting? What's the output you were trying to achieve?
    – Mark
    Nov 14 '18 at 14:06






  • 1




    Not related to your issue but you should rename your variables. Both main method and generateBatches has a batchSize argument but they don't match. That's quite misleading.
    – jhamon
    Nov 14 '18 at 14:11












  • Much better. But then: you better drop that "is there any library" part, because that renders the question off topic. And I am still not sure what you consider "wrong" about the code snippet you provided.
    – GhostCat
    Nov 14 '18 at 15:23










  • @GhostCat If my BatchSize is 506 or 1005 i am missing some records thats my problem.
    – wandermonk
    Nov 14 '18 at 16:06










  • In Postgres you can use LIMIT and OFFSET (see postgresql.org/docs/11/queries-limit.html), does your database have something similar?
    – Robert
    Nov 14 '18 at 16:51








3




3




What's the output you're getting? What's the output you were trying to achieve?
– Mark
Nov 14 '18 at 14:06




What's the output you're getting? What's the output you were trying to achieve?
– Mark
Nov 14 '18 at 14:06




1




1




Not related to your issue but you should rename your variables. Both main method and generateBatches has a batchSize argument but they don't match. That's quite misleading.
– jhamon
Nov 14 '18 at 14:11






Not related to your issue but you should rename your variables. Both main method and generateBatches has a batchSize argument but they don't match. That's quite misleading.
– jhamon
Nov 14 '18 at 14:11














Much better. But then: you better drop that "is there any library" part, because that renders the question off topic. And I am still not sure what you consider "wrong" about the code snippet you provided.
– GhostCat
Nov 14 '18 at 15:23




Much better. But then: you better drop that "is there any library" part, because that renders the question off topic. And I am still not sure what you consider "wrong" about the code snippet you provided.
– GhostCat
Nov 14 '18 at 15:23












@GhostCat If my BatchSize is 506 or 1005 i am missing some records thats my problem.
– wandermonk
Nov 14 '18 at 16:06




@GhostCat If my BatchSize is 506 or 1005 i am missing some records thats my problem.
– wandermonk
Nov 14 '18 at 16:06












In Postgres you can use LIMIT and OFFSET (see postgresql.org/docs/11/queries-limit.html), does your database have something similar?
– Robert
Nov 14 '18 at 16:51




In Postgres you can use LIMIT and OFFSET (see postgresql.org/docs/11/queries-limit.html), does your database have something similar?
– Robert
Nov 14 '18 at 16:51












2 Answers
2






active

oldest

votes


















0














The code is unnecessarily complicated or I misunderstood the question :)



public class Ranges {

public static void main(String args) {
int batchTotal = 605;
int batchInterval = 100;

System.out.println(generateBatches(batchTotal, batchInterval).toString());

batchTotal = 605;
batchInterval = 5;

System.out.println(generateBatches(batchTotal, batchInterval).toString());
}

public static List<String> generateBatches(int batchTotal, int batchInterval) {
List<String> ranges = new ArrayList<String>();
int n = batchTotal;
int rangeStart = 0;
while (n > 0) {
ranges.add(rangeStart + "-" + ((rangeStart + batchInterval) > batchTotal ? batchTotal : rangeStart + batchInterval));
rangeStart += batchInterval;
n -= batchInterval;
}
return ranges;
}

}





share|improve this answer





















  • You have a point!! I made it very complex. Thanks for making it so simple.
    – wandermonk
    Nov 14 '18 at 18:13



















0














Take a look at Iterables in the guava library:



public static <T> Iterable<List<T>> partition(Iterable<T> iterable, int size)



You could use it like this:



int outerLowerBound = 0;    
int outerUpperBound = 605;
int numberOfParts = 5;

List<Integer> collect = IntStream.range(outerLowerBound, outerUpperBound)
.boxed().collect(Collectors.toList());
Iterable<List<Integer>> partitions = Iterables.partition(collect, numberOfParts);


Inside the partitions list are your intervals. Then for each interval; 1st item is lower bound and last item is upper bound.



for (List<Integer> partition : partitions) {
System.out.println("[ " + Iterables.getFirst(partition, 0) + ", " + Iterables.getLast(partition) + " ]");
}


Output:



[ 1, 5 ]
[ 6, 10 ]
[ 11, 15 ]
[ 16, 20 ]
[ 21, 25 ]
[ 26, 30 ]
[ 31, 35 ]
...
[ 581, 585 ]
[ 586, 590 ]
[ 591, 595 ]
[ 596, 600 ]
[ 601, 604 ]


See doc:



https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html#partition-java.lang.Iterable-int-






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302032%2fgenerate-ranges-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The code is unnecessarily complicated or I misunderstood the question :)



    public class Ranges {

    public static void main(String args) {
    int batchTotal = 605;
    int batchInterval = 100;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());

    batchTotal = 605;
    batchInterval = 5;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());
    }

    public static List<String> generateBatches(int batchTotal, int batchInterval) {
    List<String> ranges = new ArrayList<String>();
    int n = batchTotal;
    int rangeStart = 0;
    while (n > 0) {
    ranges.add(rangeStart + "-" + ((rangeStart + batchInterval) > batchTotal ? batchTotal : rangeStart + batchInterval));
    rangeStart += batchInterval;
    n -= batchInterval;
    }
    return ranges;
    }

    }





    share|improve this answer





















    • You have a point!! I made it very complex. Thanks for making it so simple.
      – wandermonk
      Nov 14 '18 at 18:13
















    0














    The code is unnecessarily complicated or I misunderstood the question :)



    public class Ranges {

    public static void main(String args) {
    int batchTotal = 605;
    int batchInterval = 100;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());

    batchTotal = 605;
    batchInterval = 5;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());
    }

    public static List<String> generateBatches(int batchTotal, int batchInterval) {
    List<String> ranges = new ArrayList<String>();
    int n = batchTotal;
    int rangeStart = 0;
    while (n > 0) {
    ranges.add(rangeStart + "-" + ((rangeStart + batchInterval) > batchTotal ? batchTotal : rangeStart + batchInterval));
    rangeStart += batchInterval;
    n -= batchInterval;
    }
    return ranges;
    }

    }





    share|improve this answer





















    • You have a point!! I made it very complex. Thanks for making it so simple.
      – wandermonk
      Nov 14 '18 at 18:13














    0












    0








    0






    The code is unnecessarily complicated or I misunderstood the question :)



    public class Ranges {

    public static void main(String args) {
    int batchTotal = 605;
    int batchInterval = 100;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());

    batchTotal = 605;
    batchInterval = 5;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());
    }

    public static List<String> generateBatches(int batchTotal, int batchInterval) {
    List<String> ranges = new ArrayList<String>();
    int n = batchTotal;
    int rangeStart = 0;
    while (n > 0) {
    ranges.add(rangeStart + "-" + ((rangeStart + batchInterval) > batchTotal ? batchTotal : rangeStart + batchInterval));
    rangeStart += batchInterval;
    n -= batchInterval;
    }
    return ranges;
    }

    }





    share|improve this answer












    The code is unnecessarily complicated or I misunderstood the question :)



    public class Ranges {

    public static void main(String args) {
    int batchTotal = 605;
    int batchInterval = 100;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());

    batchTotal = 605;
    batchInterval = 5;

    System.out.println(generateBatches(batchTotal, batchInterval).toString());
    }

    public static List<String> generateBatches(int batchTotal, int batchInterval) {
    List<String> ranges = new ArrayList<String>();
    int n = batchTotal;
    int rangeStart = 0;
    while (n > 0) {
    ranges.add(rangeStart + "-" + ((rangeStart + batchInterval) > batchTotal ? batchTotal : rangeStart + batchInterval));
    rangeStart += batchInterval;
    n -= batchInterval;
    }
    return ranges;
    }

    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 17:48









    Dakshinamurthy Karra

    4,12111021




    4,12111021












    • You have a point!! I made it very complex. Thanks for making it so simple.
      – wandermonk
      Nov 14 '18 at 18:13


















    • You have a point!! I made it very complex. Thanks for making it so simple.
      – wandermonk
      Nov 14 '18 at 18:13
















    You have a point!! I made it very complex. Thanks for making it so simple.
    – wandermonk
    Nov 14 '18 at 18:13




    You have a point!! I made it very complex. Thanks for making it so simple.
    – wandermonk
    Nov 14 '18 at 18:13













    0














    Take a look at Iterables in the guava library:



    public static <T> Iterable<List<T>> partition(Iterable<T> iterable, int size)



    You could use it like this:



    int outerLowerBound = 0;    
    int outerUpperBound = 605;
    int numberOfParts = 5;

    List<Integer> collect = IntStream.range(outerLowerBound, outerUpperBound)
    .boxed().collect(Collectors.toList());
    Iterable<List<Integer>> partitions = Iterables.partition(collect, numberOfParts);


    Inside the partitions list are your intervals. Then for each interval; 1st item is lower bound and last item is upper bound.



    for (List<Integer> partition : partitions) {
    System.out.println("[ " + Iterables.getFirst(partition, 0) + ", " + Iterables.getLast(partition) + " ]");
    }


    Output:



    [ 1, 5 ]
    [ 6, 10 ]
    [ 11, 15 ]
    [ 16, 20 ]
    [ 21, 25 ]
    [ 26, 30 ]
    [ 31, 35 ]
    ...
    [ 581, 585 ]
    [ 586, 590 ]
    [ 591, 595 ]
    [ 596, 600 ]
    [ 601, 604 ]


    See doc:



    https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html#partition-java.lang.Iterable-int-






    share|improve this answer




























      0














      Take a look at Iterables in the guava library:



      public static <T> Iterable<List<T>> partition(Iterable<T> iterable, int size)



      You could use it like this:



      int outerLowerBound = 0;    
      int outerUpperBound = 605;
      int numberOfParts = 5;

      List<Integer> collect = IntStream.range(outerLowerBound, outerUpperBound)
      .boxed().collect(Collectors.toList());
      Iterable<List<Integer>> partitions = Iterables.partition(collect, numberOfParts);


      Inside the partitions list are your intervals. Then for each interval; 1st item is lower bound and last item is upper bound.



      for (List<Integer> partition : partitions) {
      System.out.println("[ " + Iterables.getFirst(partition, 0) + ", " + Iterables.getLast(partition) + " ]");
      }


      Output:



      [ 1, 5 ]
      [ 6, 10 ]
      [ 11, 15 ]
      [ 16, 20 ]
      [ 21, 25 ]
      [ 26, 30 ]
      [ 31, 35 ]
      ...
      [ 581, 585 ]
      [ 586, 590 ]
      [ 591, 595 ]
      [ 596, 600 ]
      [ 601, 604 ]


      See doc:



      https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html#partition-java.lang.Iterable-int-






      share|improve this answer


























        0












        0








        0






        Take a look at Iterables in the guava library:



        public static <T> Iterable<List<T>> partition(Iterable<T> iterable, int size)



        You could use it like this:



        int outerLowerBound = 0;    
        int outerUpperBound = 605;
        int numberOfParts = 5;

        List<Integer> collect = IntStream.range(outerLowerBound, outerUpperBound)
        .boxed().collect(Collectors.toList());
        Iterable<List<Integer>> partitions = Iterables.partition(collect, numberOfParts);


        Inside the partitions list are your intervals. Then for each interval; 1st item is lower bound and last item is upper bound.



        for (List<Integer> partition : partitions) {
        System.out.println("[ " + Iterables.getFirst(partition, 0) + ", " + Iterables.getLast(partition) + " ]");
        }


        Output:



        [ 1, 5 ]
        [ 6, 10 ]
        [ 11, 15 ]
        [ 16, 20 ]
        [ 21, 25 ]
        [ 26, 30 ]
        [ 31, 35 ]
        ...
        [ 581, 585 ]
        [ 586, 590 ]
        [ 591, 595 ]
        [ 596, 600 ]
        [ 601, 604 ]


        See doc:



        https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html#partition-java.lang.Iterable-int-






        share|improve this answer














        Take a look at Iterables in the guava library:



        public static <T> Iterable<List<T>> partition(Iterable<T> iterable, int size)



        You could use it like this:



        int outerLowerBound = 0;    
        int outerUpperBound = 605;
        int numberOfParts = 5;

        List<Integer> collect = IntStream.range(outerLowerBound, outerUpperBound)
        .boxed().collect(Collectors.toList());
        Iterable<List<Integer>> partitions = Iterables.partition(collect, numberOfParts);


        Inside the partitions list are your intervals. Then for each interval; 1st item is lower bound and last item is upper bound.



        for (List<Integer> partition : partitions) {
        System.out.println("[ " + Iterables.getFirst(partition, 0) + ", " + Iterables.getLast(partition) + " ]");
        }


        Output:



        [ 1, 5 ]
        [ 6, 10 ]
        [ 11, 15 ]
        [ 16, 20 ]
        [ 21, 25 ]
        [ 26, 30 ]
        [ 31, 35 ]
        ...
        [ 581, 585 ]
        [ 586, 590 ]
        [ 591, 595 ]
        [ 596, 600 ]
        [ 601, 604 ]


        See doc:



        https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/Iterables.html#partition-java.lang.Iterable-int-







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 17:35

























        answered Nov 14 '18 at 15:38









        Rob

        410319




        410319






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302032%2fgenerate-ranges-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Why https connections are so slow when debugging (stepping over) in Java?