Ú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?:))
- EKSÖN! Microsoft Surface Prémium Érintős Laptop Ultrabook 13,5" -65% i5-7300 8/256 Tűéles Retina
- Xiaomi 15T Pro 256GB, Kártyafüggetlen, 1 Év Garanciával
- ZOTAC GeForce RTX3080 AMP HOLO LHR 10GB Dobozos
- Lenovo ThinkStation P300 Workstation,i7-4790,8GB DDR3,500GB SATA3 HDD,2GB VGA,WIN11
- Eladó GIGABYTE Z790 GAMING X AX Alaplap!
- Eladó Apple iPhone 12 64GB / 12 hó jótállás
- DDR5 8/ 16/ 32GB 4800-5600MHz UDIMM PC RAM, több db- számla, garancia
- iking.hu Apple iPhone 15 128GB Blue használt, karcmentes 86% akku 6 hónap garancia
- ÁRGARANCIA!Épített KomPhone i5 12400F 16/32/64GB RAM RTX 5070 12GB GAMER PC termékbeszámítással
- ÁRGARANCIA! Épített KomPhone i5 12400F 16/32/64GB RAM RTX 3060 12GB GAMER PC termékbeszámítással
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
