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

Collection of Framework

The document provides examples of using various Java collection frameworks. It includes 7 examples: 1) Accepting employee names from the user and storing in a LinkedList, then displaying using an iterator. 2) Accepting student names from the user and storing in an ArrayList without duplicates, then sorting in ascending order. 3) Accepting employee details (name, salary) from the user and storing in a Hashtable, then displaying the highest paid employees. 4) Accepting integer elements from the user and storing in a HashSet, then sorting and displaying the elements. 5) Demonstrating use of a LinkedList and ListIterator to display the list normally and reversed. 6) Accepting student names and

Uploaded by

siddheshpatere77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Collection of Framework

The document provides examples of using various Java collection frameworks. It includes 7 examples: 1) Accepting employee names from the user and storing in a LinkedList, then displaying using an iterator. 2) Accepting student names from the user and storing in an ArrayList without duplicates, then sorting in ascending order. 3) Accepting employee details (name, salary) from the user and storing in a Hashtable, then displaying the highest paid employees. 4) Accepting integer elements from the user and storing in a HashSet, then sorting and displaying the elements. 5) Demonstrating use of a LinkedList and ListIterator to display the list normally and reversed. 6) Accepting student names and

Uploaded by

siddheshpatere77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Collection of framework

1)
Write a java program to accept n employee names from user, store them into
the LinkedList class and display them by using.
a. Iterator Interface

ListIterator Interface
package javacollection;

import java.util.*;

public class LinkedList8


{
public static void main(String[]args)

int n;
int i;
LinkedList list=new LinkedList();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the element Size :-");
n=sc.nextInt();
System.out.println("Enter employees name :-");
for( i=1;i<=n;i++)
{
String p=sc.next();
list.add(p);
}

//code for print the element in array


LinkedList s = list;
System.out.println("The Employee names is :- " +s);

System.out.println("The Employee names in iterator :- ");

Iterator<?> itr=list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}

private Iterator iterator() {


// TODO Auto-generated method stub
return null;
}

private void add(String p) {


// TODO Auto-generated method stub
}
}
2) Write a java program to read n Students names from user, store them into
the ArrayList collection. The program should not allow duplicate names. Display
the names in Ascending order.
package arraylist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.*;

public class arraylistprogram {


public static void main(String[] args) {
ArrayList student = new ArrayList();
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of students:-");
n=sc.nextInt();
System.out.println("Enter the Student names :-");

for (int i = 0; i < n; i++)


{

String t = sc.next();
student.add(t);

ArrayList s= student;
System.out.println("student name"+s);

Set<String> set = new HashSet<>(student);


student.clear();
student.addAll(set);
System.out.println("removing duplicate value: " +student);

Collections.sort(student);

System.out.println("The students in ascending order are: "


+student);

}
}

3) Write a java program to accept the details of ‘n’ employees (EName ,Salary)
from the user, store them into the Hashtable and displays the Employee Names
having maximum Salary.
package arraylist;
import java.util.*;

public class collection11 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Hashtable<String, Integer> employeeTable = new Hashtable<>();

System.out.print("Enter the number of employees: ");


int n = sc.nextInt();

for(int i=0; i<n; i++) {


System.out.print("Enter the name of employee "+(i+1)+": ");
String eName = sc.next();

System.out.print("Enter the salary of employee "+(i+1)+": ");


int salary = sc.nextInt();

employeeTable.put(eName, salary);
}

int maxSalary = Integer.MIN_VALUE;


ArrayList<String> maxSalaryEmployees = new ArrayList<>();
for(Map.Entry<String, Integer> entry : employeeTable.entrySet()) {
int salary = entry.getValue();
if(salary > maxSalary) {
maxSalaryEmployees.clear();
maxSalaryEmployees.add(entry.getKey());
maxSalary = salary;
} else if(salary == maxSalary) {
maxSalaryEmployees.add(entry.getKey());
}
}

System.out.println("Employees with the highest salary:");


for(String employee : maxSalaryEmployees) {
System.out.println(employee);
}
}
}

