Saravanan's Corner: Blackberry Dev

Monday, 15 October 2018

Collection Framework

There are 9 key Interfaces are available in collection framework



1. Collection -

1. If you want to represent a group of an individual object as an entity, you should go for Collection

2 In general, the collection interface is considered a root interface of any collection interface

3. Define the most common methods which are applicable for any collection objects

4. There is no concrete class that implements the collection interface directly.

2. List Interface -

1. if you want to represent a group of an individual object as an entity where duplicates allowed and insert order is preserved

2. ArrayList, Linked List, Vector and Stacks are Implementation classes

Collections --> List --> ArrayList,
                                      Linked List,
                                      Vector-->Stacks
                                                                 
3. Collections came in 1.2

4. Vector came in 1.0 version which are legacy classes

3. SET

1. if you want to represent a group of individual objects as an entity where duplicates are not allowed and insert no order is preserved

2. Hash set, LinkedHashSet

Collections (1.2v)--> Set (1.2v)--> HashSet(1.2v) --> LinkedHashset (1.4)

Difference between SET and LIST

List - Duplicates allowed, Insertion order is preserved

SET - Duplicated not allowed, Insertion order is not preserved


4. SortedSet Interface

1. if you want to represent a group of individual objects as an entity where duplicates are not allowed and all objects inserted in sorting order

Collections (I)--> Set (I)--> SortedSet (I)(1.2v)

5. NavigableSet Interface

NavigableSet is child interface of SortedSet defines several methods for navigation purpose

Collections (I)--> Set (I)--> SortedSet (I)(1.2v) --> NavigableSet (I) (1.2v) --> TreeSet (Imp class) (1.2)

6. Queue Interface

If you want to represent a group of an individual object prior to processing then go for Queue

Queue Implementation classes: (V1.5)
 Priority Queue, Blocking Queue, Linked Blocking Queue, PriorityBlockingQueue

7. Map

If you want to represent a group of an individual object as a key-value pair then go for Map interface

- Map is not child interface of collection

- Duplicate keys are not allowed but values can be duplicate

Implementation classes:

Hashmap, LinkedHashmap, WeakHshMap, IdentityHashMap, HashTable, Properties, Dictionary

Map -> Hashmap -> LinkedHashmap (1.4v)

Map -> WeakHshMap (1.4v)

Map -> IdentityHashMap (1.4v)

Map -> HashTable
                                --> Properties (1.0v)
                                ---> Dictionary (1.0v)

8.SortedMap

If you want to represent a group of an individual object as a key-value pair with some sorted order of keys then go for SortedMap interface

Map --> SortedMap

9. NavigableMap

It defines several utility methods for navigation purpose

Map --> SortedMap --> Navigable Map (1.6v) --> TreeMap(Impl)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-----------------------------------------------------------------------------------------
               |  Implements   | Order | Random | Key-Value | Duplicate | Null | Thread |
-----------------------------------------------------------------------------------------
ArrayList      |      List     |    X  |    X   |           |     X     |   X  |        |
-----------------------------------------------------------------------------------------
Vector         |      List     |    X  |    X   |           |     X     |   X  |    X   |
-----------------------------------------------------------------------------------------
LinkedList     | List, Deque   |    X  |        |           |     X     |   X  |    X   |
-----------------------------------------------------------------------------------------
HashSet        |      Set      |    X  |    X   |      X    |     X     |   X  |        |
-----------------------------------------------------------------------------------------
TreeSet        |   SortedSet   |    X  |    X   |           |           |      |        |
-----------------------------------------------------------------------------------------
Stack          | Vector, List  |    X  |        |           |     X     |   X  |    X   |
-----------------------------------------------------------------------------------------
Properties     | Hashtable,Map |       |    X   |      X    |           |      |    X   |
-----------------------------------------------------------------------------------------
HashMap        |      Map      |       |    X   |      X    |           |   X  |        |
-----------------------------------------------------------------------------------------
TreeMap        |   SortedMap   |    X  |    X   |      X    |           |      |        |
-----------------------------------------------------------------------------------------
Hashtable      |      Map      |       |    X   |      X    |           |      |    X   |
-----------------------------------------------------------------------------------------

Where to use ArrayList in java

