Loading topic
List<String>. Now suppose you want a list of numbers. Your first try might be List<int>. Java will not compile it. This chapter explains why, what to use instead, and the few traps that come with storing numbers in collections.4 min readArrayList, which is a list that keeps items in order and lets you reach any item by its index. Java has a second common list called LinkedList. Both classes implement the List interface, so they share methods like add, get, set, remove, and size. The difference is ho5 min readList and check for duplicates yourself, but Java has a collection built for the job. This chapter shows you how to use it.4 min readHashSet, you saw that a set keeps each element only once. You also saw that a HashSet makes no promise about order. When you print it, the elements can come out in any order, and that order can change as the set grows.4 min readput, get, and merge to work with one key at a time. This chapter shows how to visit every pair in a map, one after another. Visiting each item in turn5 min readHashMap finds values fast, but it makes no promise about order. If you print one, the keys can come out in any order. That order can even change when you add more keys. Sometimes you need a fixed order, for example in a report or a menu.5 min readArrayList, HashSet, or HashMap and calling add or put many times. That works, but often you already know every element when you write the code. Examples are the days of the week, a list of allowed file types, or a fixed price table. In5 min readArrayList, LinkedList, HashSet, LinkedHashSet, TreeSet, HashMap, LinkedHashMap, and TreeMap. This chapter shows you how to pick one of them for a task. You can usually decide by answering three or four short questions about your data.5 min read