un

un

Collection API in java

Collections in java are the most important part of core java and it helps programmer to code with super ease.
The Java Collections Framework is a collection of interfaces and classes which helps in storing and processing the data efficiently. This framework has several useful classes which have tons of useful functions which makes a programmer task super easy.





So now forgot the theory, We will make you understand all the things and concept with the use of program.

Lets Start the journey ;)
========================================================================
public class M1
{
public static void main(String[] args)
{
int[] x = {10, 20, 30, 40}; // we cant change its size through its life
System.out.println(x);
x = new int[10];       //its not modifying same array, we defined a new array
System.out.println(x);
}
}
//Initially x is pointing to array of size 4 than next tym new array of 10, so array of line 7 will b abandoned
//we cant store multiple type of elements in same array
//manipulating of array is difficult (i.e for printing also u have to use arrays.tostring)

=================================================================
package com.lara;

import java.util.Arrays;

public class M2
{
public static void main(String[] args)
{
int[] x = {10, 20, 30, 40};        // we cant change its size through its life
System.out.println(Arrays.toString(x));
x = new int[10];                     //its not modifying same array, we defined a new array
System.out.println(Arrays.toString(x));
}
}

=======================================================================

package com.lara;

import java.util.Arrays;

public class M3 
{
public static void main(String[] args) 
{
int[] x = {12, 14, 6, 9, 15, 3};
System.out.println(Arrays.toString(x));
Arrays.fill(x, 1, 4, 100);                    //1 is inclusive  is exclusive
System.out.println(Arrays.toString(x));
}
}
//in order to fill frm specific number to specific number u can go for fill method
//it will print last argument to 1 to 3rd (cz 4th is exclusive) index

========================================================================
package com.lara;

import java.util.Arrays;

public class M4
{
public static void main(String[] args)
{
int[] x = {20, 3, 15, 1, 8, 5, 16, 4};
System.out.println(Arrays.toString(x));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
}
}

//we can sort the elements of an arrays by using sort
//default is ascending.

//for descending we shld go for wrapper type
========================================================================
package com.lara;

import java.util.Arrays;
import java.util.Collections;

public class M5 
{
public static void main(String[] args) 
{
Integer[] x = {20, 3, 15, 1, 8, 5, 16, 4};
System.out.println(Arrays.toString(x));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
Arrays.sort(x, Collections.reverseOrder());
System.out.println(Arrays.toString(x));
}
}

========================================================================
package com.lara;

import java.util.Arrays;

class A
{
int i;
A(int i)
{
this.i = i;
}
}
public class M6
{
public static void main(String[] args)
{
A[] x = {new A(90), new A(9), new A(0), new A(8)};
System.out.println(Arrays.toString(x));    //internally its calling toString method for every element
}

}
=======================================================================
package com.lara;

import java.util.Arrays;

class B
{
int i;
B(int i)
{
this.i = i;
}
public String toString()
{
return "i = " + i;
}
}
public class M7
{
public static void main(String[] args)
{
B[] x = {new B(9), new B(6), new B(0), new B(4)};
System.out.println(Arrays.toString(x));//internally its calling toString method for every element
}
}

/*
 * cz we overrided toString method its printing content
 */
=======================================================================
package com.lara;

import java.util.Arrays;

class C {
int i;

C(int i) {
this.i = i;
}

@Override
public String toString() {

return "i = " + i;
}
}

public class M8 {
public static void main(String[] args) 
{
C[] x = {new C(9), new C(10), new C(5)};
System.out.println(Arrays.toString(x));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
}

}
//ClassCastException, class can have any nmber of object
//while sorting, it dosent know on which attribute sorting should be done
========================================================================
package com.lara;

import java.util.Arrays;

class D implements Comparable
{
int i;
D(int i)
{
this.i = i;
}
public String toString()
{
return "i = " + i;
}
@Override
public int compareTo(Object o) 
{
return i - ((D)o).i;
}
}
public class M9 
{
public static void main(String[] args) 
{
D[] x = {new D(10), new D(1), new D(9), new D(4)};
System.out.println(Arrays.toString(x));
Arrays.sort(x); //internally its calling compareTo for evry object
System.out.println(Arrays.toString(x));
}
}

//for sorting derived data is in array we need to use compareTo
//over ride compareTo() method and define on which attribute sorting should be done
=======================================================================
package com.lara;

import java.util.Arrays;

class E implements Comparable
{
int i,j;
E(int i, int j)
{
this.i = i;
this.j = j;
}
@Override
public String toString() {
return "(" + i + "," + j + ")";
}
@Override
public int compareTo(Object o) 
{
return j - ((E)o).j;
}
}
public class M10 
{
public static void main(String[] args) 
{
E[] x = {new E(10,20), new E(1, 10), new E(4, 26)};
System.out.println(Arrays.toString(x));
Arrays.sort(x);// sorting is done based on j attribute(i.e compareTo method specified this)
System.out.println(Arrays.toString(x));
}
}
========================================================================
package com.lara;

