16 containers

737 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
737
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

16 containers

  1. 1. Containers<br />DhrubojyotiKayal<br />
  2. 2. Collections<br />a sequence of individual elements with one or more rules applied to them. <br />List, Queue, Set<br />Map<br />a group of key-value object pairs, allowing you to look up a value using a key. <br />Basics<br />
  3. 3. Lists promise to maintain elements in a particular sequence.<br />ArrayList - excels at randomly accessing elements, but is slower when inserting and removing elements in the middle of a List. <br />LinkedList - which provides optimal sequential access, with inexpensive insertions and deletions from the middle of the List. <br />List<br />
  4. 4. List list = new ArrayList();<br />list.add(1);<br />Integer val = (Integer) list.get(0);<br />System.out.println(list.size());<br />list.add(2);<br />list.remove(0);<br />System.out.println(list.size());<br />List in Action<br />
  5. 5. Declare an ArrayList and fill 100 integers in it.<br />Now loop through the list to print the list.<br />Exercise<br />
  6. 6. List list = new ArrayList();<br />list.add(1);<br />list.add("john");<br />Integer val2 = (Integer) list.get(1);<br />Paving way for Generics<br />
  7. 7. Allow only a specific type to be added in a list<br />Runtime system takes care of the type casts under the hood.<br />List<Integer> list = new ArrayList<Integer><br />list.add(1);<br />list.add(“hi”);<br />Generics<br />
  8. 8. Declare an ArrayList and fill it with 100 integers<br />Now iterate the list using for each loop<br />Exercise<br />
  9. 9. An iterator is an object whose job is to move through a sequence and select each object in that sequence without the client programmer knowing or caring about the underlying structure of that sequence.<br />Java Iterator can move in only one direction<br />Typical workflow<br />Get iterator object - list.iterator()<br />Get the next object in the sequence with next( ).<br />See if there are any more objects in the sequence with hasNext( ). <br />Iterator<br />
  10. 10. Repeat the previous example with an Iterator<br />Excercise<br />
  11. 11. Concrete implementation of Set interface<br />Duplicate free collection<br />Opmtized for rapid lookup<br />Set set = new HashSet<br />set.add(1);<br />set.contains(1)  boolean<br />HashSet<br />
  12. 12. Declare an integer array with 6 elements with elements – {1,5,3,5,6,3}<br />Now find the duplicates in that array.<br />Excercise<br />
  13. 13. Implements the basic List interface<br />Insertion and removal in the middle of the List is more efficient than ArrayList<br />It is less efficient for random-access operations.<br />Adds methods that allow it to be used as a stack, a Queue or a double-ended queue (deque).<br />add(Object o)<br />addFirst(Object o) <br />addLast(Object o)<br />removeFirst()<br />removeLast()<br />getFirst();<br />getLast();<br />LinkedList<br />
  14. 14. Write a Java program to implement a Queue with a LinkedList<br />Write a Java program to implement a Stack with a LinkedList<br />Excercise<br />
  15. 15. Implementation of Map<br />Key Value pair<br />Useful for cache and lookup<br />Map<String, Integer> map = new HashMap<String, Integer><br />map.get(key)<br />map.put(key,value);<br />HashMap<br />
  16. 16. Q&A<br />

×