• No results found

pollLast(): remove & returns last element

In document Core_Java By Venu Kumaar (Page 146-155)

Iterator itr = c.iterator();

6) pollLast(): remove & returns last element

7) descendingSet(): returns the NavigationSet in reverse order /**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:NavigableSetDemo

.java

*/

import java.util.*;

class NavigableSetDemo {

public static void main(String args[]) {

TreeSet<Integer> t=new TreeSet<Integer>();

t.add(1000);

t.add(2000);

t.add(3000);

t.add(4000);

t.add(5000);

System.out.println("Tree Map" + t);

System.out.println("t.ceiling(2000)= " + t.ceiling(2000));

System.out.println("t.higher(2000)= " + t.higher(2000));

System.out.println("t.floor(3000)= " + t.floor(3000));

System.out.println("t.lower(3000)= " + t.lower(3000));

System.out.println("t.pollFirst()= " + t.pollFirst());

System.out.println("t.pollLast()= " + t.pollLast());

System.out.println("t.descendingSet()= " + t.descendingSet());

System.out.println("Tree Map" + t);

} }

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

/*

OUTPUT: javac NavigableSetDemo

Tree Map[1000, 2000, 3000, 4000, 5000]

t.ceiling(2000)= 2000 t.higher(2000)= 3000 t.floor(3000)= 3000 t.lower(3000)= 2000 t.pollFirst()= 1000 t.pollLast()= 5000

t.descendingSet()= [4000, 3000, 2000]

Tree Map[2000, 3000, 4000]

*/

Hash Set Tree Set Storage method:

hash table red black tree Space used:

O(n) O(n)

Put speed:

O(1) O(lg n) Iteration order:

Arbitrary Sorted Hash map Tree map

Storage method:

hash table red black tree Space used:

O(n) O(n)

Put speed:

O(1) O(lg n) Iteration order:

Arbitrary Sorted concrete implements description

||Om Shree Hanumathe Namaha||

Venu Kumaar.S collection

HashSet Set hash table

TreeSet SortedSet balanced binary tree ArrayList List resizable-array LinkedList List linked list Vector List resizable-array HashMap Map hash table

TreeMap SortedMap balanced binary tree Hashtable Map hash table

Queue (v1.5)

Queue(I) → PriorityQueue(c)

→ BlockingQueue(c) → PriorityBlockingQueue

→ LinkedBlockingQueue

Queue(I):

It is the child Interface of collection. if we want to represent a group of individual objects prior to processing then we should go for Queue.

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:PriorityQueueDemo

.java

*/

import java.util.*;

import java.io.*;

class PriorityQueueDemo {

public static void main(String[] args) throws IOException { PriorityQueue q = new PriorityQueue();

System.out.println("q.peek() :" + q.peek()); // null

// System.out.println(q.element()); //RE: NoSuchElementException for (int i = 0; i <= 10; i++) {

q.offer(i);

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

}

System.out.println("Priority Q: " + q);

System.out.println("q.poll() :" + q.poll()); // del and returns the head element System.out.println("After Poll Priority Q: " + q);

} }

/*

OUTPUT:

java PriorityQueueDemo q.peek() :null

Priority Q: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

q.poll() :0

After Poll Priority Q: [1, 3, 2, 7, 4, 5, 6, 10, 8, 9]

*/

PriorityQueue(C)

PriorityQueue is the DS to hold a group of individual objs prior to processing according to some priority.

The priority can be either default natural sorting order or customized sorting order.

If we are depending on default natural sorting compulsory objects should be homogeneous &

Comparable otherwise we will get classCastException.

If we are defining our own customized sorting by Comparator then the objects need not be Homogeneous and Comparable.

Duplicate objs are not allowed.

Insertion order is not preserved.

null insertion is not possible even as first element also.

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:PriorityQueueDemo1

.java

*/

import java.util.*;

import java.io.*;

class PriorityQueueDemo1 {

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

PriorityQueue q = new PriorityQueue(15, new MyComparator3());

System.out.println("q.peek() :" + q.peek()); // null

// System.out.println(q.element()); //RE NoSuchElementException q.offer("A");

q.offer("Z");

q.offer("L");

q.offer("B");

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

System.out.println("Priority Q: " + q);

} }

class MyComparator3 implements Comparator { public int compare(Object obj1, Object obj2) {

String s1 = (String) obj1;

String s2 = (String) obj2;

return s2.compareTo(s1);

} }

/*

OUTPUT:

java PriorityQueueDemo1 q.peek() :null

Priority Q: [Z, B, L, A]

*/

Package

Package:

It is an Encapsulation mechanism to group related classes and interfaces into a single module. The main purposes of packages are:

1. To resolve naming conflicts.

2. To provide security to the classes & interfaces, so that outside person can't access directly.

3. It improves modularity of the application.

java package

lang, io, net, util, awt, applet, sql, rmi, math, text javax package

swing, sql, xml, naming, transaction

There is one universally accepted convention to name packages i.e., to use internet domain name in reverse.

