Lists in Java - ProgrammerTech
Language:

Search

Lists in Java

  • Share this:
Lists in Java

 Lists

Lists in Java are a tool for storing data, just like an array. We use an array to store data, and a list is used to store data.

 

The difference between lists and arrays

List is more effective, dynamic and more efficient because through it we can search within the data with ease "search, add, save" and retrieve and read the data with ease. Lists have two types that we will explain in detail, namely (ArrayList, LinkedList).

 

ArrayList in Java

ArrayList and Array stands for an array and it will save us defining it and setting its size, meaning when. We know an array of type ArrayList we don't need to define its size because the size will grow automatically with adding elements, and it stores data in memory regularly.

List<String> s=new ArrayList<String>();

To build the List, we write the reserved word List and then follow it with the type of data that will be stored in the List, for example. String Then we write an object for example n = new then the name of the string and then the data type again.

package forech;
import java.util.ArrayList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<String> s=new ArrayList<String>();
        s.add("Saja");
        s.add("Ahmed");
        s.add("Ahsan");
        System.out.println(s);
    }
}

At first we included the input package for the ArrayList and also the input package for the List. After that, we entered the main program and created a not of type String and the input object is s. We wrote a name for the list and added three elements to it via the add function. And the elements are "Saja, Ahmed, Ahsan." Then we asked the program to display the complete data on the screen via the println function.

 

LinkedList in Java

This type stores data in memory randomly in the form of nodes and each node has an address. Which makes it easier for us to search, modify and add, and it is the most common and used type.

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<String> s=new LinkedList<String>();
        s.add("parrot");
        s.add("wazer");
        s.add("data");
        System.out.println(s);
    }
}

At first we included the input package for the LinkedList array and also the input package for the List. After that, we entered the main program and created a non-text type, then we added three elements to it, which are "parrot, wazer, data" and then we printed the entire array on the screen.

 

Adding various numerical values ​​in a List of type LinkedList

In this paragraph, we will learn how to define a non-number type, and a number is inclusive of Integer, Double, Float, and everything related to numbers.

The general form of adding numerical values ​​in a LinkedList

List<Number> s=new LinkedList<Number>();

To add an integer value of type Float, Long, or Double to the object we do not use.

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<Number> s=new LinkedList<Number>();
        s.add(new Integer(4));
        s.add(new Float(9.4));
        s.add(new Double(236.269));
        s.add(new Long(82687985));
        System.out.println(s);
    }
}

We entered the main program and created a non-number type called LinkedLists. Through the object s by adding four digital elements of different types (Int, Float, Double, Long) and then we print all the elements of the object on the screen.

 

Using foreach with Lists in Java

If we want to print each node individually, we must use the forEach property, and in the Java language we write a regular for.

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<Number> s=new LinkedList<Number>();
        s.add(new Integer(4));
        s.add(new Float(9.4));
        s.add(new Double(236.269));
        s.add(new Long(82687985));
        s.set(2, new Double(561.235));
        for(Number i:s)
        System.out.println(i);
    }
}

We entered the main program and created a non-number type called LinkedLists, then we added. Numbers of different data types via object s then we got into a foreEach loop and said. Its i:s means we store all the values ​​of s in i and then print i each node. seperately and i will still refer to the values ​​of the s value by value and will print the results to the screen each value on a line by itself.

 

Add a new value in List and locate it

In this paragraph, we will learn how to add a value to the list, as well as determine its location in the list, and how to retrieve a value from the list. We must know that the first node has its address 0, the second node is 1, the third is 2 ... etc. We use the add function to add.

1- Adding a new value to a specific location in the list

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<Number> s=new LinkedList<Number>();
        s.add(new Integer(3));
        s.add(new Float(5.4));
        s.add(new Double(2036.269));
        s.add(new Long(155562));
        s.add(2,new Integer(10));
        System.out.println(s);
    }
}

We created a non-number type and added four numeric values ​​of different types to it, then we forced it. By adding an integer value to node number 2, its type, Integer, and its value. 10 When running, the program will add it in the place of node 2 and move the rest forward.