4)
import java.io.*;
import java.util.*;
public class collseta1 {

public static void main(String[] args) {

Set<Integer> set=new HashSet<Integer>();


BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int a[];
try
{
System.out.println("How many elements u want to accept
:");
int n=Integer.parseInt(br.readLine());
a=new int[n];
System.out.println("Enter the elements :");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
set.add(a[i]);
}

TreeSet sortedSet=new TreeSet<Integer>(set);


System.out.println("The sorted list is :");
System.out.println(sortedSet);
System.out.println("The first element of the set is
:"+(Integer)sortedSet.first());
System.out.println("The first element of the set is
:"+(Integer)sortedSet.last());
}
catch(Exception e)
{}
}
}

5)
import java.io.*;
import java.util.*;
public class collseta2 {

public static void main(String[] args) throws Exception{

int ch;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
LinkedList l=new LinkedList();
l.add("red");
l.add("blue");
l.add("yellow");
l.add("orange");
Iterator itr=l.iterator();
ListIterator litr=l.listIterator();
do
{
System.out.println("\n1:Display List");
System.out.println("2:Display List In Reveres Order");
System.out.println("3:Create New List");
System.out.println("4:Exit");
System.out.println("Enter your choice :");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Content of list:");
while(itr.hasNext())
{
String e=(String)itr.next();
System.out.println(e);
}
break;
case 2:
System.out.println("Content of list:");
System.out.println(l);
System.out.println("Content of list in reverse
order :");
while(litr.hasNext())
{
String e=(String)litr.next();
}
while(litr.hasPrevious())
{
//String e=(String)itr.next();
System.out.println(litr.previous());
}
break;
case 3:
System.out.println("\nudating link list item :");
l.set(0,"red");
l.set(1,"blue");
l.set(2,"pink");
l.set(3,"green");
l.add("yellow");
l.add("orange");
System.out.println(l);
break;
}
}while(ch!=4);
}

6) import java.io.*;
import java.util.*;
public class collseta3 {

public static void main(String[] args) throws Exception


{
String name;
double per;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Hashtable ht=new Hashtable();
System.out.println("How many records u want to enter :");
int n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("\nEnter the "+(i+1)+" student name and
percentage:");
name=br.readLine();
per=Double.parseDouble(br.readLine());
ht.put(name,per);
}
System.out.println("\n The hash table content is :");
System.out.println(ht);
System.out.println("\n Enter the student name u want to search:");
name=br.readLine();
System.out.println("\n The percentage of student "+name+" is
:"+ht.get(name));

7)

import java.io.*;
import java.util.*;
import java.util.Collections;
public class collseta11 {

public static void main(String[] args) {

Set<Integer> set=new HashSet<Integer>();


BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int a[];
try
{
System.out.println("How many elements u want to accept
:");
int n=Integer.parseInt(br.readLine());
a=new int[n];
System.out.println("Enter the elements :");
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
set.add(a[i]);
}

TreeSet sortedSet=new TreeSet<Integer>(set);


ArrayList AL=new ArrayList(set);
System.out.println("The sorted list is :");
System.out.println(sortedSet);
System.out.println("The first element of the set is
:"+(Integer)sortedSet.first());
System.out.println("The first element of the set is
:"+(Integer)sortedSet.last());
System.out.println("Enter the element u want to search :");
int no=Integer.parseInt(br.readLine());
int index=Collections.binarySearch(AL,no);
System.out.println("The element is present at "+index+"
position");
}
catch(Exception e)
{}

}
}

8) import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class collsetb1 extends JFrame implements ActionListener
{
java.awt.List l1;
JTextField t1,t2,t3,t4;
JButton b1,b2,b3;
Hashtable HT;
collsetb1()
{
super("collsetb1");
setSize(600,400);
setLayout(null);
setLocation(200,200);

l1=new java.awt.List(15);
t1=new JTextField();
t2=new JTextField();
t3=new JTextField();
t4=new JTextField();
b1=new JButton("Add");
b2=new JButton("Remove");
b3=new JButton("Search");

add(l1); add(t1); add(t2); add(t3);


add(t4); add(b1); add(b2); add(b3);

l1.setBounds(100,100,150,110);
t1.setBounds(260,100,100,20);
t2.setBounds(370,100,100,20);
b1.setBounds(260,130,210,20);
t3.setBounds(260,160,100,20);
t4.setBounds(370,160,100,20);
b2.setBounds(260,190,100,20);
b3.setBounds(370,190,100,20);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

t1.setToolTipText("Enter City Name");


t2.setToolTipText("Enter City Code");
t3.setToolTipText("Enter City Name to remove from collection");
t4.setToolTipText("Enter City Name to Search");

setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

HT=new Hashtable();
t1.requestFocus();

public void actionPerformed(ActionEvent e)


{
if(e.getSource()==b1)
{
if(t1.getText().length()==0 && t2.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"Enter city
name and code");
t1.requestFocus();
}
else
{
if(!HT.containsKey(t1.getText()))
l1.add(t1.getText()+" "+t2.getText());

HT.put(t1.getText(), new
Integer(Integer.parseInt(t2.getText())));
t1.setText("");
t2.setText("");
t1.requestFocus();
JOptionPane.showMessageDialog(null,HT);
}

if(e.getSource()==b2)
{
if(t3.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"Enter city
name to remove");
t3.requestFocus();
}
else
{
if(HT.containsKey(t3.getText()))
HT.remove(t3.getText());
JOptionPane.showMessageDialog(null,HT);
}

if(e.getSource()==b3)
{
if(t4.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"Enter city
name to search");
t4.requestFocus();
}
else
{
JOptionPane.showMessageDialog(null,"Value
="+HT.get(t4.getText()));
}

}
}

public static void main(String args[])


{
new collsetb1();
}
}

You might also like