0

So in my project I used to read/write data from/to .txt files, but I realized that It will be better if I would do that from an excel file. This is how I did it.

        for (File benchmarkLoop : listOfFiles) {
        String line = null;
        BufferedReader in = null;
        try {
                in = new BufferedReader(new FileReader("benchmarks\\" + benchmarkLoop.getName()));  
            } catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
            }
            Writer writer = null;
            File file = new File("results", benchmarkLoop.getName());
            writer = new BufferedWriter(new FileWriter(file));}

Now I have to change this and I'm not so familiar with jxl.

        while (initializingIterations > 0) {
                line = in.readLine();
                writer.write(0 + System.getProperty("line.separator"));          
                markov.update(  new Integer((int) (Math.round(Float.parseFloat(line)/interval))));   
                initializingIterations--;     
            }

        while ((line = in.readLine()) != null ) 
Catalin
  • 31
  • 8
  • What have you tried and where are you stuck? subList() not working for you? – OH GOD SPIDERS Mar 21 '17 at 09:53
  • 1
    Possible duplicate of [Java: how can I split an ArrayList in multiple small ArrayLists?](http://stackoverflow.com/questions/2895342/java-how-can-i-split-an-arraylist-in-multiple-small-arraylists) – Krishnanunni P V Mar 21 '17 at 10:03

3 Answers3

2

Try this,

Lists.partition(pv1Column, 3);

Refer: Lists.Partition()

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

Using Java 8 without any additional libraries:

private static <T> List<List<T>> partitionList(List<T> list, int size) {
    return IntStream.range(0, (list.size() + (size - 1)) / size)
            .mapToObj(x -> list.subList(x * size, Math.min((x + 1) * size, list.size())))
            .collect(Collectors.toList());
}
Markus Benko
  • 1,507
  • 8
  • 8
0
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SplitList {
    public static void main(String[] args){
        List<Integer> list = IntStream.rangeClosed(0, 10).boxed().collect(Collectors.toList());
        int[] count = new int[1];
        List<List<Integer>> sublists = list.stream()
                .collect(Collectors.groupingBy(e -> (list.size() - count[0]++)/6))
                .values()
                .stream()
                .collect(Collectors.toList());
        sublists.stream().forEach(System.out::println);
    }
}


>> The results on cli:
[6, 7, 8, 9, 10]
[0, 1, 2, 3, 4, 5]
maxwu
  • 421
  • 6
  • 17