First, consider a method call getWindowhandles().

So, it is clear that the method getWindowhandles()returns a Settype String, so we are pretty correct when we mention:
Set<String> handles = driver.getWindowhandles();
Now I would keep my answer limited Mozilla Firefoxsince it follows a separate line of specification c W3C Specs.
WebDriver W3C Specification clearly mentions the following:
The Get Window Handles command returns a list of window handles for every open top-level browsing context. The order in which the window handles are returned is arbitrary.
WebDriver W3C Specification :
Each browsing context has an associated window handle which uniquely identifies it. This must be a String and must not be "current".
The web window identifier is the string constant "window-fcc6-11e5-b4f8-330a88ab9d7f".
, , .
:
, Selenium focus , , Set window handle driver.switchTo().window(win_handle); :
String parent_window = driver.getWindowHandle();
System.out.println("Parent Window ID is : "+parent_window);
element2.click();
Set<String> allWindows = driver.getWindowHandles();
for(String child_1:allWindows)
if(!parent_window.equalsIgnoreCase(child_1))
driver.switchTo().window(child_1);
System.out.println(driver.getTitle());
String child1_window = driver.getWindowHandle();
System.out.println("Child 1 Window ID is : "+child1_window);
driver.findElement(By.linkText("Link")).click();
Set<String> all_Windows = driver.getWindowHandles();
for(String child_2:all_Windows)
if(!parent_window.equalsIgnoreCase(child_2) && !child1_window.equalsIgnoreCase(child_2))
driver.switchTo().window(child_2);
String child2_window = driver.getWindowHandle();
System.out.println("Child 2 Window ID is : "+child2_window);