0% found this document useful (0 votes)
3 views

Untitled Document.b

Uploaded by

dekud9556
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Untitled Document.b

Uploaded by

dekud9556
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex no : 10 COLLECTIONS 09/10/2025

Aim:
To implement collections hierarchy classes in given programs.

1. Write a program to store contact names into LinkedList. And perform following operations.1.
Insert 2. Delete 3. update 4. display (use for-each loop and iterator) 5. sort (use Collections.
Sort
(obj));

Program :
package labcodes;
import java.util.*;
public class LinkedListDemo {

public static void main(String[] args) {


LinkedList<String> l= new LinkedList<>();
Scanner s=new Scanner(System.in);
System.out.println("enter the no of names\t");
int opt=s.nextInt();
for(int i=0;i<opt;i++)
{
System.out.println("enter\n");
String n= s.next();
l.add(n);
}
Iterator<?> i=l.iterator();
System.out.println("traversal using iterator");
while(i.hasNext())
{
System.out.println("name:"+i.next());
}
System.out.println("enter the name to delete\n");
String n= s.next();
l.remove(n);
System.out.println("after removing "+n+"\n");
i=l.iterator();

System.out.println("traversal using iterator");


while(i.hasNext())
{
System.out.println("name:"+i.next());
}
System.out.println("sorting:\n");
Collections.sort(l);
i=l.iterator();
System.out.println("traversal using iterator");
while(i.hasNext())
{
System.out.println("name:"+i.next());
}
}
}

Output:
enter the no of names
3
enter

Karthi
enter

Kavi
enter

Dinesh
traversal using iterator
name: karthi
name: kavi
name: dinesh
enter the name to delete

Kavi
after removing kavi

traversal using iterator


name: karthi
name:dinesh
sorting:

traversal using iterator


name: dinesh
name:karthi
2. Contact Management System
Program:
package labcodes;
import java.util.*;
public class ContactDetails {
String contactId;
String name;
String ph_no;
String email;
ContactDetails(String a,String b,String c,String d)
{
contactId=a;
name=b;
ph_no=c;
email=d;
System.out.println("hi");
}

public String dis() {


return "ContactDetails contactId=" + contactId + ", name=" + name + ", ph_no=" +
ph_no + ", email=" + email
+ "]";
}

public String getName() {


return name;
}

public String getPh_no() {


return ph_no;
}

public static void main(String a[])


{
int opt;
Scanner sc= new Scanner(System.in );
ArrayList<ContactDetails> l=new ArrayList<>();
do {
System.out.println("1.add\n2.remove\n3.edit\n4.search\n5.dis\n6.exit");
System.out.println("enter the option");
opt=sc.nextInt();
switch(opt)
{
case 1:
System.out.println("enter the contactid\n");
String n=sc.next();
System.out.println("enter the name\n");
String n1=sc.next();
System.out.println("enter the ph_no\n");
String n2=sc.next();
System.out.println("enter the email\n");
String n3=sc.next();
ContactDetails c=new ContactDetails (n,n1,n2,n3);
l.add(c);
break;
case 2:
System.out.println("enter the contact name\n");
String n4=sc.next();

for(int i=0;i<l.size();i++)
{ int i3=l.get(i).getName().compareTo(n4);
if((i3==0))
{
l.remove(l.get(i));
break;
}
}
break;
case 3:
System.out.println("enter the contact name to edit\n");
String n5=sc.next();
for(int i=0;i<l.size();i++)
{
int i2=l.get(i).getName().compareTo(n5);
if(i2==0)
{
System.out.println("enter the contactid\n");
String n11=sc.next();
System.out.println("enter the name\n");
String n12=sc.next();
System.out.println("enter the ph_no\n");
String n21=sc.next();
System.out.println("enter the email\n");
String n31=sc.next();
l.get(i).email=n31;
l.get(i).contactId=n11;
l.get(i).name=n12;
l.get(i).ph_no=n21;
break;
}
}
break;
case 4:
System.out.println("enter the contact name\n");
String n7=sc.next();

for(int i=0;i<l.size();i++)
{
int i1=l.get(i).getName().compareTo(n7);
if(i1==0)
{
System.out.println(l.get(i).dis());
break;
}
}
break;

case 5:
for(int i=0;i<l.size();i++)
{

System.out.println(l.get(i).dis());

}
break;
case 6:
break;

}
}while(opt<=6);
}
}
OUTPUT:

You might also like