In this article we can discuss the best practices of java collection and sample examples for each usages.
1) Choose the right collection
This is the most important step before using any collection. Depending upon functionality or problem solve to decide the right collection to use in the code implementation. There are multiple criteria to choose right collection -
1) collection allows duplicates or not
2) accepts null or not
3) search based on index
4) Supports concurrency.
and also the developer should know the performance impact on each collection usages.
When you declare or create any collection, prefer to use interface type instead of specific collection class.
Example:-
List<String> listOfItems = new ArrayList<>();
instead of,
ArrayList<String> listOfItems = new ArrayList<>();
Using interface type is more flexible and convenient because we can change concreate implementation as needed.
For example, List<String> listOfItems = new LinkedList<>();
3) Method return type should be interface instead of collection class.
Best practice to return type of method should be interface instead of collection class. It is more flexible because if any changes done inside the method should not impact on the caller.
public List<String> getAllItems() {
List<String> listOfItems = new ArrayList<>();
//in future if we change arrayList to linkedList it doesn't impact on caller
// get all items - fetch items from database
return listOfItems;
}
4) Use generic type and diamond operator
Use generic type when declare any collection otherwise chances of throwing ClassCastException at run time.
Example : -
List listOfItems = new ArrayList<>(); listOfItems.add("item"); listOfItems.add(1);
so avoid using above declaration, apply generics,
List<String> listOfItems = new ArrayList<>();
listOfItems.add("company");
listOfItems.add(12); //compile time error
Use diamond operator(<>),
This operator <> is called the diamond operator. Without diamond operator we have to write declaration twice so using this operator no need to write declaration twice as follows,
List<String> listOfItems = new ArrayList<String>();
With <> operator,
List<String> listOfItems = new ArrayList<>();
if (listOfItems.size > 0) {
//write logic if list is not empty
}
if (!listOfItems.isEmpty()) {
//write logic if list is not empty
}
if (CollectionUtils.isNotEmpty(listOfItems)) {
//write logic if list is not empty
}
//Avoid this public List<String> getAllPreSaleItems(String itemType) { List<String> preSaleItems = null; if (itemType.equalsIgnoreCase("preSale")) { //preSaleItems = fetch all presale items from database } return preSaleItems; }
Best practice to return empty list as,
//best practice to return empty list private List<String> getAllPreSaleItems(String itemType) { List<String> preSaleItems = null; if (itemType.equalsIgnoreCase("preSale")) { //preSaleItems = fetch all presale items from database } if (preSaleItems == null) { return Collections.EMPTY_LIST; } return preSaleItems; }
List<String> listOfItems = Arrays.asList("a", "b", "c"); for (int i=0; i<listOfItems.size(); i++) { System.out.println(listOfItems(i)); }
List<String> listOfItems = Arrays.asList("a", "b", "c"); for (String item : listOfItems) { System.out.println(item); }
List<String> skuList = Arrays.asList("MTCCC", "MTAAA", "PT1111");
skuList.forEach(sku -> System.out.println(sku));