Difference between HashSet and HashMap

by Chaitanya Singh August 21, 2014

In this article we are gonna discuss the differences between HashSet and HashMap classes. HashSet vs HashMap Differences: HashSet HashMap HashSet class implements the Set interface HashMap class implements the Map interface In HashSet we store objects(elements or values) e.g. If we have a HashSet of string elements then it could depict a set of HashSet elements: {“Hello”, “Hi”, “Bye”, […]

Read the full article →

Difference between HashSet and TreeSet

by Chaitanya Singh August 20, 2014

In this article we are gonna discuss the differences between HashSet and TreeSet. HashSet vs TreeSet 1) HashSet gives better performance (faster) than TreeSet for the operations like add, remove, contains, size etc. HashSet offers constant time cost while TreeSet offers log(n) time cost for such operations. 2) HashSet does not maintain any order of elements while TreeSet elements are […]

Read the full article →

How to convert a HashSet to a TreeSet

by Chaitanya Singh August 18, 2014

Description Program to convert a HashSet to a TreeSet Program Here is the complete code for HashSet to TreeSet conversion. We have a HashSet of Strings and we are creating a TreeSet of strings by copying all the elements of HashSet to TreeSet. import java.util.HashSet; import java.util.TreeSet; import java.util.Set; class ConvertHashSettoTreeSet{ public static void main(String[] args) { // Create a […]

Read the full article →

Converting a HashSet to an Array

by Chaitanya Singh August 18, 2014

Here is the program for converting a HashSet to an array. Program import java.util.HashSet; class ConvertHashSettoArray{ public static void main(String[] args) { // Create a HashSet HashSet<String> hset = new HashSet<String>(); //add elements to HashSet hset.add("Element1"); hset.add("Element2"); hset.add("Element3"); hset.add("Element4"); // Displaying HashSet elements System.out.println("HashSet contains: "+ hset); // Creating an Array String[] array = new String[hset.size()]; hset.toArray(array); // Displaying Array […]

Read the full article →

Convert HashSet to a List/ArrayList

by Chaitanya Singh August 18, 2014

In this tutorial we will be learning how to convert a HashSet to a List (ArrayList). Program Here we have a HashSet of String elements and we are creating an ArrayList of Strings by copying all the elements of HashSet to ArrayList. Following is the complete code: import java.util.HashSet; import java.util.List; import java.util.ArrayList; class ConvertHashSetToArrayList{ public static void main(String[] args) […]

Read the full article →

Delete all the elements from HashSet

by Chaitanya Singh August 18, 2014

Here we are gonna see how to remove all the elements of HashSet in one go. We can do so by calling clear() method of HashSet class. Example import java.util.HashSet; class EmptyHashSetExample{ public static void main(String[] args) { // Create a HashSet HashSet<String> hset = new HashSet<String>(); //add elements to HashSet hset.add("Element1"); hset.add("Element2"); hset.add("Element3"); hset.add("Element4"); hset.add("Element5"); // Display HashSet elements […]

Read the full article →

How to copy one Set to another Set

by Chaitanya Singh August 17, 2014

In this article we are gonna see an example program to copy one Set to another Set. Example In this example we are copying one HashSet to another HashSet, however you can use any other Set like TreeSet, LinkedHashSet etc in the same manner as shown below. import java.util.HashSet; class CopySetExample{ public static void main(String[] args) { // Create a […]

Read the full article →

How to Iterate over a Set/HashSet

by Chaitanya Singh August 17, 2014

There are following two ways to iterate through HashSet: 1) Using Iterator 2) Without using Iterator Example 1: Using Iterator import java.util.HashSet; import java.util.Iterator; class IterateHashSet{ public static void main(String[] args) { // Create a HashSet HashSet<String> hset = new HashSet<String>(); //add elements to HashSet hset.add("Chaitanya"); hset.add("Rahul"); hset.add("Tim"); hset.add("Rick"); hset.add("Harry"); Iterator<String> it = hset.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } } Output: […]

Read the full article →

Difference between String and StringBuffer

by Chaitanya Singh August 15, 2014

In the last post we discussed the difference between StringBuffer and StringBuilder. Here we are gonna discuss the differences between String and StringBuffer class. String vs StringBuffer 1) Mutability: String is immutable (Once created, cannot be modified) while StringBuffer is mutable (can be modified). Example - String is immutable: String str = "Hello World"; str = "Hi World!"; By seeing […]

Read the full article →

Difference between StringBuilder and StringBuffer

by Chaitanya Singh August 15, 2014

In this article we are gonna discuss the differences between StringBuilder and StringBuffer. Before discussing the differences, lets have a look at what java documentation says about these classes: StringBuffer javadoc: A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, […]

Read the full article →

StringBuffer substring method Example

by Chaitanya Singh August 14, 2014

There are following two variations of substring method: public String substring(int startIndex, int endIndex): Returns a new String by taking characters from the original string. The character sequence starts with startIndex (beginning index, inclusive) and ends at endIndex (The ending index, exclusive). public String substring(int startIndex): The character sequence starts with startIndex (beginning index, inclusive) and ends at the end […]

Read the full article →

Our Friend's website link : FromDev.com