Created
July 4, 2025 23:28
-
-
Save gbajaj/8f5ba8283223f55000d4c01496c4cd98 to your computer and use it in GitHub Desktop.
Convert int [][] into a list of custom Records
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class CustomRecordExample { | |
// Your static nested class is defined here | |
static class Record { | |
int first; | |
int second; | |
int third; | |
public Record(int first, int second, int third) { | |
this.first = first; | |
this.second = second; | |
this.third = third; | |
} | |
@Override | |
public String toString() { | |
return "Record[" + first + ", " + second + ", " + third + "]"; | |
} | |
} | |
public static void main(String[] args) { | |
int[][] sourceArray = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}}; | |
// Stream the 2D array and map each row to a new Record instance | |
List<Record> recordList = Arrays.stream(sourceArray) | |
.map(row -> new Record(row[0], row[1], row[2])) | |
.collect(Collectors.toCollection(ArrayList::new)); | |
System.out.println(recordList); | |
// Output: [Record[10, 20, 30], Record[40, 50, 60], Record[70, 80, 90]] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment