List interface

List in Java

List Interface in Java

  • It cares about the position of the element.
  • An element can be added in any position in the list.
  • By default in list the position of new element is last if you not specify its position.
  • The same element is allowed in the list multiple times. 

Note : At end git link is there for reference

Defining List in Java

By using List.of() method list becomes immutable, so we cannot add any new element in it. To make it mutable we use arrayList

package listInterface;

import java.util.List;

public class ListClass {

	public static void main(String[] args) {
		// Defining the list
		List<String> alphabets = List.of("A", "B", "C", "D");
		System.out.println(alphabets);

	}

}

Array List in Java

  • Presents since 1.2 version of java
  • Traversing is fast as it works on indexes.
  • Adding and deleting elements is time-consuming.
  • Suitable when there is less insertion and deletion and we want to access elements using their position.
  • Most method of the vector is not Synchronized i.e. it is not thread-safe.
  • If you don’t worry about thread safety, then go with vectors.

Linked List in Java

  •  Traversing is time-consuming.
  • Adding and Deleting any element is fast.
  • Going to any specific position is time-consuming.
  • Suitable when there is more addition and deletion of elements and less accessing of elements.

Vector List in Java

  • Presents since 1st version of Java.
  • Most method of the vector is Synchronized i.e it is thread-safe. 
  • Almost time-constant access.
  • If you worry about thread safety, then go with vectors.

Class Using List in Java

  • Create class student.
  • Having fields Id and name

 

package listInterface;

import java.util.List;

public class ListClass {

	public static void main(String[] args) {
		// Defining the list
		List<String> alphabets = List.of("A", "B", "C", "D");
		System.out.println(alphabets);

	}

}

Class to run Student class

  • Create class StudentClassRunner.
package listInterface;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

// class to custom sorting
class DescendingStudentComparator implements Comparator<Student> {
	@Override
	public int compare(Student stu1, Student stu2) {

		return Integer.compare(stu2.getId(), stu1.getId());
	}

}

public class StudentClassRunner {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Student> students = List.of(new Student(3, "Shubham"), new Student(1, "Ram"), new Student(2, "Krishn"));
		List<Student> studentsAl = new ArrayList<>(students);

		System.out.println(students.toString());
		System.out.println(studentsAl);

		// Comparable interface
		Collections.sort(studentsAl);
		System.out.println(" After sorting Ascending order: ");
		System.out.println(studentsAl);
		// descending order

		// Collections.sort(studentsAl, new DescendingStudentComparator());
		// we can call above method also
		// or directly we call the sort method
		studentsAl.sort(new DescendingStudentComparator());
		System.out.println("After sorting DescendingStudentComparator order: ");
		System.out.println(studentsAl);

	}

}

Leave A Comment

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