MainMenu

Home Java Overview Maven Tutorials

Tuesday, 13 March 2018

Map Interface in Java, What is HashMap in Java? HashMap vs HashTable



HashMap in java


Hi all, in this demo i will describe about HashMap in java.



Java HashMap is a hash table based implementation of Java’s Map interface.
A Map, is a collection of key-value pairs. It maps keys to values.

Map Interface.
public interface Map
An object that maps key to values. A map cannot contain duplicate keys;each key can map to at most one value.
This interface takes the place of the Disctionary class, which was a totally Abstract Class rather than an interface.

HashMap is important for selenium as well as for interview also.
hashMap is a Map based collection class that is used for storing Key & Value(associated with keys).
1).It is donated as HashMap.
2).Hashmap contains only unique elements.
3).Hashmap may have only one null key and multiple null values
4). Hashmap maintains no order.

Here is the example of HashMap in Java.


import java.util.HashMap;
import java.util.Map;
public class TestCsc2
{
public static void main(String args[])
{
Map<String, Integer> hmp = new HashMap<String, Integer>();
hmp.put(null, 6); //hash map can have one null key which will be stored at hashMap(0)
hmp.put("aaa", 1);
hmp.put("aaa", 5);
hmp.put("bbb", 2);
hmp.put("ccc", 3);
hmp.put("ddd", 4);
System.out.println(hmp);
}
}

Output :

{null=6, aaa=5, ccc=3, bbb=2, ddd=4}





Here is another example of HashMap in Java.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class hashmapdemo
{
public static void main(String args[])
{
hashmapdemo obj = new hashmapdemo();
obj.g();
}
public void g()
{
Map <String, Integer > record = new HashMap< String, Integer >();
record.put("Anjana", 26);
record.put("Silpi", 27);
record.put("Heena", 28);
record.put("Adhiraj", 29);
System.out.println(record.get("Heena"));
Set<String> keys = record.keySet();
for(String i : keys)
{
System.out.println(i + ":" +record.get(i));
}
}
}

Output :

28
Adhiraj:29
Anjana:26
Silpi:27
Heena:28


-------------------------------------------------------------------
Another example of hashmap :-

public class ConsTest
{
public static void main(String args[])
{
Map<Integer, String> map = new HashMap<Integer, String<();
map.put(10, "Chandan");
map.put(20, "Adhiraj");
map.put(30, "Anngraj");
System.out.println(map.get(10));
for(Map.Entry m : map.entrySet())
{
System.out.println(m.getKey()+ " " + m.getValue());
}
System.out.println("Printing the value using set");
Set<Integer> keys = map.keySet();
for(int i : keys)
{
System.out.println(i + " " + map.get(i));
}
}
}

Output :-

Chandan
20 Adhiraj
10 Chandan
30 Anngraj
Printing the value using set
20 Adhiraj
10 Chandan
30 Anngraj




TreeMap in Java

TreeMap is Red-Black tree based NavigableMap implementation. It is sorted according to the natural ordering of its keys.
TreeMap class implements Map interface similar to HashMap class.
The main difference between them is that HashMap is an unordered collection while TreeMap is sorted in the ascending order of its keys.
TreeMap is unsynchronized collection class which means it is not suitable for thread-safe operations until unless synchronized explicitly.

Here is the example of TreeMap in Java.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class hashmapdemo
{
public static void main(String args[])
{
hashmapdemo obj = new hashmapdemo();
obj.g();
}
public void g()
{
//Map<String, Integer> record = new HashMap<String, Integer>();
TreeMap<Integer, String> record = new TreeMap<String, Integer>();
record.put(26, "Anjana");
record.put(28, "Heena");
record.put( 29, "Adhiraj");
record.put(27, "Silpi");
System.out.println(record.get(28));
for(Integer i : record.keySet())
{
System.out.println(i + ":" +record.get(i));
}
}
}


Output :

Heena
26:Anjana
27:Silpi
28:Heena
29:Adhiraj

