JavaRush /Java Blog /Random EN /Coffee break #144. How to convert an array to List (Array...

Coffee break #144. How to convert an array to List (ArrayList) in Java. Dependency Injection in Java

Published in the Random EN group

How to convert array to List (ArrayList) in Java

Source: Rrtutors Today you will learn about different ways to convert an array to a List (ArrayList) and look at code examples in each of them. Coffee break #144.  How to convert an array to List (ArrayList) in Java.  Dependency Injection in Java - 1Although arrays are simple and easy to use, they have many limitations, such as fixed size. This makes it difficult to add a new element at the beginning and rearrange elements. Thanks to the Collections Framework, we can implement List, Set, and Queue in different ways. For example, using a universal and flexible list of arrays (ArrayList). There are three methods you can use when converting an array in Java. These methods include:
  1. Naive or Brute Force Method.
  2. Arrays.asList() method .
  3. Collections.addAll() method .

Using a naive or brute force method

In this method, a list is created from an empty array and each element of the array is added to it one by one. The method works after doing the following:
  • Get the array.
  • Create an empty list.
  • Loop through the elements in the array.
  • Now add each element to the array.
  • Return the complete list.
This example converts an array of fruits into a list of fruits:
import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class NaivemethodExample {

    public static <T> List<T> convertArrayToList(T array[])

    {

        List<T> list = new ArrayList<>();

        for (T t : array) {

            list.add(t);

        }

        return list;

    }

    public static void main(String args[])

    {

        String array[]

            = { "Mangoes", "Oranges", "berries" };

        System.out.println("Array: "

                           + Arrays.toString(array));

        List<String> list = convertArrayToList(array);

        System.out.println("List: " + list);

    }

}

Arrays.asList() method

Using the Arrays.asList() method , an array is passed to the List constructor as a parameter to the constructor. To convert an array to a list here you need to follow the following steps:
  • Get the array.
  • Create a list by passing the array as a parameter in the list constructor.
  • Return the complete list.
Here's an example of using the Arrays.asList() method to convert an array of fruits to a list:
package asList;

import java.util.Arrays;

import java.util.List;

public class asListExample {

    public static <T> List<T> convertArrayToList(T array[])

    {

        List<T> list = Arrays.asList(array);

        return list;

    }

    public static void main(String args[])

    {

        String array[]

            = { "Mangoes", "Oranges", "berries" };

        System.out.println("Array: "

                          + Arrays.toString(array));

        List<String> list = convertArrayToList(array);

        System.out.println("List: " + list);

    }

}

Collections.addAll() method

Since lists are part of the Collection package in Java, you can convert an array to a list using the Collections.addAll() method . This method converts an array to a list using the following steps:
  • We get an array.
  • Create an empty list.
  • Let's convert the array to a list using the collections.addAll() method .
  • We return a list.
Example:
import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class collectionsall {

    public static <T> List<T> convertArrayToList(T array[])

    {

        List<T> list = new ArrayList<>();

        Collections.addAll(list, array);

        return list;

    }



    public static void main(String args[])

    {

        String array[]

            = { "peas", "tomatoes", "water melons" };

        System.out.println("Array: "

                           + Arrays.toString(array));

        List<String> list = convertArrayToList(array);

        System.out.println("List: " + list);

    }

}
Conclusion:
Array: [peas, tomatoes, water melons ] List: [peas, tomatoes, water melons ]

Dependency Injection in Java

Source: Medium In this post, you will learn what dependency injection is in Java, where it is used, and what benefits it provides to the developer. Coffee break #144.  How to convert an array to List (ArrayList) in Java.  Dependency Injection in Java - 2Dependency Injection (DI) is the process of providing an external dependency to a software component. Dependency injection aims to separate the problems of creating objects from using them. The embedding principle assumes that an object or function that wants to use a given service does not need to know how to create it. Instead, the receiving "client" (object or function) receives its dependencies from external code (an "injector") that it is unaware of. Here's a good example. When a class X uses some functionality of a class Y, we say that class X has a dependency on class Y. Dependency injection allows you to create dependent objects outside the class and exposes those objects to the class in various ways. In this case, the creation and binding of dependent objects are moved outside the class that depends on them. The dependency injection pattern includes three types of classes:
  1. Service Class that provides services to a client class.
  2. Client Class - a class that depends on the service class.
  3. Injector Class is a class that injects a service class object into a client class.

Types of Dependency Injection

There are three types of dependency injection:
  1. Constructor Injection - When constructor injection occurs, the injector provides a service (dependency) through the constructor of the client class.
  2. Setter Injection - In this type of injection (also known as property injection), the injector exposes a dependency through a public property of the client class.
  3. Method Injection - In this type of injection, the client class implements an interface that declares the method(s) to provide the dependency. The injector uses this interface to provide the dependency to the client class.

Benefits of Dependency Injection

Implementing dependency injection gives us the following benefits:
  • Code reuse.
  • Ease of refactoring.
  • Ease of testing.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION