ASSIGNMENT NO 5
NAME # SAMUDDIN
REG NO # 15PWBCS0491
SECTION #B
LIST AND SET
DIFFRENCE
(1) :-
LIST IS AN ORDERED COLLECTION IT MAINTAIN THE INSERTION ORDER WHICH MEAN
UPON DISPLAYING THE LIST CONTENT IT WILL DISPLAY THE ELEMANT IN THE SAME
ORDER IN WHICH THEY GOT INSERTED INTO THE LIST.
SET IS AN UNORDERED COLLECTION. ITS DOESNOT MAINTAIN ANY ORDER.
THERE ARE FEW IMPLEMENTATION OF SET WHICH MAINTAIN THE ORDER.
(2) : -
LIST IMPLEMENTATION: ARRAYLIST,LINKED LIST.
SET IMPLEMENTATION : HASHSET ,TREE SET,LINKEDHASH SET.
(3): -
LIST ALLOWS ANY NUMBER OF NULL VALUES
SET CAN HAVE ONLY A SINGLE NULL VALUES AT MOST
(4): -
LIST ALLOWS YOU TO STORE DUPLICATE ELEMANT
SET NOT ALLOWS YOU TO STORE DUPLICATE ELEMANT
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arraylistcheck;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author Samuddin
*/
public class SetAndList{
public static void main( String[] args )
{
System.out.println("List example .....");
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("1");
for (String temp : list){
System.out.println(temp);
System.out.println("Set example .....");
Set<String> set = new HashSet<String>();
set.add("1");
set.add("2");
set.add("3");
set.add("4");
set.add("1");
set.add("2");
set.add("5");
for (String temp : set){
System.out.println(temp);
}
Array list
Array list: -
Array list: -
Array list object can be used to store a list of an object.
Java array list class uses a dynamic array for storing the element.
It extend abstract list class and implement list interface
Java array list class can contain duplicate elements
Java array list class maintains insertion order
Java array list class is non synchronized
Java array list class allow random access because array work at
the index basis
SOURCE
CODE
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arraylistcheck;
import java.util.ArrayList;
/**
* @author Samuddin
*/
public class ArrayListCheck {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<String> DeptList = new ArrayList<>();
DeptList.add("Computer Science");
DeptList.add("Computer System");
DeptList.add("Chemical");
DeptList.add("Software");
DeptList.add("Information Technology");
DeptList.add("Civil");
DeptList.add("Mechincal");
DeptList.add("Mining");
DeptList.add("Mechatronic");
DeptList.add("Electrical");
DeptList.add("Agriculture");
// Remove the Dept
System.out.println("Remove Mining Engineering "+DeptList.remove("Mining"));
System.out.println("Remove Civil Engineering "+DeptList.remove("Civil"));
System.out.println("Remove Agriculture Engineering "+DeptList.remove("Agriculture"));
//Check the Size of List (Mean How many Dept In List)
System.out.println("List Size "+DeptList.size());
//Check the List Some Dept
System.out.println("Cantain Mining Engineering "+DeptList.contains("Mining"));
// Location of Dept that what Index/List the Dept are Store
System.out.println("The Location of Comuter System "+DeptList.indexOf("Computer
System"));
//Check the List is Empty or Not
System.out.println("Check the List is Empty "+DeptList.isEmpty());
// Add new Dept To lst
System.out.println("Add the New Dept to List "+DeptList.add("Basic Scince"));
// Remove The Dept using Index
System.out.println("Remove the Dept using Index "+DeptList.remove(4));
//Display The List
System.out.println("Display the List "+DeptList.toString());
// Display The List In Reverse Order
for(int i=DeptList.size()-1;i>=0;i--){
System.out.println(DeptList.get(i)+"");
System.out.println();