-------------------------------------------------------------------
Another example of Treemap :-



public class ConsTest
{
public static void main(String args[])
{
Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(10, "Chandan");
map.put(20, "Adhiraj");
map.put(30, "Anngraj");
System.out.println(map.get(10));
for(Map.Entry m : map.entrySet())
{
System.out.println(m.getKey()+ " " + m.getValue());
}
System.out.println("Printing the value using set");
Set<Integer> keys = map.keySet();
for(int i : keys)
{
System.out.println(i + " " + map.get(i));
}
}
}

Output :-

Chandan
10 Chandan
20 Adhiraj
30 Anngraj
Printing the value using set
10 Chandan
20 Adhiraj
30 Anngraj

HashMap vs TreeMap



HashMap vs HashTable vs SynchronizedHashMap vs ConcurrentHashMap




HashMapLinkedHashMapTreeMap
HashMap allows one null key. LinkedHashMap permits one null key. TreeMap does not permit null keys.
HashMap permits multiple null values LinkedHashMap allows multiple null values. TreeMap allows null values.
HashMap does not maintain any order. LinkedHashMap maintains the insertion order. TreeMap maintains keys in a sorted order.


HashMap :- Not thread-safe, can have one null key & multiple null values.
Map<String, Integer>mp = new HashMap<String, Integer>();
// Map Interface is required to create object of HashMap class.

Hashtable :- Thread-safe, slow performance, null key and null values are not allowed.

SynchronizedMap :- Thread - safe, slow performance, one null key and multiple null values are allowed.
Collections.synchronizedMap(=new HashMap<String, Integer>());

ConcurrentHashMap :- Thread-safe, fast performance, null key and null values are not allowed.
ConcurrentHashMap<String, Integer> chm = new ConcurrentHashMap<String, Integer>();
//No Map interface is required to create the object of ConcurrentHashMap class.
//ConcurrenthashMap does not throw any ConcurrentModificationException.


Saturday, 3 March 2018

for each loop in java with example



For each loop in Java with example

for Each loop in java :
Hi all, in this demo i will describe about for-each loop in java.

This loop is important for selenium as well as for interview also.
So as the name says when we want to perform some operation with each element of the list or collection or array, then we use this loop.
The Syntax of the loop is :
for(data_type variable_name : array|collection)
{
}
it will get more clear when we describe it through an example :

Example of for-each loop in java


public class foreach
{
public static void main(String args[])
{
int arr[] = {10, 20, 30, 44};
for(int x : arr)
{
System.out.println("Your number is :" + x);
}
}
}
---------------------------------------------------------------------------
Now, Suppose you are working in some selenium project, and have create a WebElement list then, you can perform operation with each element using for -each loop

For Each Loop in selenium Example :
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class foreach_selenium
{
WebDriver driver;
@BeforeTest
public void g() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Complete selenium\\ChromeDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.way2testing.com");
Thread.sleep(3000);
}
@Test
public void h()
{
List<WebElement >list = driver.findElements(By.cssSelector("#post-body-751561208295527719>div>div>table>tbody>tr>td>a"));
System.out.println("Your total list elements are :" +list.size());
for(WebElement wb : list)
{
System.out.println(wb.getText());
}
}
}




Output :

Manual Testings Introduction
Definitions of Manual Testings
Defect Clustering in Software Testing
Bug Life Cycle and Reviews
Level of Testing
TEST CASE, TEST PLAN and RTM
Manual test Cases for Pen
Manual test cases for Email
ISTQB Practice set
Important Unix Command for Software Tester
Testings Myth
Agile Methodology
Windows and Linux Shortcut keys
Test Strategy in Software Testing
Finance Testing Basics
Mobile Test
Effort Estimation Technique
Java Script Basics
How to test Login page & Date fields manually
Technical Tips
Function in Javascript
Discussion Forum
Download e_Books
Professional Ethics
Aptitude Tricks
English Speaking Tutorials