Java Lesson 6: Collections β€” List and Map

Java Lesson 6: Collections β€” List and Map featured illustration

πŸ“Ž This article includes 1 downloadable practice file ↓

⏱ 1 min readUpdated 12 October 2026

πŸ“˜ Java Beginner Course Β· Lesson 6 of 8

List<Line> lines = new ArrayList<>();
Map<String, Double> byItem = new TreeMap<>();
for (Line l : lines) byItem.merge(l.item(), l.amount(), Double::sum);   // like SUMIF
double taxable = lines.stream().mapToDouble(Line::amount).sum();

List keeps order and allows duplicates; Map stores key-value pairs (TreeMap sorted by key, HashMap fastest). Streams express filter/map/sum in one line.

Advertisement

More: Java streams for beginners.

Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.

Advertisement

Practice

Add a third invoice line and check the totals.

πŸ“Ž Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong