Új hozzászólás Aktív témák
-
Csaby25
őstag
Sziasztok!
A következő metódus csv file-ból olvas ki sorokat és objektummá alakítom (minden sor egy objektum):private static void readData(File inputFile, Class type) {
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String line;
// Reading first line..
while ((line = reader.readLine()) != null) {
String[] values = line.split(COMMA_DELIMITER);
if (type == Student.class) {
Student student = populateStudent(values);
if (students == null) {
students = new HashMap<Integer, Student>();
}
students.put(student.getStudentId(), student);
}
if (type == Subject.class) {
Mark mark = populateMark(values);
Subject subject = populateSubject(values);
if (subject.getMarks() == null) {
subject.setMarks(new ArrayList<Mark>());
}
students.get(mark.getStudentId()).getSubjects().put(subject.getSubjectId(), subject);
students.get(mark.getStudentId()).getSubjects().get(subject.getSubjectId()).getMarks().add(mark);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Ez szeretném megoldani Stream-el:
private static void readData(Path inputFile, Class type) {
Student student;
Subject subject;
Mark mark;
try {
Stream<String> lines = Files.lines(inputFile);
List<List<String>> values = lines.map(line -> Arrays.asList(line.split(COMMA_DELIMITER))).collect(Collectors.toList());
if (type == Student.class) {
values.forEach(value -> student = populateStudent(value));
if (students == null) {
students = new HashMap<Integer, Student>();
}
students.put(student.getStudentId(), student);
}
if (type == Subject.class) {
values.forEach(value -> mark = populateMark(value));
if (subject.getMarks() == null) {
subject.setMarks(new ArrayList<Mark>());
}
students.get(mark.getStudentId()).getSubjects().put(subject.getSubjectId(), subject);
students.get(mark.getStudentId()).getSubjects().get(subject.getSubjectId()).getMarks().add(mark);
}
//}
} catch (Exception e) {
e.printStackTrace();
}
}
A forEach-ben (
value -> student = populateStudent(value)
) ezt a hibát kapom: ''Local variable student defined in an enclosing scope must be final or effectively final'Tudja valaki, hogy lehet ezt megoldani?
Köszi!
Új hozzászólás Aktív témák
A topicot kiemeltem. Valaki nem akar egy nyitó hsz-t írni?:))
- Gamer PC-Számítógép! Csere-Beszámítás! R5 3600 / GTX 1080 8GB / 32GB DDR4 / 512 SSD!
- Új áru!!! 09.23. Lenovo ThinkPad, X1 carbon, X1 Yoga 5-13. gen 12,5-15" all-in-one, Workstation
- Xiaomi Redmi Note 10 Pro 128GB // Számla+Garancia //
- ÚJ Dell Latitude 15 5550 - 15.6"FullHD IPS - Ultra 5 135U - 16GB - 512GB SSD - Win11 - 2,5+ év gari
- PlayStation 5 FAT Lemezes + kontroller 6 hó garancia, számlával!
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest