I would like to know what are the best ways to split a sheet into smaller sheets according to the values ​​in the object, preferably within the framework of Java SE 7 . For example, there is an ArrayList personList containing 1000 objects of type Person, sorted by Person.name. It is necessary to break the personList, into other sheets by the same person names. So far, apart from such things, nothing came to mind:

ArrayList<Person> personList = new ArrayList<Person>(); ArrayList<ArrayList<Person>> allLists = new ArrayList<ArrayList<Person>>(); ArrayList<Person> arr2 = new ArrayList<Person>(); String name = personList.get(0).getName(); arr2.add(personList.get(0)); for (int i = 1; i < personList.size(); i++) { if(!name.equals(personList.get(i).getName())) { name = personList.get(i).getName(); allLists.add(arr2); arr2 = new ArrayList<Person>(); } if(personList.size() == i+1) { arr2.add(personList.get(i)); allLists.add(arr2); break; } arr2.add(personList.get(i)); } 
  • through streams make - Sanaev
  • and if there is a lot of data, then through parallelstream - Sanaev
  • @Sanaev They are available in the 8th jdk version, I forgot to point out that I need solutions to the 7th version. - baka 5:26 pm
  • I understand you have a word - a separator and you divide a sheet by it? - Sanaev
  • You can try to do it through the sublist - Sanaev

2 answers 2

If you want to split the list into identical elements:

  List<String> personList = new ArrayList<>(); personList.add("Dimon"); personList.add("Dimon"); personList.add("Dimon"); personList.add("Ivan"); personList.add("Ivan"); personList.add("Ivan"); personList.add("Petr"); personList.add("Petr"); personList.add("Petr"); personList.add("Max"); personList.add("Sergey"); personList.add("Leha"); personList.add("Leha"); personList.add("Leha"); personList.add("Max"); personList.add("Leha"); personList.add("Leha"); personList.add("Leha"); List<List<String>> allLists = new ArrayList<>(); int k=0; for (int i = 0; i < personList.size()-1; i++) { String name = personList.get(i++); while(i< personList.size() && name.equals(personList.get(i)) ){i++;} if(i< personList.size()){ allLists.add(personList.subList(k,i)); k=i--; } } allLists.add(personList.subList(k,personList.size())); 

The result is:

enter image description here

    Here k is the beginning of the sublist index.

    Further, after we have found the name name (the name is a word - separator) we make a sublist of i + 1 (not inclusive) and write the result on the sheet.

     List<String> personList = new ArrayList<>(); personList.add("Dimon"); personList.add("Ivan"); personList.add("Petr"); personList.add("Max"); personList.add("Sergey"); personList.add("Leha"); personList.add("Max"); personList.add("Leha"); List<List<String>> allLists = new ArrayList<>(); String name = "Max"; int k=0; for (int i = 1; i < personList.size(); i++) { if(name.equals(personList.get(i))) { allLists.add(personList.subList(k,i+1)); k=i+1; } } allLists.add(personList.subList(k,personList.size())); 

    Below is the result of the function:

    enter image description here