Home >Java >javaTutorial >Why do we need to use collections framework in Java?

Why do we need to use collections framework in Java?

WBOY
WBOYforward
2023-08-27 18:09:07839browse

Why do we need to use collections framework in Java?

An array is a collection that stores elements of the same type in consecutive memory allocations. They are used to represent lists of things like numbers, strings, etc.

grammar

<element-type>[] <array-name> = new <element-type>[<array-size>];

algorithm

To implement an array, please follow these steps

  • Step 1 should be carefully considered in advance for each of the elements that will populate the required array Select the appropriate data type for individual elements.

  • Step 2 In addition, determining the required capacity by considering specific usage requirements will be able to Choose an accurate and optimal array size.

  • Step 3 − Declare an array variable.

  • Step 4 - Access individual elements of the array. Just use the indexing operator [] and do whatever is necessary.

  • Step 5 - Perform relevant operations on each element contained in the array. Loops can be used to systematically iterate through them and perform the required tasks.

The Chinese translation of

Example

is:

Example

public class Main {
   public static void main(String[] args) {
      int[] marks = new int[3];
      marks[0] = 75;
      marks[1] = 20;
      marks[2] = 87;
      for (int i = 0; i < marks.length; i++) {
         System.out.println("Marks of student " + (i + 1) + ": " + marks[i]);
      }
      int sum = 0;
      for (int i = 0; i < marks.length; i++) {
         sum += marks[i];
      }
      double average = (double) sum / marks.length;
      System.out.println("Average marks: " + average);
   }
}

Output

Marks of student 1: 75
Marks of student 2: 20
Marks of student 3: 87
Average marks: 60.66

Limitations of arrays in Java

  • The size of the array is fixed - The size of the array cannot be increased or decreased at runtime.

  • Arrays are not memory efficient - If the number of elements added to an array is less than the allocated size, memory may be wasted. p>

  • Array has no built-in available methods− Array does not have any built-in methods to perform common operations such as adding, searching, etc.

  • Arrays contain only data elements of the same type - Arrays can only store elements of the same type.

  • No underlying data structure - The idea of ​​an array is not implemented using standard data structures. Therefore, no ready method support is available.

Collection framework in java

In Java, a framework aims to provide a standardized way, or we can say it provides a ready-made architecture to solve a specific problem or task by utilizing a set of abstract classes and interfaces and other components.

A collection is a combination of multiple individual objects as a whole. Java's collection framework provides several different classes and interfaces that can effectively represent collections. Common choices include ArrayList, LinkedList, HashSet, and TreeSet, which can be accessed through the java.util package.

grammar

The syntax depends on the specific class, for example -

The translation of

ArrayList

is:

ArrayList

ArrayList<T> list = new ArrayList<T>();
The translation of

LinkedList

is:

Linked List

LinkedList<T> list = new LinkedList<T>();

algorithm

To implement, follow these steps -

  • Step 1 - Select the appropriate collection class based on the requirements of the program.

  • Step 2 - Import the classes required for the collection.

  • Step 3 - Declare a variable of the collection class.

  • Step 4 - Instantiate the collection using the appropriate constructor.

  • Step 5 - Use method as per requirement.

  • Step 6 - Use this collection in your program as needed.

The translation of

Example

is:

Example

import java.util.ArrayList;
public class Main {
   public static void main(String[] args) {
      ArrayList<String> names = new ArrayList<String>();
      names.add("Apoorva");
      names.add("Anamika");
      names.add("Supriya");
      System.out.println("Names: " + names);
      names.remove(1);
      names.set(1, "Neha");
      System.out.println("Names: " + names);
      for (String name : names) {
         System.out.println("Name: " + name);
      }
   }
}

Output

Names: [Apoorva, Anamika, Supriya]
Names: [Apoorva, Neha, Supriya]
Name: Apoorva
Name: Neha
Name: Supriya

Thus, using the Java collection framework can overcome the shortcomings or limitations of arrays. So we need a collection framework. The advantages of this framework are as follows -

  • Scalable nature of collections − Now, due to the scalable nature of collections, size is no longer an issue and we can increase or decrease the size at runtime.

  • Collections are memory efficient - Elements can be increased or decreased as per requirement, so from a memory perspective, it is recommended to use collections.

  • Built-in methods available in collections− Collections have many built-in methods to perform common operations such as add, search, etc.

  • Collections hold homogeneous and heterogeneous data elements - Collections can hold elements of the same as well as different types.

  • Standard Data Structures - Collections are based on standard data structures, so each collection supports ready-made methods.

in conclusion

Obviously, both arrays and collections have unique advantages and disadvantages. The specific requirements of your program are critical in deciding between them. Arrays prove to be more suitable in situations where the data size is predetermined and fast access to elements is required. Collections are better suited when you need a more flexible data structure and need built-in methods to manipulate the data.

The above is the detailed content of Why do we need to use collections framework in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete