Collections concept in Java

download Collections concept in Java

of 12

Transcript of Collections concept in Java

  • 8/21/2019 Collections concept in Java

    1/27

    Collection

    List Set

    SortedSet

    1D Collection FrameWork Interfaces

  • 8/21/2019 Collections concept in Java

    2/27

    Methods in java.util.Collection•public int sie!"•public boolean is#mpt$!"•public boolean add!%bject"

    - used to add elements to any CFW variable only at the end.- Returns TRUE -> when Collection and List interfaces are used.- Returns False -> when et and ortedet interfaces are used if

    du!licate elements are entered.•&ublic boolean add'll!Collection"

    - "ddin# the contents of one CFW variable to another  $which is referred as "da!tability- one of the advanta#esof CFW%.•&ublic %bject() to'rra$!"  - Used for convertin# all the elements of CFW in the form of array

    ob&ect of &ava.lan#.ob&ect.

  • 8/21/2019 Collections concept in Java

    3/27

    et o s n ava.ut . st•&ublic void add!int*%bject"

    -Used for addin# an element to any CFW variable either inbe#inini#'end'middle.

    #+ample,  -(nsertin# element at the end of the list

    l.add$l.si)e$%*new (nte#er$+,,%%•public %bject et!int"

    - used to obtain a value from the CFW variable by !assin# the!osition.

    #+ample,b&ect ob&/l.#et$0%ystem.out.!rintln$ob&%

    /%0#, If the position entered is ron then 2null3 isreturned.•

    public %bject remove!int"•public void remove!%bject"

    -Used to remove the values for a CFW variable based on positionor content.

    -If removal is based on content onl$ the 4rst occurance is

    removed.•&ublic void remove'll!"

  • 8/21/2019 Collections concept in Java

    4/27

    Methods in java.util.Set

    • "s et is the derived from thecollection interface* it inherits all themethods of collection.

    • E1ce!t the basic di2erence i.eUni3ue elements are allowed and theelements are dis!layed in random

    order.

  • 8/21/2019 Collections concept in Java

    5/27

     5ava.util.SortedSet• ame as et interface but the elements

    are dis!layed in sorted order.• 4ublic b&ect 5rst$%

    • 4ublic b&ect last$%

    • 4ublic ortedet headet$b&ect ob&%

    • 4ublic ortedet subet$b&ect ob&+*

    b&ect ob&0%

    • 4ublic ortedet tailet$b&ect ob&%

    0aret%bject

     6i78obj

    0aret%bject

      6i 9ob

    %bj178 6i7

    %bj:

    6i  represents value of

    SortedSet

  • 8/21/2019 Collections concept in Java

    6/27

    E6"74LE

    1;

    :;

    ;

    ?;

    @;

    A;

    B;

    Sorted Set hs8ss.headSet!ne Inteer!

  • 8/21/2019 Collections concept in Java

    7/27

    e standard Collection classes

  • 8/21/2019 Collections concept in Java

    8/27

    0he 'rra$List Class

     The"rrayListclass e1tends"bstractListand im!lementstheListinterface."rrayListis a

    #eneric class that has this declaration8

    class 'rra$List7#9

    9ere*Es!eci5es the ty!e of ob&ects that the list will hold.

  • 8/21/2019 Collections concept in Java

    9/27

    im!ort &ava.util.:class 'rra$ListDemo ;!ublic static void main$trin# ar#s$%

    ystem.out.!rintln$?(nitial si)e of al8 ? @ al.si)e$%%'' "dd elements to the array list.al.add$?C?%al.add$?"?%al.add$?E?%al.add$?A?%al.add$?B?%al.add$?F?%al.add$+* ?"0?%ystem.out.!rintln$?i)e of al after additions8 ? @ al.si)e$%%'' Bis!lay the array list.ystem.out.!rintln$?Contents of al8 ? @ al%'' Remove elements from the array list.al.remove$?F?%al.remove$0%ystem.out.!rintln$?i)e of al after deletions8 ? @al.si)e$%%

    ystem.out.!rintln$?Contents of al8 ?@ al%

    0he output from this proram isshon here,(nitial si)e of al8 ,i)e of al after additions8 D

    Contents of al8

  • 8/21/2019 Collections concept in Java

    10/27

    %btainin an 'rra$ from an 'rra$List

    When worin# with "rrayList* you will sometimes want to obtain an actualarray that containsthe contents of the list. Gou can do this by callin# to"rray$ %* which is de5ned

    by Collection.everal reasons e1ist why you mi#ht want to convert a collection into anarray* such as8H 0o obtain faster processin times for certain operations 0o pass an arra$ to a method that is not overloaded to accept acollection

    0o interate collection-based code ith leac$ code that does notunderstand collections

    %bject( ) to'rra$! "709 0( ) to'rra$!0 arra$( )"

  • 8/21/2019 Collections concept in Java

    11/27

    Convert an 'rra$List into an arra$.im!ort &ava.util.:class "rrayListTo"rray ;!ublic static void main$trin# ar#s'' "dd elements to the array list.al.add$+%al.add$0%al.add$I%al.add$J%

    ystem.out.!rintln$?Contents of al8 ? @ al%'' Ket the array.(nte#er ia

  • 8/21/2019 Collections concept in Java

    12/27

    0he LinkedList Class

     TheLinedListclass e1tends"bstracte3uentialListand im!lementstheList*Be3ue* andueueinterfaces.

    (t !rovides a lined-list data structure.LinedListis a #eneric class that

    has this declaration8

    class LinkedList7#9

  • 8/21/2019 Collections concept in Java

    13/27

    im!ort &ava.util.:class LinkedListDemo ;!ublic static void main$trin# ar#s ll / new LinedListtrin#>$%

    '' "dd elements to the lined list.ll.add$?F?%ll.add$?A?%ll.add$?B?%ll.add$?E?%ll.add$?C?%

    ll.addLast$?M?%ll.addFirst$?"?%ll.add$+* ?"0?%ystem.out.!rintln$?ri#inal contents of ll8 ? @ ll%'' Remove elements from the lined list.ll.remove$?F?%

    ll.remove$0%ystem.out.!rintln$?Contents of ll after deletion8 N@ ll%'' Remove 5rst and last elements.ll.removeFirst$%ll.removeLast$%ystem.out.!rintln$?ll after deletin# 5rst and last8 N@ ll%

    '' Ket and set a value.

    trin# val / ll.#et$0%ll.set$0* val @ ? Chan#ed?%ystem.out.!rintln$?ll afterchan#e8 ? @ll%

    0he output from this proram isshon here,ri#inal contents of ll8

  • 8/21/2019 Collections concept in Java

    14/27

    0he GashSet Class9ashete1tends"bstractetand im!lements theetinterface. (t creates acollection thatuses a hash table for stora#e.9ashetis a #eneric class that has thisdeclaration8

    class GashSet7#9

    9ere*Es!eci5es the ty!e of ob&ects that the set will hold.

    LinkedGashSet

    (t maintains a lined list of the entries in the set* in the order in whichthey were inserted.class LinkedGashSet7#9

    0he 0reeSet Class

     Treeete1tends"bstractetand im!lements theOavi#ableetinterface. (tcreates acollection that uses a tree for stora#e. b&ects are stored in sorted*ascendin# order.

    class 0reeSet7#9

  • 8/21/2019 Collections concept in Java

    15/27

    Demonstrate GashSet.im!ort &ava.util.:class 9ashetBemo ;!ublic static void main$trin# ar#s hs / new 9ashettrin#>$%'' "dd elements to the hash set.hs.add$?A?%hs.add$?"?%hs.add$?B?%

    hs.add$?E?%hs.add$?C?%hs.add$?F?%ystem.out.!rintln$hs%

    0he folloin is the output from thisproram,class GashSet7#9(D* '* F* C* H* #)

    class LinkedGashSet7#9(H* '* D* #* C* F)hich is the order in hich the elementsere inserted.class 0reeSet7#9('* H* C* D* #* F)

  • 8/21/2019 Collections concept in Java

    16/27

    (terator (nterface• (f we want to dis!lay each element of the

    Collection* we can use one of these two!rocedures8

    •  ne way to do this is to em!loy aniterator*which is an ob&ect that im!lementseither the Iterator or

      the ListIterator interface.

    • Iterator enables you to cycle throu#h a

    collection* obtainin# or removin#elements.

    • ListIterator e1tends Iterator to allowbidirectional traversal of a list* and themodi5cation of elements.

  • 8/21/2019 Collections concept in Java

    17/27

    7ethods de5ned by (terator

  • 8/21/2019 Collections concept in Java

    18/27

    7ethods de5ned byList(terator

  • 8/21/2019 Collections concept in Java

    19/27

    E1am!le of (terator

    (terator itr/c.iterator$%While$itr.hasOe1t$%% Forward

    Birection

    ; itrb&ect ob&/itr.ne1t$%

      (nte#er io/$(nte#er%ob&

      int 1/io.intPalue$%  ystem.out.!rintln$1%

    +,

    0, I, J,

  • 8/21/2019 Collections concept in Java

    20/27

    E1am!le of List(teratorList(terator litr/l.List(terator$%

    while$litr.hasOe1t$%% ;

      b&ect ob&/litr.ne1t$%  ystem.out.!rintln$ob&%

    while$litr.has4revious$%%;

      b&ect ob&/litr.!revious$%

      (nte#er io/$(nte#er%ob&

      (nt n/io.intPalue$%

      ystem.out.!rintln$ob&%

     

     

    '' Bemonstrate iterators

  • 8/21/2019 Collections concept in Java

    21/27

    '' Bemonstrate iterators.im!ort &ava.util.:class (teratorBemo ;!ublic static void main$trin# ar#s$%'' "dd elements to the array list.al.add$?C?%al.add$?"?%al.add$?E?%al.add$?A?%

    al.add$?B?%al.add$?F?%'' Use iterator to dis!lay contents of al.ystem.out.!rint$?ri#inal contents of al8 ?%(teratortrin#> itr / al.iterator$%while$itr.hasOe1t$%% ;

    trin# element / itr.ne1t$%ystem.out.!rint$element @ ? ?%ystem.out.!rintln$%'' 7odify ob&ects bein# iterated.List(teratortrin#> litr / al.list(terator$%

    while$litr.hasOe1t$%% ;trin element / litr.ne1t

    litr.set$element @ ?@?%ystem.out.!rint$?7odi5ed contentsof al8 ?%itr / al.iterator$%

    while$itr.hasOe1t$%% ;trin# element / itr.ne1t$%ystem.out.!rint$element @ ? ?%ystem.out.!rintln$%'' Oow* dis!lay the list bacwards.ystem.out.!rint$?7odi5ed listbacwards8 ?%while$litr.has4revious$%% ;trin# element / litr.!revious$%ystem.out.!rint$element @ ? ?%

    ystem.out.!rintln$%0he output is shon here,

    %riinal contents of al, C ' # H D FModi4ed contents of al, C ' #H D F

    Modi4ed list backards, F D H

  • 8/21/2019 Collections concept in Java

    22/27

    0B Collection Framewor

    7"4

    •  The 9ash7a! Class

    •  The 9ash7a! class e1tends "bstract7a! andim!lements the 7a! interface. (t uses a hash

      table to store the ma!.

     This allows the e1ecution time of #et$ %and !ut$ %to remain constant even for lar#e sets.

      9ash7a! is a #eneric class that has thisdeclaration8

    • class 9ash7a!Q* P>

    • 9ere*Q s!eci5es the ty!e of eys* and P s!eci5esthe ty!e of values.

  • 8/21/2019 Collections concept in Java

    23/27

    7"4 (OTERF"CE

    •  ava.util.7a!

    •  ava.util.7a!.Entry

     ava.util.orted7a!

    im!ort &ava util :

  • 8/21/2019 Collections concept in Java

    24/27

    im!ort &ava.util.:class GashMapBemo ;!ublic static void main$trin# ar#s hm / new 9ash7a!trin#* Bouble>$%

    '' 4ut elements to the ma!hm.!ut$?ohn Boe?* new Bouble$IJIJ.IJ%%hm.!ut$?Tom mith?* new Bouble$+0I.00%%hm.!ut$?ane Aaer?* new Bouble$+IDS.,,%%hm.!ut$?Tod 9all?* new Bouble$.00%%hm.!ut$?Ral!h mith?* new Bouble$-+.,S%%

    '' Ket a set of the entries.et7a!.Entrytrin#* Bouble>> set / hm.entryet$%'' Bis!lay the set.for$7a!.Entrytrin#* Bouble> me 8 set% ;ystem.out.!rint$me.#etQey$% @ ?8 ?%ystem.out.!rintln$me.#etPalue$%%

    ystem.out.!rintln$%'' Be!osit +,,, into ohn Boes account.double balance / hm.#et$?ohn Boe?%hm.!ut$?ohn Boe?* balance @ +,,,%ystem.out.!rintln$?ohn Boes new balance8 ? @

    hm.#et$?ohn Boe?%%

    %utput from this proram isshon here !the precise orderma$ var$",Ral!h mith8 -+.,S

     Tom mith8 +0I.00 ohn Boe8 IJIJ.IJ Tod 9all8 .00 ane Aaer8 +IDS., ohn BoeVs new balance8 JJIJ.IJ

  • 8/21/2019 Collections concept in Java

    25/27

     ava.util.7a!

    • !ublic int si)e$%

    • !ublic boolean isEm!ty$%

    !ublic boolean !ut$b&ect eyob&*b&ect vob&%

      E#8 m.!ut$new (nte#er$+,%* newFloat$0.f%%

    • !ublic b&ect #et$b&ect ob&%

  • 8/21/2019 Collections concept in Java

    26/27

     ava.util.7a!.Entry

    • !ublic b&ect #etQey$%

    • !ublic b&ect #etPalue$%

  • 8/21/2019 Collections concept in Java

    27/27

     ava.util.orted7a!

    • !ublic b&ect 5rst$%

    • !ublic b&ect last$%

    !ublic orted7a! head7a!$b&ectob&%

    • !ublic orted7a! sub7a!$b&ectQob&+* b&ect Pob&+%

    • !ublic orted7a! tail7a!$b&ectob&%