Commit 193ccb0e authored by Suresh Kumar's avatar Suresh Kumar

Collection-Framwork

parent 6c965654
package collectionframewrok.com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Retrieve {
public static void main(String[] args) {
ArrayList<Student> al = new ArrayList<Student>();
Student s1 = new Student(1, "Asad Bhai ", 25);
Student s2 = new Student(2, "Suresh Kumar", 24);
Student s3 = new Student(3, "Rahyan Bhai", 26);
Student s4 = new Student(4, "Bilal Bhair", 27);
// Student s5 = new Student(5, "Suresh Kumar", 28);
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
// al.add(s5);
Comparator<Student> compareName = Comparator.comparing(Student::getName);
Collections.sort(al, compareName);
Comparator<Student> compareAge=null;
String str = null;
for (Student st : al) {
if (st.getName().toString().equals(str)) {
System.out.println("-------------- Sorting According To Student Age-------------");
compareAge = Comparator.comparing(Student::getAge);
Collections.sort(al, compareAge);
for(Student record: al){
System.out.println("\nId : ----------- " + record.getId() + "---------> Name : ----- " + record.getName() + "-------> Age : --------" + record.getAge());
}
}
str= st.getName();
}
if(compareAge==null){
System.out.println("------------------Sorting According to Student Name---------------");
for(Student std: al)
System.out.println("\nId : ----------- " + std.getId() + "---------> Name : ----- " + std.getName() + "-------> Age : --------" + std.getAge());
}
}
}
\ No newline at end of file
package collectionframewrok.com;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedList;
public class SearchInArrayListAndLinkedList {
public static void main(String [] args){
List <Integer> al = new ArrayList <Integer>();
for (int i=0; i<=1000; i++){
al.add(i);
}
System.out.println ("\nArraylist data is "+ al);
long startOne = System.nanoTime();
int indexOne = al.get(al.size()-1);
long endOne = System.nanoTime();
long durationOne = endOne-startOne;
System.out.print("\nReturn Operation is in ArrayLIst-------- " + indexOne + "---> ");
System.out.println("Execution Time Of Array List for get method ------- " + durationOne);
long startTwo = System.nanoTime();
int search1 = al.indexOf(10);
long endTwo = System.nanoTime();
System.out.print("Element " + search1 + " -------> Is Exist in Array List ---> ");
long durationTwo = startTwo-endTwo;
System.out.println("Execution Time Of Array List For Searching Exist is ------- " + durationTwo);
long startThree = System.nanoTime();
int search2 = al.indexOf(1011);
long endThree = System.nanoTime();
System.out.print("Element " + search2 + " -------> Is Not Exist in Array List ---> ");
long durationThree = startThree-endThree;
System.out.println("Execution Time Of Array List For Searching Not Exist is ------- " + durationThree);
// (----------------------------- Linked List --------------------------------)
List <Integer> ll = new LinkedList<Integer>();
for (int j=1; j <= 1000; j++){
ll.add(j);
}
System.out.println("\nLinkedlist data is "+ ll);
long startFour = System.nanoTime();
int indexFour = ll.get(ll.size()-1);
long endFour = System.nanoTime();
long durationFour = endFour-startFour;
System.out.print("\nReturn Operation is in Linked LIst ------- " + indexFour + "----> ");
System.out.print("Execution Time Of Linked List for get method ----- " + durationFour );
long startFive = System.nanoTime();
int search3 = al.indexOf(10);
System.out.print("\nIndex " + search3 + "--------> Is Exist in Linked List---> ");
long endFive = System.nanoTime();
long durationFive = startFive-endFive;
System.out.println("Execution Time Of Linked List For Searching Exist ------------ " + durationFive);
long startSix = System.nanoTime();
int search4 = al.indexOf(1011);
System.out.print("Index " + search4 + " -------- > Is Not Exist in Linked List----->");
long endSix = System.nanoTime();
long durationSix = startSix-endSix;
System.out.println(" Execution Time Of Linked List For Searching Not Exist --------------- " + durationSix);
if(durationOne>durationFour){
System.out.println("\n----------------Arraylist is Faster Than Linked List-------------");
}
else {
System.out.println("\n ---------------Linked List is Faster Than Array List-------------");
}
}
}
package collectionframewrok.com;
import java.util.ArrayList;
import java.util.*;
import java.util.Scanner;
public class SearchItem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] number = {2, 1, 3, 5, 0, 8, 7, 9, 10};
System.out.print("\nOrigional Number Means Before Sorting ------- > ");
for (int i = 0; i < number.length; i++) {
System.out.print(number[i] + " ");
}
System.out.print("\n"+"\nNow Number Are in Series Means After Sorting ------- > ");
for (int i=0; i<number.length; i++){
for (int j=i; j<number.length; j++){
int temp=0;
if (number[i] > number[j]){
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
}
System.out.print(number[i] + " ");
}
List <Integer> list = new ArrayList <Integer>();
for(int i=0 ; i<number.length; i++){
list.add(number[i]);
}
System.out.print("\n \nNow Let's Search The Object in ArrayList------(Search The Object Here) ------- > ");
int ob = sc.nextInt();
int indexAt = list.get(ob);
System.out.println("\n Your Object Position is ----- > " +indexAt);
System.out.println("\n-------------Search Object in the Arraylist using Method-----------");
}
}
\ No newline at end of file
package collectionframewrok.com;
public class Student {
int id;
String name;
int age;
Student(int id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
}
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