import java.util.Arrays;

class F implements Comparable
{
int i, j, k;
F(int i, int j, int k)
{
this.i = i;
this.j = j;
this.k = k;
}
@Override
public String toString() {
return "(" + i + "," + j + "," + k + ")";
}
@Override
public int compareTo(Object o) {
return k - ((F)o).k;
}
}

public class M11 
{
public static void main(String[] args) {
//F[] x = {new F(1,2,0), new F(2,3,14), new F(3,4,8)};
F f1 = new F(1, 2, 3);
F f2 = new F(3, 0, 1);
F f3 = new F(0, 4, 10);
F f4 = new F(2, 20, 5);
F[] x = {f1, f2, f3, f4};
System.out.println(Arrays.toString(x));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
}
}
========================================================================
package com.lara;

import java.util.Arrays;

public class M12 
{
public static void main(String[] args) {
Integer i1 = new Integer(90);
Integer i2 = new Integer(9);
Integer i3 = new Integer(0);
Integer i4 = new Integer(4);
Integer[] x = {i1, i2, i3, i4};
System.out.println(Arrays.toString(x));
Arrays.sort(x); //internally its using comparable cz only 1 attribute
System.out.println(Arrays.toString(x));
}
}
//whn class is containung only 1 attribute thn we can use comparable
========================================================================
package com.lara;

import java.util.Arrays;

public class M14 
{
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("xyz");
StringBuffer sb2 = new StringBuffer("abc");
StringBuffer sb3 = new StringBuffer("test");
StringBuffer[] x = {sb1, sb2, sb3};
System.out.println(Arrays.toString(x));
Arrays.sort(x); //exception
System.out.println(Arrays.toString(x));
}
}

//classCastException
//comparable for StringBuffer is not implemented
========================================================================
package com.lara;

import java.util.Arrays;

public class M15 
{
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("xyz");
StringBuilder sb2 = new StringBuilder("abc");
StringBuilder sb3 = new StringBuilder("test");
StringBuilder[] x = {sb1, sb2, sb3};
System.out.println(Arrays.toString(x));
Arrays.sort(x); //exception
System.out.println(Arrays.toString(x));
}
}
//among all except StringBuilder & StringBuffer  implementing comparable
========================================================================
package com.lara;

import java.util.Arrays;

class G implements Comparable < G >
{
int i;
G(int i)
{
this.i = i;
}
@Override
public String toString() {
return "i = " + i;
}
@Override
public int compareTo(G o) {
return i - o.i;
}
}
public class M16 
{
public static void main(String[] args) 
{
G[] x = {new G(90), new G(9), new G(0), new G(10)};
System.out.println(Arrays.toString(x));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
}
}
========================================================================
package com.lara;

import java.util.Arrays;
import java.util.Comparator;

class J
{
int x, y;
J(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
static class A implements Comparator
{
@Override
public int compare(Object o1, Object o2) {
return ((J)o1).x - ((J)o2).x;
}
}
static class B implements Comparator
{
@Override
public int compare(Object o1, Object o2) {
return ((J)o1).y - ((J)o2).y;
}
}
}
public class M17 
{
public static void main(String[] args) 
{
J[] x = {new J(1, 0), new J(0, 10), new J(20, 30), new J(35, 18), new J(25, 0)};
System.out.println(Arrays.toString(x));
Arrays.sort(x, new J.A());//1 in arrays 2nd in comparator
System.out.println(Arrays.toString(x));
Arrays.sort(x, new J.B());
System.out.println(Arrays.toString(x));
}
}
//sorting can be done by any specific attribute, whichever attribute we are specifying in comparator
========================================================================
package com.lara;

import java.util.Arrays;
import java.util.Comparator;

class K
{
int p, q, r;
K(int p, int q, int r)
{
this.p = p;
this.q = q;
this.r = r;
}
@Override
public String toString() 
{
return "(" + p + "," + q + "," + r + ")";
}
}
public class M18 
{
public static void main(String[] args) 
{
K[] x = {
new K(10, 4, 20),
new K(20, 5, 1),
new K(5, 500, 100),
new K(25, 100, 0),
new K(15, 3, 25)
};
System.out.println(Arrays.toString(x));
Comparator c1 = new Comparator() //Anonymous inner class
{
@Override
public int compare(Object arg0, Object arg1) {
return ((K)arg0).p - ((K)arg1).p;
}
};
Arrays.sort(x, c1);//taking 2 elements from x, calling compare method for these 2
System.out.println(Arrays.toString(x));
Comparator c2 = new Comparator() //Anonymous inner class
{
@Override
public int compare(Object arg0, Object arg1) {
return ((K)arg0).q - ((K)arg1).q;
}
};
Arrays.sort(x, c2);
System.out.println(Arrays.toString(x));
Comparator c3 = new Comparator() //Anonymous inner class
{
@Override
public int compare(Object arg0, Object arg1) {
return ((K)arg0).r - ((K)arg1).r;
}
};
Arrays.sort(x, c3);
System.out.println(Arrays.toString(x));
}
}
========================================================================
package com.lara;

import java.util.Arrays;
import java.util.Comparator;

class L
{
int i, j, k;
L(int i, int j, int k)
{
this.i = i;
this.j = j;
this.k = k;
}
@Override
public String toString() {
return "(" + i + "," + j + "," + k + ")";
}
}
public class M19 
{
public static void main(String[] args) 
{
L[] x = {
new L(10, 20, 30),
new L(40, 10, 20),
new L(20, 5, 40),
new L(0, 25, 10),
new L(30, 15, 25),
};
System.out.println(Arrays.toString(x));
Arrays.sort(x, new Comparator<L>() 
{
@Override
public int compare(L arg0, L arg1) {
return arg0.i - arg1.i;
}
}
);
System.out.println(Arrays.toString(x));
Arrays.sort(x, new Comparator<L>() 
{
@Override
public int compare(L arg0, L arg1) {
return arg0.j - arg1.j;
}
}
);
System.out.println(Arrays.toString(x));
Arrays.sort(x, new Comparator<L>() 
{
@Override
public int compare(L arg0, L arg1) {
return arg0.k - arg1.k;
}
}
);
System.out.println(Arrays.toString(x));
}
}
=======================================================================
package com.lara;

import java.util.Arrays;

class M
{
int i, j, k;
M(int i, int j, int k)
{
this.i = i;
this.j = j;
this.k = k;
}
@Override
public String toString() {
return "(" + i + "," + j + "," + k + ")";
}
}

public class M20 
{
public static void main(String[] args) 
{
M[] x = {
new M(10,50, 60),
new M(20,10, 10),
new M(1,40, 30),
new M(30,20, 70),
new M(0,3, 10),
new M(15,30, 0),
};
System.out.println(Arrays.toString(x));
Arrays.sort(x, (m1, m2) -> m1.i - m2.i);
System.out.println(Arrays.toString(x));
Arrays.sort(x, (m1, m2) -> m1.j - m2.j);
System.out.println(Arrays.toString(x));
Arrays.sort(x, (m1, m2) -> m1.k - m2.k);
System.out.println(Arrays.toString(x));
}
}
========================================================================
package com.lara;

import java.util.Arrays;

public class M21 
{
public static void main(String[] args) 
{
String[] x = {"java", "abc", "xyz", "test", "hello" ,"j2ee" };
System.out.println(Arrays.toString(x));
int i = Arrays.binarySearch(x,"test");
System.out.println(i);
}
}
=======================================================================
package com.lara;

import java.util.Arrays;

public class M22 
{
public static void main(String[] args) 
{
String[] x = {"java", "abc", "xyz", "test", "hello" ,"j2ee" };
System.out.println(Arrays.toString(x));
Arrays.sort(x);
System.out.println(Arrays.toString(x));
int i = Arrays.binarySearch(x,"test");
System.out.println(i);
}
}
======================================================================
package com.lara;

import java.util.Arrays;
import java.util.Collections;

public class M23 
{
public static void main(String[] args) 
{
String[] x = {"java", "abc", "xyz", "test", "hello" ,"j2ee" };
System.out.println(Arrays.toString(x));
Arrays.sort(x, Collections.reverseOrder());
System.out.println(Arrays.toString(x));
int i = Arrays.binarySearch(x,"j2ee", Collections.reverseOrder());
System.out.println(i);
}
}

//collection api is available in java.utill package

/*
 *to overcome limitations to arrays, we use collection api
 *it can be divided into 4 part 
 * 1- List - maintains order in which elements are sorted
 * 2 - Queue - maintains first in first out
 * 3 - set - mainly for avoiding duplicates(allow only unique value, no duplicates allowed)
 * 4 - map - for sorting elements(value) with key.
 * 
 * frst three are collection type
 * map is not collection type but its a part of collection framwork
 * collection is interface, List Queue, set and map are also interface
 * 
 * List interface has 3 concrete class 
 * 1- ArrayList, 
 * 2- Vector
 * 3- LinkedList
 * 
 * Queue has -
 * LinkedList is implementing Queue also
 * Priority Queue is implementing only Queue type
 * 
 * Set has -
 * 1- HashSet(class)
 * 2- LinkedHashSet
 * 3- SortedSet(interface)
 * 
 * SortedSet has -
 *  NaviagableSet has -
 *   TreeSet
 *  
 *  under Map has - 
 *   HashMap (is a class)
 *   LinkedhashTable(is a class)
 *   LinkedhashMap(is a class) 
 *   SortedMap(is a interface)
*/


Note;- Comment your query in comment box

No comments

Theme images by nicodemos. Powered by Blogger.