2-Print one value from the list

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<Number> s=new LinkedList<Number>();
        s.add(new Integer(3));
        s.add(new Float(5.4));
        s.add(new Double(2036.269));
        s.add(new Long(155562));
        s.add(2,new Integer(10));
        Number N=s.get(1);
        System.out.println(N);
    }
}

We created a non-number type and added four numeric values ​​of different types to it and then forced it to add. An integer value in Node No. 2 of type Integer and a value of 10, and when running, the program will add it somewhere. Node 2 and shift the rest forward, then we created a variable with the same data type N and specified for it to print only node No. 1 for us through the object and the get function, and we set the print command to print N for us.

 

Editing or deleting a node in the list

If we want to modify a value in the list or delete a node, we can use the private object. List then the set function, and in the event of modification, we are asked to centralize the node and the new value, and to delete a value from a node we use the remove function.

 

Modify the set node in Java

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<Number> s=new LinkedList<Number>();
        s.add(new Integer(3));
        s.add(new Float(5.4));
        s.add(new Double(2036.269));
        s.add(new Long(155562));
        s.set(2, new Double(2665.62));
        System.out.println(s);
    }
}

In this example, we have modified node number 2 and added a new value to it, which is 2665.62. This number will replace node number 2 when printing.

 

delete node remove in java

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<Number> s=new LinkedList<Number>();
        s.add(new Integer(3));
        s.add(new Float(5.4));
        s.add(new Double(2036.269));
        s.add(new Long(155562));
        s.set(2, new Double(2665.62));
        s.remove(1);
        System.out.println(s);
    }
}

In this example we used the remove function to delete, and with the s object we used the remove from function. In order to delete the values ​​of node number 1 and when running it will delete node number 1 and print the rest of the nodes.

 

Searching for a value within the list

Sometimes we want to search for a value or name inside the list for direct access in case it is big data, this is done by the contains function.

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<String> s=new LinkedList<String>();
        s.add("Parrot");
        s.add("Kaissar");
        s.add("Bashar");
        s.add("S49");
        if(s.contains("Bashar"))
            System.out.println("Found");
        else
            System.out.println("Not Found");
    }
}

In this example, we entered the if loop through the contains function and told him that if the name Bashar is present in the list, print to us that it exists, and if you do not find it, print to us that it does not exist.

 

Arrange a list of names in the list

To arrange names within lists, for example, by alphabet, we do this easily via Collections.sort for alphabetical order and for reverse order we use Collections.reverce.

1- Alphabetical order Collections.sort

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<String> s=new LinkedList<String>();
        s.add("Parrot");
        s.add("Kaissar");
        s.add("Bashar");
        s.add("S49");
    Collections.sort(s);
        System.out.println(s);
    }
}

We have created an array containing four elements and by using the Collections.sort function we have defined the object to it in order to arrange these elements in alphabetical order when the program is run.

2- Reverse order Collections.reverce

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<String> s=new LinkedList<String>();
        s.add("Parrot");
        s.add("Kaissar");
        s.add("Bashar");
        s.add("S49");
    Collections.sort(s);
        System.out.println(s);
    Collections.reverce(s);
        System.out.println(s);
    }
}

We have created a text array containing four elements and by using the Collections.reverce function and specifying the object to it it will sort the List elements in reverse, unlike the previous example.

 

Copying a list from another list

Let's learn here how to copy a list to another list and we will use the Collections.copy function in this.

package forech;
import java.util.LinkedList;
import java.util.List;
public class Forech {
    public static void main(String[] args) {
        List<String> C=new LinkedList<String>();
        C.add("Parrot");
        C.add("Kaissar");
        C.add("Bashar");
        C.add("S49");
         List<String> S=new LinkedList<String>();
          S.add("Parrot");
          S.add("Kaissar");
          S.add("Bashar");
          S.add("S49");
          Collections.copy(S, C)   /copy C in S
              System.out.println(S);    /print S
    }
}

Here we have created two lists, the first with its object C, and the second with its object S, each with different data. Then we use the Collections.copy function to copy the elements of the array whose object is C into the array. whose object is S and when run the contents of C will be erased and placed in S then. We print the contents of S to find the contents of object C.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com