Showing posts with label Set. Show all posts
Showing posts with label Set. Show all posts

Wednesday, 20 August 2014

Java Collections -Set

Set a Collection used to store Unordered but unique elements.
If you try to store a duplicat element in a set it will throw an error.

Example of set 

Set setA = new HashSet();

String element = "element 1";

setA.add(element);

System.out.println( set.contains(element) );

Implemenation of set 

Since Set is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Set implementations in the Java Collections API
1. HashSet
2. EnumSet
3. TreeSet
4. LinkedHashSet
How to reverse a string in java

HashSet: HashSet is backed by a HashMap. It makes no guarantees about the sequence of the elements when you iterate them. 

eg. Set setB = new HashSet();


LinkedHashSet : LinkedHashSet differs from HashSet by guaranteeing that the order of the elements during iteration is the same as the order they were inserted into the LinkedHashSet. 
Reinserting an element that is already in the LinkedHashSet does not change this order.

Set setC = new LinkedHashSet();

TreeSet :TreeSet also guarantees the order of the elements when iterated, but the order is the sorting order of the elements. In other words, the order in which the elements whould be sorted if you used a Collections.sort() on a List or array containing these elements. This order is determined either by their natural order (if they implement Comparable), or by a specific Comparator implementation.
eg :Set setD = new TreeSet();



You may Like :
Read More »

Saturday, 16 August 2014

List in java Collection

List :

It is an interface inheritd from Collections for ordered collection of objects.
It can contain duplicate elements.

How is ArrayList different from Array?


Array
Is not definite in size. Can expand according to requirement.
Is definite in size.
Stores objects
Store primitive data.
Is present in collections
framework Present in lang package

See :How to sort an Array List without using Comparator or sort method


For internal working of Array List Read: Internal worling of ArrayList

How to convert an array to a list?

using Arrays.asList() method.


ArrayList and LinkedList 



Array List and Link List are two classes that implements List Interface .

ArrayList -> is an array based representation of list.
Can be accessed using get and set methods.
Used for simple sequence.

Linked List->
Based on Link List representation.
Give good performance with add() and remove()n methods.
Gives poor performance with get() set() methods.


List Iterator

Implements Iterator interface.

List Iterator methods -> 
1.hasnext()
2. next()
3. hasprevious()
4. previous
5. nextindex();
6. previousindex();
7. remove()
8. add();
9. set()


See :Java Collection-Set
Static varibale,static class,Static method
Read More »

Java Collections- Everything you want to know

Collection is an interface - is a framework which allows for using and manipulating collections.


Two interface trees, one starting with Collection and including Set, SortedSet, List, and Queue, and the other starting with Map and including SortedMap.

Set- A collection that cannot contain duplicate elements.
2. Elements are in unordered manner.

List- A collection that can contain duplicate elements.
Elements are in ordered manner.

Read more about List here


Map and HashMap
Map is a n interface and Hash Map is class that implements it.

Iterator:1. Used to iterate over collections.
2. Move only in forward direction.

List Iterator : Used to iterate over List Collection specifically.
2. can traverse in both forward and backward direction.

Read More »