Favor Java API Over DIY

 class​ Inventory {
 
 private​ List<Supply> supplies = ​new​ ArrayList<>();
 
 int​ getQuantity(Supply supply) {
 if​ (supply == ​null​) {
»throw​ ​new​ NullPointerException(​"supply must not be null"​);
  }
 
 int​ quantity = 0;
 for​ (Supply supplyInStock : supplies) {
 if​ (supply.equals(supplyInStock)) {
  quantity++;
  }
  }
 
»return​ quantity;
 
  }
 }

In the early days of programming, you had to do everything by yourself. In C, for instance, you still need to create a String using a char[] or implement your own list. You had to do this for all kinds of data structures and algorithms. Although this is a nice exercise, it’s also time-consuming and error-prone.

Times have changed. The Java API is ...

Get Java By Comparison now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.