Today I will introduce to you the usage of list segmentation in Java. The content is very compact. I hope you can study it carefully.
Sometimes, we need to export data from one system and import it into another system. The data is very large, and the data import is restricted and cannot be implemented. At this time, we need to list the data. Segment, then export one by one, and finally, implement data import.
For segmented processing of data, we can use the subList method to implement it. For specific usage, please refer to the following cases:
import java.util.ArrayList; import java.util.List; public class listTest { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); list.add("five"); list.add("six"); list.add("seven"); int ftest = 3;//每次取的数据 int size = list.size(); int temp = size / ftest + 1; boolean special = size % ftest == 0; List<String> cutList = null; for (int i = 0; i < temp; i++) { if (i == temp - 1) { if (special) { break; } cutList = list.subList(ftest * i, size); } else { cutList = list.subList(ftest * i, ftest * (i + 1)); } System.out.println("第" + (i + 1) + "组:" + cutList.toString()); } } }
The result is:
Group 1 :[one, two,three]
Group 2: [four, five,six]
Group 3: [seven]
If ftest = 1, get The result is:
Group 1: [one]
Group 2: [two]
Group 3: [three]
Group 4: [four]
Group 5: [five]
Group 6: [six]
Group 7: [seven]
The above are examples of its usage, which can be combined with examples for practical application!
Related recommendations:
Analysis and processing of Java thread leaks
Handling of unsigned numbers in Java
The above is the detailed content of How to do segmentation in Java? Of course, use list. For more information, please follow other related articles on the PHP Chinese website!