domain name in reverse.module name.submodule name.class name Ex: com.hdfcbank.loan.housingloan.Account

package com.venu.kumar;

public class TestPackage {

public static void main(String[] args) { System.out.println("Package demo");

} }

1. javac TestPackage.java → Generated class file will be placed in current working directory.

2. javac -d . TestPackage.java

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

-d

destination to place generated class files .

current working directory

Generated class file will be placed into corresponding package structure.

If specified package structure is not already available then this command itself will create that package structure.

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:TestPackage

.java

*/

package com.venu.kumar;

public class TestPackage {

public static void main(String[] args) { System.out.println("Package demo");

} }

/*

To compile:

javac -d . TestPackage.java

The generated class file is placed in corresponding package structure

-d

destination to place generated class file .

current working directory

TestPackage.class is placed in com

venu

kumar

TestPackage.class To Execute:

java com.venu.kumar.TestPackage output:

Package demo

*/

static imports:

This feature is introduced in java 5 to import static members of class By using this feature we can access all

non-private static members without using class name from other classes with in the package and

protected and public members from outside package class members without using class name.

syntax: import static packagename.classname.*;

It allows to call all static members of the class or

||Om Shree Hanumathe Namaha||

Venu Kumaar.S import static packagename.classname.staticmembername;

allows only to call the imported static member.

Ex: import p1.*;

We can access all classes from p1 package import p1.A;

We can access only class A from p1 package.

import static p1.A.*;

We can access all static members of class A from p1 package.

Using this import statement we cannot access non-static members, we can't create object, we can't develop subclass from A class. for this purpose we must also write import statement separately for accessing class A as import p1.A;

import static p1.A.a;

We can access only the static variable "a".If "a" is a non-static variable it leads to CE:cannot find symbol "static a"

import static p1.A.m1;

We can access only static method "m1".

Ex1:Program

package p2;

import static java.lang.System.*;

public class Test {

public static void main(String args[]) { out.println("Hi");

} }

/*Output:

java Test Hi

*/

Ex2:Progam

package p1;

public class Example {

public static int a = 10;

public int x = 20;

public static void m1() {

System.out.println(".p1.Example.m1().");

}

public void m2() {

System.out.println(".p1.Example.m2().");

} }

package p2;

||Om Shree Hanumathe Namaha||

Venu Kumaar.S

import static p1.Example.*;

public class StaticSample {

public static void main(String args[]) { // static int a=10;

// accessing static members

System.out.println("p1.Example.a...."+p1.Example.a);

m1();

// accessing static members with classname // System.out.println(Example.a);//CE:

// Example.m1();//CE:

// accessing non-static members // Example e=new Example();CE:

// System.out.printlm(e.x);

// e.m2();

} }

/*

Output:

java StaticSample p1.Example.a....10 .p1.Example.m1().

*/

package p2;

import p1.Example;

public class NonStaticSample {

public static void main(String args[]) { // accessing static members with classname

System.out.println("...Accessing static members....:");

System.out.println("Example.a...."+Example.a);

Example.m1();

// accessing non-static members

System.out.println("\n...Accessing non-static members....:");

Example e=new Example();

System.out.println("e.x...."+e.x);

e.m2();

} }

/*

Output:

java NonStaticSample ...Accessing static members....:

Example.a....10

||Om Shree Hanumathe Namaha||

Venu Kumaar.S .p1.Example.m1().

...Accessing non-static members....:

e.x....20

.p1.Example.m2().

*/

VarArgs 1.5v

VarArgs 1.5v: Until 1.4v we can't declare a method with variable number of args, if there is any change in number of arguments compulsory we should declare a new method. This approach increases length of the code & reduces readability.

To resolve these problem, var-arg method in 1.5v. Hence, from 1.5v onwards we can declare a method with variable number of args such type of methods are called var-arg methods.

We can declare var-arg method as : methodname(int... variable);

Ex: m1(int... x);

We can invoke this method by passing any number of int values include zero number also.

m1();

m1(10,20);

m1(10);

m1(10,20,30,40);

Internally var-arg method is implemented by using single dimensional arrays concept. Hence with in the var-arg method we can differentiate args by using index.

/**@author:Venu Kumar.S @date:Oct 6, 2012

* @fileName:TestVarArgs

.java

*/

public class TestVarArgs {

public static void sum(int... x) {

System.out.println("Summation...:");

int total = 0;

for (int y : x) { total += y;

System.out.println(total);

}

System.out.println("total..=" + total);

}

public static void main(String[] args) { System.out.println("Var Args Demo...");

sum();

sum(10);

sum(20, 30);

sum(40, 50, 60, 70);

} }

||Om Shree Hanumathe Namaha||

Venu Kumaar.S /*

OutPut:

Var Args Demo...

Summation...:

total..=0

Summation...:

10

total..=10

Summation...:

20 50

total..=50

Summation...:

40 90 150 220

total..=220

*/

In document Core_Java By Venu Kumaar (Page 146-155)