Java

Managing collections of data in Java

The Java Collections Framework supports multiple approaches to creating and managing collections of data. Collections of data can be ordered or unordered. There are various interfaces that are exposed by the Java Collections Framework to implement specific classes and methods.

Arrays

Arrays are ordered collections of data items. You can define an array by either explicitly declaring the number of data items in the array or by creating the data items in the array when declaring it.

An array is not resizable and you cannot add or delete items from it, you can, however, reset the values at varying indexes if required.

public class CreateAStringArray {

     public static void main(String[] args) {

          //Declaring and displaying items in an array of type String
          String[] colors = new String[3];
          colors[0] = "Green";
          colors[1] = "Blue";
          colors[2] = "Red";
   
          //display the values of the array
          for (int i = 0; i < colors.length; i++) {
              System.out.println(colors[i]);
          }

          //Defining an object array
          Clothing[] items = {
              new Shirt(
                  Size.M,
                  Qty:3,
                  Price:20.00),
              new Hat(
                  Size.S,
                  Qty:6,
                  Price:5.00)
          }; 

          //display the clothing item objects of the array
          for (Clothing item: items) {
              displayClothingItems(item);
          }
     }

     //method for displaying clothing items
     public static void displayClothingItems(Clothing item) {
          var totalPrice = item.getPrice() * item.getQty();
          var formatter = NumberFormat.getCurrencyInstance();
          var output = String.format("You cart items are :",
                  item.getSize(),
                  item.getType(),
                  formatter.format(totalPrice));
          System.out.println(output);
     }
//Merge arrays
import java.util.*;  
public class MergeArrayCollectionsExample  {  
    
     public static void main(String[] args)   {  
    
        //source array
        String str1[] = { "N", "A", "I" };
        //destination array             
        String str2[] = { "O", "M", "I" };                
        
        //returns a list view of an array  
        List list = new ArrayList(Arrays.asList(str1)); 

        //returns a list view of str2 and adds all elements of str2 into list  
        list.addAll(Arrays.asList(str2)); 
    
        //converting list to array  
        Object[] str3 = list.toArray();
        
        //prints the result
        System.out.println(Arrays.toString(str3));
    
     }
}  

Lists

A list is an interface of the Java Collections Framework. When declaring a list, you can define the types of the items in the list using generic notation.

public class ImplementingListsExample {
   public static void main(String[] args) {

       List<String> colors = new ArrayList<>();
       colors.add("Red");
       colors.add("Yellow");
       colours.add("Purple");
       
       //displaying the items from the list
       for (int i = 0; i < colors.size; i++) {
          System.out.println(colors.get(i));
       }

       List<ClothingItem> items = new ArrayList<>();
       items.add(new Shirt(
                 ClothingSize.L,
                 Price:19.99,
                 Quantity:3));
       items.add(new Hat(
                 new Hat(
                  Size.S,
                  Qty:6,
                  Price:5.00));

       for (ClothingItem item : item) {
            displayItemDetails(item);
       }

    }
    
    //method for displaying clothing items
     public static void displayClothingItems(Clothing item) {
          var totalPrice = item.getPrice() * item.getQty();
          var formatter = NumberFormat.getCurrencyInstance();
          var output = String.format("You cart items are :",
                  item.getSize(),
                  item.getType(),
                  formatter.format(totalPrice));
          System.out.println(output);
     }
}

Maps

Maps are typically used for unordered data collections where data is stored in a key-value pair. A map is also an interface that exposed concrete classes that implement the methods defined by the Map interface.

When declaring a Map, you can use generic notation to indicate the type of both the key and the value. In Java, any object type can be used.

public class MapExample{

   public static void main(String[] args) {

   Map<String, ClothingItem> items = new HashMap<>();
   items.put("shirt", new Shirt(
             ClothingSize.L,
             Price:19.99,
             Qty:3));
   items.put("hat", new Hat(
             ClothingSize.M,
             Price:29.99,
             Qty:1));
   }

   //display an item
   var anItem = items.get("hat");
   displayItemDetails(anItem);

   //display all items
   for (String key : keys) {
       var item = items.get(key);
       displayItemDetails(items);
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *