Arrays.asList Concept With Example

Arrays.asList creates a List representation of Array.  This allows more flexibility to access the array.

Here are things to remember –

  1. Arrays.asList creates a wrapper on the Array and presents the Array as a List.
  2. If you change the element value in the List it will also get changed in the Array.
  3. You cannot add or remove any element from the ArrayList.

Here is the code example

package com.freetipscentral;
 
import java.util.Arrays;
import java.util.List;
 
public class ArraysAsList {
	public static void main(String args[])  {
		//Initialize the array
		String names[] = {"Vishy","Bony","Maddy"};
 
		System.out.println("Names in Array");
		for(String name : names) {
			System.out.println(name);
		}
 
		//Convert the names array to List
		List<String> nameList =  Arrays.asList(names);
 
		System.out.println("\nNames in List");
		for(String name : nameList) {
			System.out.println(name);
		}
 
		//Change the value in the second element in the List
		nameList.set(1, "Johnny");
		//Print the names in the array.  We can see that value also gets changed in the array also.
		System.out.println("\nNames in Array after the change in list");
		for(String name : names) {
			System.out.println(name);
		}
	}
}

If you try to add an element in the List

nameList.add(“Chris”);

You will get the following exception –

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at com.freetipscentral.ArraysAsList.main(ArraysAsList.java:32)

To make a copy of the Array so that you can make changes in the list declare the List in the following manner

//Convert the names array to List
List<String> nameList = new ArrayList<String>(Arrays.asList(names));

 

What Is The Difference Between ArrayList And HashSet

Difference between List and Set is one of the most common interview questions for Java.  In this post let us see practically what are the differences between two implementation classes of List and Set.  We are going to learn the differences between List and Set using concrete classes ArrayList and HashSet.

Practical differences between  List and Set

In the example below we are doing the following –

  1. Create a Book class.
  2. Create a ArrayList which is implementation of List interface.  Add four book objects including a duplicate book.
  3. Print the list and the size of the list.
  4. Then we create a HashSet which is implementation of Set interface.  Add four book objects including a duplicate book.
  5. Print the set and the size of the set.

What we will learn  –

  1. List maintains the order of insertion.  Which means the printing will be done in the order or insertion.  It also allows duplicates.
  2. Set does not maintains the order of insertion.  Which means the printing may not be done in the order or insertion.  Sets do not allow duplicates.

Book Class

package com.freetipscentral.domain;
 
public class Book {
 
String name;
int serialNumber;
 
public Book(String name, int serialNumber) {
this.name = name;
this.serialNumber = serialNumber;
}
 
public String getName() {
return name;
}
public void setNam(String name) {
this.name = name;
}
public int getserialNumber() {
return serialNumber;
}
public void setSerialNumber(int serialNumber) {
this.serialNumber = serialNumber;
}
 
public String toString() {
return serialNumber + " " + name;
}
 
}

ArrayListAndSet Class

package com.freetipscentral;
 
IMPORT java.util.ArrayList;
IMPORT java.util.HashSet;
IMPORT java.util.List;
IMPORT java.util.Set;
 
IMPORT com.freetipscentral.domain.Book;
 
public class ArrayListAndSet {
public static void main(STRING args[]) {
 
Book book1 = new Book("Harry Porter 1",1);
Book book2 = new Book("Harry Porter 2",2);
Book book3 = new Book("Harry Porter 3",3);
 
List<Book> books = new ArrayList<Book>();
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book2);
 
System.out.println("Books in the List \n");
FOR (Book book: books) {
System.out.println(book.toString());
}
System.out.println("\nThe number of books in the list -> "+books.size());
 
Book book4 = new Book("Lord Of The Rings",4);
Book book5 = new Book("Head First Java",5);
Book book6 = new Book("Java Made Easy",6);
Set<Book> bookSet = new HashSet<Book>();
bookSet.add(book4);
bookSet.add(book5);
bookSet.add(book5);
bookSet.add(book6);
 
System.out.println("\nBooks in the Set \n");
FOR (Book book: bookSet) {
System.out.println(book.toString());
}
System.out.println("\nThe number of books in the list -> "+bookSet.size());
 
}
}

Output of the above program

Books in the List
 
1 Harry Porter 1
2 Harry Porter 2
3 Harry Porter 3
2 Harry Porter 2
 
The number of books in the list -> 4
 
Books in the Set
 
6 Java Made Easy
4 Lord Of The Rings
5 Head First Java
 
The number of books in the list -> 3