This is obvious that if ArrayList is the part of Collection Framework then it will give some sort of operation (store, retrieve, searching, sorting, insertion, deletion, and advance manipulation) on the Objects stored inside it.
  • ArrayList provides faster iteration so where faster iteration or index-based retrieval is required and there are not many additions or deletion operation takes place then it is recommended to use ArrayList in java.
  • ArrayList is slower in addition or removal data, so where more operation of addition and deletion is required then the use of ArrayList is not recommended.
        
        System.out.println("Following is the output ArrayList with for loop");
        for(String name : listName){  // for loop to iterate arraylist
             System.out.println(name); 
        
         
        System.out.println("Following is the output ArrayList with for loop");
         
        Iterator itr = listName.iterator();//getting Iterator from arraylist to iterate elements 
        while(itr.hasNext()){
            String name = itr.next();
            System.out.println(name); 
        }

ArrayList in java collections framework is defined as below -
  • ArrayList in java is used to store the java element or object.
  • ArrayList class extends AbstractList class and implements List interface.
  • ArrayList stores and iterates data in order to which they are inserted.
  • ArrayList class uses arrays to store the element or object internally.
  • ArrayList size grows automatically when elements or objects are added to it.
  • Java ArrayList allows random access of elements and java objects.
  • ArrayList class can store duplicate elements or java objects.
  • ArrayList class is not thread-safe. if thread safety is required we need to synchronize this.

Tuesday, 9 October 2018

PL-SQL

Procedure with cursor:
---------------------------

create or replace PROCEDURE               "PlaPROC" AS

CURSOR cr_update IS
select su.subscriberuser_id,
       su.subscriberuser_pid secondary_user_subuser_pid,
       su3.subscriberuser_pid default_subuser_pid,
       su3.subscriberuser_pid
       || substr(su.subscriberuser_pid, instr(su.subscriberuser_pid,'_'), 4) revised_subuser_pid
from subscriberuser su
join subscriberuser su3 on su.subscriber_id = su3.subscriber_id
where su.usertype_id = 2
and exists (select * from subscriberuser su2
            where su.subscriber_id = su2.subscriber_id
            and su2.subscriberuser_id between 10714703 and 10983638)
and su3.usertype_id = 1
and su.subscriberuser_id between 8617661 and 10714702
and not exists (select * from subscriberuser su4
                where su3.subscriberuser_pid
               || substr(su.subscriberuser_pid, instr(su.subscriberuser_pid,'_'), 4) = su4.subscriberuser_pid)
and su.subscriber_id not in (
1234,
5678);

counter NUMBER(9) := 0;
counter2 NUMBER(9) := 0;

BEGIN
//log
  DBMS_OUTPUT.PUT_LINE(to_char(sysdate,   'DD-MON-YYYY HH24:MI:SS') || ' Starting update to subscriber_pid in SUBSCRIBERUSER.');

  FOR cr_rec IN cr_update
  LOOP
    BEGIN
      counter := counter + 1;
      counter2 := counter2 + 1;

      UPDATE subscriberuser
      SET subscriberuser_pid = cr_rec.revised_subuser_pid
      WHERE subscriberuser_id = cr_rec.subscriberuser_id;

      IF counter2 = 10000 THEN
        COMMIT;
        counter2 := 0;
      END IF;

    END;
  END LOOP;

  COMMIT;

  DBMS_OUTPUT.PUT_LINE(to_char(sysdate,   'DD-MON-YYYY HH24:MI:SS') || ' - ' || counter || ' SUBSCRIBERUSER records updated');
  NULL;

END PlaPROC;


How to call a stored procedure from shell program?


sqlplus "userid/pwd" <<!
set serveroutput on size 1000000
exec my_storedProc;
!

Monday, 1 October 2018

Lambda Expressions in Java (from Function Interface)


Lambda Expressions are anonymous methods (methods without names) used to implement a method defined by a functional interface. 

public interface MyGeneric<T> {
public T compute(T t);
}

public interface Greetings {

public String GreetPeople(String str);

}

       public static void main(String args[]) {
Greetings morningGreeting = (str) -> "Good Morning " + str + "!";

Greetings eveningGreeting = (str) -> "Good Evening " + str + "!";

Greetings reverseString = (str) ->{
String result = "";
for(int i = str.length()-1; i >= 0; i--)
result += str.charAt(i);

return result;
};
System.out.println(morningGreeting.GreetPeople("Luis"));

System.out.println(eveningGreeting.GreetPeople("Sara"));

System.out.println(reverseString.GreetPeople("emocleW"));


MyGeneric<String> reverse = (str) -> {
String result = "";
for(int i = str.length()-1; i >= 0; i--)
result += str.charAt(i);

return result;
};
MyGeneric<Integer> factorial = (n) -> {
int result = 1;

for(int i=1; i <= n; i++)
result = i * result;

return result;
};

System.out.println(reverse.compute("sara"));
System.out.println(factorial.compute(40));

}