0% found this document useful (0 votes)
2 views2 pages

Java Collections

The document contains Java code examples demonstrating the use of collections, including ArrayList, HashSet, and HashMap. It includes classes for listing fruits, managing a set of unique fruits, and implementing a simple calculator GUI. Each class has a main method to execute the functionality, showcasing how to add, display, and perform operations on the collections.

Uploaded by

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

Java Collections

The document contains Java code examples demonstrating the use of collections, including ArrayList, HashSet, and HashMap. It includes classes for listing fruits, managing a set of unique fruits, and implementing a simple calculator GUI. Each class has a main method to execute the functionality, showcasing how to add, display, and perform operations on the collections.

Uploaded by

prasath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] map.

put(1, "Apple");
[Link](2, "Banana");
package collections; [Link](3, "Orange");
import [Link];
public class ListExample { // To display
public static void [Link]("Map
main(String[] args) { Example:");
ArrayList<String> list = for ([Link]<Integer, String>
new ArrayList<>(); entry : [Link]()) {
[Link]("Apple");
[Link]("Banana");
[Link]("Orange"); [Link]([Link]() + ": " +
// to display [Link]());
[Link]("List }
Example:"); }
for (String fruit : list)
{ }

[Link](fruit); [Link]
}
} package collections;
}
public class CollectionsDemo {
[Link] public static void main(String[] args)
{
package collections; [Link](args);
import [Link]; [Link](args);
public class SetExample { [Link](args);
public static void main(String[] args) }
{ }
HashSet<String> set = new
HashSet<>(); javac -d . [Link]
[Link]("Apple"); javac -d . [Link]
[Link]("Banana"); javac -d . [Link]
[Link]("Orange"); javac -d . [Link]
[Link]("Apple"); // This won't be java [Link]
added since sets don't allow duplicates
// To display
[Link]("Set /* Program to create a Simple Calculator */
Example:"); import [Link].*;
for (String fruit : set) { import [Link].*;
[Link](fruit); public class MyCalculator extends Frame
} implements ActionListener {
} double num1,num2,result;
} Label lbl1,lbl2,lbl3;
TextField tf1,tf2,tf3;
[Link] Button btn1,btn2,btn3,btn4;
char op;
package collections; MyCalculator() {
import [Link]; lbl1=new Label("Number 1: ");
public class MapExample { [Link](50,100,100,30);
public static void main(String[] args)
{ tf1=new TextField();
HashMap<Integer, String> map = [Link](160,100,100,30);
new HashMap<>();
lbl2=new Label("Number 2: ");
[Link](50,170,100,30); [Link]([Link](result));
}
tf2=new TextField(); if([Link]() == btn2)
[Link](160,170,100,30); {
result = num1 - num2;
btn1=new Button("+");
[Link](50,250,40,40); [Link]([Link](result));
}
btn2=new Button("-"); if([Link]() == btn3)
[Link](120,250,40,40); {
result = num1 * num2;
btn3=new Button("*");
[Link](190,250,40,40); [Link]([Link](result));
}
btn4=new Button("/"); if([Link]() == btn4)
[Link](260,250,40,40); {
result = num1 /
lbl3=new Label("Result : ");
num2;
[Link](50,320,100,30);
[Link]([Link](result));
tf3=new TextField();
}
[Link](160,320,100,30);
}
[Link](this);
public static void main(String args[])
[Link](this);
{
[Link](this); MyCalculator calc=new
[Link](this); MyCalculator();
}
add(lbl1); add(lbl2);
}
add(lbl3);
add(tf1); add(tf2); add(tf3);
add(btn1); add(btn2);
add(btn3); add(btn4);

setSize(400,500);
setLayout(null);
setTitle("Calculator");
setVisible(true);

public void
actionPerformed(ActionEvent ae) {

num1 =
[Link]([Link]());
num2 =
[Link]([Link]());

if([Link]() == btn1)
{
result = num1 + num2;

You might also like