Commit f6c550da authored by droja's avatar droja

Collection framework module

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CollectionFramework</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=16
package com.example.list.Arraylist;
import java.util.ArrayList;
import java.util.Arrays;
public class ArraylistSample2 {
public static void main (String[] args) {
String arr[]= {"Oppo A31","Iphone 8S","OnePlus"};
for(String value:arr) {
System.out.println(value);
}
ArrayList al = new ArrayList(Arrays.asList(arr));
System.out.println(al);
}
}
package com.example.list.Arraylist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Arraylistsample {
public static void main(String args[]) {
// Declare Arraylist
ArrayList al = new ArrayList();
ArrayList al1 = new ArrayList();
// ArrayList <String>al1 = new ArrayList();
// ArrayList <Integer>al2 = new ArrayList();
// List al3 = new ArrayList();
// add new elements to the arraylist
al.add(100);
al.add("Welcome");
al.add(15.5);
al.add(true);
System.out.println(al);
// Adding homogenious elemnets
al1.add("X");
al1.add("Y");
al1.add("Z");
al1.add("A");
al1.add("B");
al1.add("C");
al1.add("D");
// size()
System.out.println("Number of elements in array list:" + al.size());
// remove
al.remove(15.5);
al.remove("Welcome");
System.out.println(al);
System.out.println("after removing elements in array list:" + al.size());
// insert
al.add(2, "Python");
System.out.println("After insertion:" + al);
// retrieve specific element
System.out.println(al.get(0));
// change element
al.set(0, "Java");
System.out.println("After repalcing with a new vale" + al);
// search -conatins()
System.out.println(al.contains("Java"));
System.out.println(al.contains("Bigdata"));
// isEmpty()
System.out.println(al.isEmpty());
// 1)for loop
// System.out.println("Reading elements using for loop......");
//
// for(int i=0;i<=al.size();i++) {
// System.out.println( al.get(i));
// }
// for .. each loop
System.out.println("Reading elements using for..each loop");
for (Object e : al) {
System.out.println(e);
}
// iterator()
System.out.println("Reading elements using iterator method");
Iterator it = al.iterator();
while (it.hasNext()) {
System.out.println("Iterator::" + it.next());
}
// Adding a collection of elements in to another arrayList
al.addAll(al1);
System.out.println("after adding a collection of elements of one arraylist to another::" + al);
// removing elements
al.removeAll(al1);
System.out.println("after removing a collection of elements of one arraylist to another::" + al);
//Sort --- Collections.sort()
System.out.println("Elements in the array list "+al1);
Collections.sort(al1);
System.out.println("Elements in the array list after sorting"+al1);
Collections.sort(al1,Collections.reverseOrder());
System.out.println("Elements in the array list after sorting in reverse order:"+al1 );
//Shuffling -Collections .shuffels()
Collections.shuffle(al1);
System.out.println("Elements in the array list after Shuffling:"+al1 );
}
}
package com.example.list.Arraylist;
\ No newline at end of file
package com.example.list.Linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListSample {
public static void main(String args[]) {
// Declare Linked list
LinkedList l = new LinkedList();
LinkedList<Integer> l1 = new LinkedList<Integer>();
LinkedList<String> l2 = new LinkedList<String>();
// Add element into linked list
l.add(100);
l.add("Welcome");
l.add(15.5);
l.add('A');
l.add(true);
l.add(null);
System.out.println(l);
System.out.println(l.size());// 6
// remove
// l.remove(3);//index
System.out.println("After removing, new list:" + l);// [100, Welcome, 15.5, true, null]
// Inserting/adding element in the middle of the linked list
l.add(3, "Java");
System.out.println("After inserting elements:" + l);// [100, Welcome, 15.5, Java, A, true, null]
//retriving value /object
System.out.println(l.get(3));
//change the value
l.set(5, "X");
System.out.println("After cahnging the value:"+l);//[100, Welcome, 15.5, Java, A, X, null]
//contains()
System.out.println(l.contains("Java"));
System.out.println(l.contains("Python"));
//isEmpty()
System.out.println(l.isEmpty());//false
//Reading elements from LL using for loop
for(int i = 0;i<l.size();i++)
{
System.out.println(l.get(i));
}
//Reading elements from LL using for ..each loop
for(Object e:l)
{
System.out.println(e);
}
//iterator() method
Iterator it = l.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
package com.example.list.Linkedlist;
import java.util.LinkedList;
public class LinkedlistDemo2 {
public static void main(String[] args)
{
LinkedList l = new LinkedList();
l.add("X");
l.add("Y");
l.add("Z");
l.add("A");
l.add("B");
l.add("C");
LinkedList new_l= new LinkedList();
//new_l.addAll(c);
}
}
package com.example.list.Linkedlist;
\ No newline at end of file
package com.example.Queue;
//Java program to demonstrate a Queue
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueDemo {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
Queue<String> pq = new PriorityQueue<>();
// Adds elements {0, 1, 2, 3, 4} to
// the queue
for (int i = 0; i < 5; i++)
q.add(i);
// Adds elements to pq queue
pq.add("Geeks");
pq.add("For");
pq.add("Geeks");
System.out.println(pq);
// Display contents of the queue.
System.out.println("Elements of queue " + q);
// To remove the head of queue.
int removedele = q.remove();
System.out.println("removed element-" + removedele);
System.out.println(q);
// To view the head of queue
int head = q.peek();
System.out.println("head of queue-" + head);
// Rest all methods of collection
// interface like size and contains
// can be used with this
// implementation.
int size = q.size();
System.out.println("Size of queue-" + size);
}
}
package com.example.Queue;
\ No newline at end of file
package com.example.list.Arraylist;
import java.util.ArrayList;
import java.util.Arrays;
public class ArraylistSample2 {
public static void main (String[] args) {
String arr[]= {"Oppo A31","Iphone 8S","OnePlus"};
for(String value:arr) {
System.out.println(value);
}
ArrayList al = new ArrayList(Arrays.asList(arr));
System.out.println(al);
}
}
package com.example.list.Arraylist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Arraylistsample {
public static void main(String args[]) {
// Declare Arraylist
ArrayList al = new ArrayList();
ArrayList al1 = new ArrayList();
// ArrayList <String>al1 = new ArrayList();
// ArrayList <Integer>al2 = new ArrayList();
// List al3 = new ArrayList();
// add new elements to the arraylist
al.add(100);
al.add("Welcome");
al.add(15.5);
al.add(true);
System.out.println(al);
// Adding homogenious elemnets
al1.add("X");
al1.add("Y");
al1.add("Z");
al1.add("A");
al1.add("B");
al1.add("C");
al1.add("D");
// size()
System.out.println("Number of elements in array list:" + al.size());
// remove
al.remove(15.5);
al.remove("Welcome");
System.out.println(al);
System.out.println("after removing elements in array list:" + al.size());
// insert
al.add(2, "Python");
System.out.println("After insertion:" + al);
// retrieve specific element
System.out.println(al.get(0));
// change element
al.set(0, "Java");
System.out.println("After repalcing with a new vale" + al);
// search -conatins()
System.out.println(al.contains("Java"));
System.out.println(al.contains("Bigdata"));
// isEmpty()
System.out.println(al.isEmpty());
// 1)for loop
// System.out.println("Reading elements using for loop......");
//
// for(int i=0;i<=al.size();i++) {
// System.out.println( al.get(i));
// }
// for .. each loop
System.out.println("Reading elements using for..each loop");
for (Object e : al) {
System.out.println(e);
}
// iterator()
System.out.println("Reading elements using iterator method");
Iterator it = al.iterator();
while (it.hasNext()) {
System.out.println("Iterator::" + it.next());
}
// Adding a collection of elements in to another arrayList
al.addAll(al1);
System.out.println("after adding a collection of elements of one arraylist to another::" + al);
// removing elements
al.removeAll(al1);
System.out.println("after removing a collection of elements of one arraylist to another::" + al);
//Sort --- Collections.sort()
System.out.println("Elements in the array list "+al1);
Collections.sort(al1);
System.out.println("Elements in the array list after sorting"+al1);
Collections.sort(al1,Collections.reverseOrder());
System.out.println("Elements in the array list after sorting in reverse order:"+al1 );
//Shuffling -Collections .shuffels()
Collections.shuffle(al1);
System.out.println("Elements in the array list after Shuffling:"+al1 );
}
}
package com.example.list.Arraylist;
\ No newline at end of file
package com.example.list.Linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListSample {
public static void main(String args[]) {
// Declare Linked list
LinkedList l = new LinkedList();
LinkedList<Integer> l1 = new LinkedList<Integer>();
LinkedList<String> l2 = new LinkedList<String>();
// Add element into linked list
l.add(100);
l.add("Welcome");
l.add(15.5);
l.add('A');
l.add(true);
l.add(null);
System.out.println(l);
System.out.println(l.size());// 6
// remove
// l.remove(3);//index
System.out.println("After removing, new list:" + l);// [100, Welcome, 15.5, true, null]
// Inserting/adding element in the middle of the linked list
l.add(3, "Java");
System.out.println("After inserting elements:" + l);// [100, Welcome, 15.5, Java, A, true, null]
//retriving value /object
System.out.println(l.get(3));
//change the value
l.set(5, "X");
System.out.println("After cahnging the value:"+l);//[100, Welcome, 15.5, Java, A, X, null]
//contains()
System.out.println(l.contains("Java"));
System.out.println(l.contains("Python"));
//isEmpty()
System.out.println(l.isEmpty());//false
//Reading elements from LL using for loop
for(int i = 0;i<l.size();i++)
{
System.out.println(l.get(i));
}
//Reading elements from LL using for ..each loop
for(Object e:l)
{
System.out.println(e);
}
//iterator() method
Iterator it = l.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
package com.example.list.Linkedlist;
import java.util.LinkedList;
public class LinkedlistDemo2 {
public static void main(String[] args)
{
LinkedList l = new LinkedList();
l.add("X");
l.add("Y");
l.add("Z");
l.add("A");
l.add("B");
l.add("C");
LinkedList new_l= new LinkedList();
//new_l.addAll(c);
System.out.println(l);
}
}
package com.example.list.Linkedlist;
\ No newline at end of file
package com.example.list.vector;
//Java Program to Demonstrate Working of Vector
//Via Creating and Using It
//Importing required classes
import java.util.*;
//main class
public class Vectordemo {
// Main driver method
public static void main(String[] args) {
// Size of the Vector
int n = 5;
// Declaring the Vector with
// initial size n
Vector<Integer> v = new Vector<Integer>(n);
// Case 1
// Creating a default vector
Vector v1 = new Vector();
// Appending new elements at
// the end of the vector
for (int i = 1; i <= n; i++)
v.add(i);
// Printing elements
System.out.println(v);
// Remove element at index 3
v.remove(3);
// Displaying the vector
// after deletion
System.out.println(v);
// iterating over vector elements
// usign for loop
for (int i = 0; i < v.size(); i++)
// Printing elements one by one
System.out.print(v.get(i) + " ");
// Adding custom elements
// using add() method
v1.add(1);
v1.add(2);
v1.add("Roja");
v1.add("Gauthami");
v1.add(3);
// Printing the vector elements to the console
System.out.println("Vector v1 is " + v1);
// Case 2
// Creating generic vector
Vector<Integer> v2 = new Vector<Integer>();
// Adding custom elements
// using add() method
v2.add(1);
v2.add(2);
v2.add(3);
v2.add(12);
v2.add(23);
v2.add(22);
v2.add(10);
v2.add(20);
// Printing the vector elements to the console
System.out.println("Vector v2 is " + v2);
// Displaying the Vector
System.out.println("Vector: " + v2);
// Using set() method to replace 12 with 21
System.out.println("The Object that is replaced is: " + v2.set(0, 21));
// Using set() method to replace 20 with 50
System.out.println("The Object that is replaced is: " + v2.set(4, 50));
// Displaying the modified vector
System.out.println("The new Vector is:" + v2);
// removing first occurrence element at 1
v2.remove(1);
// checking vector
System.out.println("after removal: " + v2);
}
}
package com.example.list.vector;
\ No newline at end of file
package com.example.set;
//Java program Illustrating Set Interface
//Importing utility classes
import java.util.*;
public class HashsetDemo {
// Main driver method
public static void main(String[] args) {
// Demonstrating Set using HashSet
// Declaring object of type String
Set<String> hash_Set = new HashSet<String>();
// Adding elements to the Set
// using add() method
hash_Set.add("Sam");
hash_Set.add("Jam");
hash_Set.add("Ram");
hash_Set.add("Wexer");
hash_Set.add("Dautron");
// Printing elements of HashSet object
System.out.println(hash_Set);
// Creating an object of Set class
// Declaring object of Integer type
Set<Integer> a = new HashSet<Integer>();
// Adding all elements to List
a.addAll(Arrays.asList(new Integer[] { 1, 3, 2, 4, 8, 9, 0 }));
// Creating an object of Set and
// declaring object of type String
Set<String> hs = new HashSet<String>();
// Elements are added using add() method
// Later onwards we wil show accessing the same
// Custom input elements
hs.add("A");
hs.add("B");
hs.add("C");
hs.add("A");
// Print the Set object elements
System.out.println("Set is " + hs);
// Again declaring object of Set class
// with reference to HashSet
Set<Integer> b = new HashSet<Integer>();
b.addAll(Arrays.asList(new Integer[] { 1, 3, 7, 5, 4, 0, 7, 5 }));
// To find union
Set<Integer> union = new HashSet<Integer>(a);
union.addAll(b);
System.out.print("Union of the two Set");
System.out.println(union);
// To find intersection
Set<Integer> intersection = new HashSet<Integer>(a);
intersection.retainAll(b);
System.out.print("Intersection of the two Set");
System.out.println(intersection);
// To find the symmetric difference
Set<Integer> difference = new HashSet<Integer>(a);
difference.removeAll(b);
System.out.print("Difference of the two Set");
System.out.println(difference);
// Declaring a string
String check = "D";
// Check if the above string exists in
// the SortedSet or not
// using contains() method
System.out.println("Contains " + check + " " + hs.contains(check));
// Removing custom element
// using remove() method
hs.remove("B");
// Printing Set elements after removing an element
// and printing updated Set elements
System.out.println("After removing element " + hs);
}
}
package com.example.set;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashsetdemo {
public static void main(String[] args) {
Set<String> lh = new LinkedHashSet<String>();
// Adding elements into the LinkedHashSet
// using add()
lh.add("India");
lh.add("Australia");
lh.add("South Africa");
// Adding the duplicate
// element
lh.add("India");
// Displaying the LinkedHashSet
System.out.println(lh);
// Removing items from LinkedHashSet
// using remove()
lh.remove("Australia");
System.out.println("Set after removing " + "Australia:" + lh);
// Iterating over linked hash set items
System.out.println("Iterating over set:");
Iterator<String> i = lh.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
package com.example.set;
//Java Program Demonstrating Creation of Set object
//Using the TreeSet class
//Importing utility classes
import java.util.*;
public class TreesetDemo {
// Main driver method
public static void main(String[] args) {
// Creating a Set object and declaring it of String
// type
// with reference to TreeSet
Set<String> ts = new TreeSet<String>();
// Adding elements into the TreeSet
// using add()
ts.add("India");
ts.add("Australia");
ts.add("South Africa");
// Adding the duplicate
// element
ts.add("India");
// Displaying the TreeSet
System.out.println(ts);
// Removing items from TreeSet
// using remove()
ts.remove("Australia");
System.out.println("Set after removing " + "Australia:" + ts);
// Iterating over Tree set items
System.out.println("Iterating over set:");
Iterator<String> i = ts.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
package com.example.set;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment