March 2018
Intermediate to advanced
208 pages
4h 52m
English
| | class Inventory { |
| | |
| | private List<Supply> supplies = new ArrayList<>(); |
| | |
| | List<Supply> find(String regex) { |
| | List<Supply> result = new LinkedList<>(); |
| | for (Supply supply : supplies) { |
| » | if (Pattern.matches(regex, supply.toString())) { |
| | result.add(supply); |
| | } |
| | } |
| | return result; |
| | } |
| | } |
When you iterate over a data structure, you need to be careful with what kind of operations you perform. If you do something that is compute-intense, it can easily turn into a performance pitfall. The code above shows a typical example for this with the method find() that locates Supply objects with a regular expression.
In Java, or in any other programming language, you’ll build query strings ...