Thursday 11 August 2016

A simple ListToSet hash Programs using Selenium TestNG

..List to Set….
package examples;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 import org.testng.annotations.Test;

public class ListToSet {
  @Test
public  void list_hashset()
{
List l=new ArrayList();
l.add('a');
l.add('b');
l.add(1);
l.add(2);
l.add('a');
l.add("honey");
l.add("ice");
System.out.println(l);
Set s=new HashSet(l);
int size=s.size();
System.out.println(size);
System.out.println(s);

//Hashtable<K, V>
Hashtable<String,String> table = new Hashtable<String,String>();
table.put("name","kavya" );
table.put("place", "hyd");
table.put("surname", "cherry");
table.put("","");


System.out.println(table.get("place"));
}
}

OutPut:

[a, b, 1, 2, a, honey, ice]
6
[1, 2, b, ice, a, honey]
hyd
PASSED: list_hashset 

Note:
List can contain duplicate elements whereas Set contains unique elements only.

List:

The java.util.List interface is a sub-type of the java.util.Collection interface. It represents an ordered list of objects, meaning you can access the elements of a Listin a specific order, and by an index too. You can also add the same element more than once to a List .

ArrayList:

The ArrayList class extends AbstractList and implements the List interface.ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

Set:

 The Set Interface. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

HashSet:

uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
It contains only unique elements.It doesnot maintains order.


No comments:

Post a Comment