[Java 7] Divide the Java list and execute the process

Divide Java list and execute processing (Java 7)

Introduction

Depending on the environment, you may have to write old code on purpose. I had the opportunity to write code in Java7 that divides the List and executes the process, so I posted it for the first time.

Implementation


public class Sample {
	public static void main(String[] args) {

		List<String> sampleList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
		int size = 2;	//Number of lists to process at one time

		for (int i = 0; i < sampleList.size(); i += size) {
			List<String> list = new ArrayList<>(
					sampleList.subList(i, Math.min(i + size, sampleList.size()))
			);
			System.out.println(list);	//Perform processing here
		}
	}
}

Execution result
[aaa, bbb]	// sampleList.subList(0, 2)
[ccc, ddd]	// sampleList.subList(2, 4)
[eee]		// sampleList.subList(4, 5)

Description

subList (int from, int to) returns a List from from (included) to to (not included). The list to be processed is acquired by increasing the number of divisions of from and to by the for statement.

Math.min (int a, int b) returns the smaller of the two int values. If the size of the List is not divisible by the number of divisions, then you need to consider to, For the last iteration, we put sampleList.size () in to.

Notes on subList

The List obtained by subList shares data with the original value. That is, changing the value of list will change the value of sampleList (Example 1). However, you can change only the subList without changing the original List by making changes after new (Example 2).

Example 1 When the subList is changed, the original list is also changed.

public class Sample {
	public static void main(String[] args) {

		List<String> sampleList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
		int size = 2;	//Number of lists to process at one time

		System.out.println(sampleList);		//Before processing
		for (int i = 0; i < sampleList.size(); i += size) {
			List<String> list = sampleList.subList(i, Math.min(i + size, sampleList.size()));
			list.set(0, "zzz");				//Processing execution
			System.out.println(list);
		}
		System.out.println(sampleList);		//After treatment
	}
}
Execution result
[aaa, bbb, ccc, ddd, eee]	//Before processing
[zzz, bbb]
[zzz, ddd]
[zzz]
[zzz, bbb, zzz, ddd, zzz]	//After treatment

As mentioned above, as a result of updating list, it can be seen that sampleList has also been updated.


Example 2 By new, the original list is not updated even if the subList is changed.

public class Sample {
	public static void main(String[] args) {

		List<String> sampleList = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
		int size = 2;	//Number of lists to process at one time

		System.out.println(sampleList);		//Before processing
		for (int i = 0; i < sampleList.size(); i += size) {
			List<String> list = new ArrayList<>(
					sampleList.subList(i, Math.min(i + size, sampleList.size()))
			);
			list.set(0, "zzz");
			System.out.println(list);
		}
		System.out.println(sampleList);		//After treatment
	}
}
Execution result
[aaa, bbb, ccc, ddd, eee]	//After treatment
[zzz, bbb]
[zzz, ddd]
[zzz]
[aaa, bbb, ccc, ddd, eee]	//After treatment

As mentioned above, you can see that changing list does not change sampleList.

reference

Recommended Posts

[Java 7] Divide the Java list and execute the process
[Java] Understand the difference between List and Set
[Java] Sort the list using streams and lambda expressions
[Java] Delete the elements of List
Divide the List into arbitrary numbers
Execute Java code stored on the clipboard.
Arrylist and linked list difference in java
Process every arbitrary number in Java List
[Java8] Search the directory and get the file
[Java] Contents of Collection interface and List interface
Check the options set for the running Java process
Java buffering process
Java memorandum (list)
Clone Java List.
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
Memo: [Java] Process the read csv (extract, change according to the conditions) and output
Summarize the differences between C # and Java writing
Java check process
XXE and Java
Compile and run Java on the command line
A note about the Rails and Vue process
About List [Java]
Form and process file and String data at the same time with Spring Boot + Java
ArrayList and the role of the interface seen from List
Write ABNF in Java and pass the email address
How far is the correct answer to divide the process?
Please note the division (division) of java kotlin Int and Int
[No.007] Organization management screen and login process to the organization
To implement, write the test and then code the process
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
Quick learning Java "Introduction?" Part 2 Let's write the process
[JAVA] What is the difference between interface and abstract? ?? ??
Learn for the first time java # 3 expressions and operators
[Java] Get the file path in the folder with List
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
List of frequently used Java instructions (for beginners and beginners)
21 Load the script from a file and execute it
Prepare the environment for java11 and javaFx with Ubuntu 18.4
What is the difference between Java EE and Jakarta EE?
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Filter the Java No. 2 Optional list that was useful in business and get the first value
About Java Array List
Access Web API on Android with Get and process Json (Java for the time being)
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
List and happy classes
[Java] Variables and types
java (classes and instances)
[Memo] Java Linked List
[Java] Overload and override
[Note] Java Output of the sum of odd and even elements
Generate and execute Jar file of Java file belonging to package
[Java] Throw a request and display the screen ② (GET / POST)