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

Coffee break #144. How to convert 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 the different ways to convert an array to a List (ArrayList) and see the code examples in each. Coffee break #144.  How to convert array to List(ArrayList) in Java.  Dependency Injection in Java - 1Although arrays are simple and easy to use, they have many limitations, such as a 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 a List (List), a Set (Set), and a Queue (Queue) in various ways. For example, using a versatile and flexible array list (ArrayList). There are three methods that can be used when converting an array in Java. These methods include:
  1. Naive or Brute Force Method.
  2. Arrays.asList() method .
  3. Collections.addAll() method .

Using the 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 an array.
  • Create an empty list.
  • Iterate over the elements in the array.
  • Now add each element to the array.
  • Return the complete list.
In this example, an array of fruits is converted to 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

With the Arrays.asList() method , an array is passed to the List constructor as a parameter to the constructor. Here are the steps to convert an array to a list:
  • Get an array.
  • Create a list by passing an array as a parameter in the list constructor.
  • Return the complete list.
Here is 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

Because lists are part of the Java Collection package, 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.
  • We create an empty list.
  • Let's convert the array to a list using the collections.addAll() method .
  • We return the 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 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 concerns of creating objects and using them. The injection 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) gets its dependencies from external code (the "injector") that it doesn't know about. Here is a good example. When class X uses some of the features of class Y, we say that class X has a dependency on class Y. Dependency injection allows you to create dependent objects outside of the class and provides these 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.
  1. A Service Class that provides services to the client class.
  2. Client Class - A class that depends on the service class.
  3. Injector Class - A class that injects a service class object into the client class.

Types of Dependency Injection

There are three types of dependency injection:
  1. Constructor Injection - In constructor injection, 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 the 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 a 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