Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

How to remember Username and Password when pull or push Project with Git


Remember username and password when pull or push project 

In this tutorial I will share how to remember username and password when pull or push by working with git , also I will share how to avoid conflict lines  when developers are working at the same file in project.

To git remember username and password when pull or push

1) Go to your project folder
2) Right click in your project
3) Select Git Bash
4) Type: git config credential.helper store
5) Enter : your username and password

Then when pull or push git next time it will remember your username and password automatically.

Sometime developers are working at the same file ,one developer committed his/her task first ,and next developer do the same lines and the same file when pull it will conflict lines that required developer merge or resolved  that file or revert the previous file. To avoid conflict lines at the same file , here is step by step to avoid conflict line at the same file.

1) Commit your task (do not click button Push , just only commit )
2) Right click your project folder or your repository project
3) Stash Save
4) pull
5) stash pop
6) push

when push your project git will merge your code and line automatically.


Comparator Date - Java

In this tutorial you will know how to compare Date  in java .
In this section we will do step by step
1. Comparator Date in Java
2. Comparator by using method after(), before() and equal()

public boolean after(Date date);
public boolean  before(Date date)
public boolean equal(Object o);

See Example1;

import java.util.Date;

public class Details {
       public static void main(String[] args) {
Date today = new Date();
Date myDate = new Date(today.getYear(),today.getMonth()-1,today.getDay());
getCompareDate(today, myDate);
}
public static void getCompareDate(Date today, Date myDate){
System.out.println("method Comparator > My Date is : "+myDate);  
    System.out.println("method Comparator Today Date is : "+today);
    if (today.compareTo(myDate)<0){
        System.out.println(" method Comparator Today Date is Lesser than my Date");
    }
    else if (today.compareTo(myDate)>0){
        System.out.println(" method Comparator Today Date is Greater than my date");
    }  
    else{
        System.out.println("Both Dates are equal");
    }

}

//result:
method Comparator > My Date is : Mon Feb 01 00:00:00 ICT 2016
method Comparator Today Date is : Mon Mar 14 23:52:02 ICT 2016
 method Comparator Today Date is Greater than my date


Example2: method after(), before(), equal();

import java.util.Date;

public class Details {
public static void main(String[] args) {
Date today = new Date();
Date myDate = new Date(today.getYear(),today.getMonth()-1,today.getDay());
getDateComapreBeforeAndAfter(today, myDate);
}

private static void getDateComapreBeforeAndAfter(Date today, Date myDate){
//After() or before() or equal() method want to know today > myDate ? and it return boolean true or false
System.out.println("Date method compare Before : "+today.before(myDate));
System.out.println("Date method compare After : "+today.after(myDate));
System.out.println("Date method compare equal : "+today.equals(myDate));
}
}

result :

Date method compare Before : false
Date method compare After : true
Date method compare equal : false





Vector to Array - Java

In this tutorial we will convert vector to Array in java  and to convert vector to array we used method
Vector.toArray();


Syntax:  public Object [] toArray();

See Example:


import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Example {

public static void main(String args[]) {
//console method 
vectorToArray();

}
//declare method to get Vector 
public static void vectorToArray(){
//Declare String[] 
String elements[] = { "Book1", "Book2", "Book3", "Book4", "Book5" };
// convert String[] to List
Set set  =  new HashSet(Arrays.asList(elements));
String[] strObj = new String[set.size()];
strObj = (String[]) set.toArray(strObj);
for (int i = 0; i < strObj.length; i++) {
        System.out.println(strObj[i]);
     }
     System.out.println(set);
}

}

//result :

Book5
Book3
Book4
Book1
Book2
[Book5, Book3, Book4, Book1, Book2]



Vector Remove Object - Java

In this we will remove Object from Vecctor by using method remove()

Syntax : public boolean remove(Object o);

See Example:

import java.util.Vector;

public class Example {

public static void main(String args[]) {
Vector vector = new Vector(3);
vector.add(3);
vector.add(1);
vector.add(2);
System.out.println("Vector before remove element: "+vector);
System.out.println("Removed element from vector: "+vector.remove((Integer)1));
System.out.println("Vector after remove element: "+vector);
 
}

}

//result :

Vector before remove element: [3, 1, 2]
Removed element from vector: true
Vector after remove element: [3, 2]

Vector Remove Element - Java

To remove element from Vector we will use method


See Example:

import java.util.Iterator;
import java.util.Vector;

public class Example {

public static void main(String args[]) {
   
     Vector<String> vec = addVector();
 
}
//declare method to get Vector 
public static Vector<String> addVector(){
/* Vector of initial */
 Vector<String> vectors = new Vector<String>();
 /* Adding elements to a vector*/
 vectors.addElement("Item1");
 vectors.addElement("Item2");
 vectors.addElement("Item3");
 vectors.addElement("Item4");
 vectors.addElement("Item5");
 System.out.println("Vector elements : "+vectors);
 System.out.println("Vector Size Before Remove : "+vectors.size());
 //remove first element "Item1" of index zero
 System.out.println("Removed element: "+vectors.remove(0));
 System.out.println("Vector Size after Remove : "+vectors.size());

 return vectors;

}

}

//result :

Vector elements : [Item1, Item2, Item3, Item4, Item5]
Vector Size Before Remove : 5
Removed element: Item1
Vector Size after Remove : 4

Vector Iterator - java

In this tutorial we will loop or Iterator  vector .





See Example:


import java.util.Iterator;
import java.util.Vector;

public class Example {

public static void main(String args[]) {
   
     Vector<String> vec = addVector();
     System.out.println("Elements Vector : "+vec);
     Iterator iterator = vec.iterator();
     while(iterator.hasNext()){
         System.out.println("Vector Iterator  : "+iterator.next());
       }
  }
//declare method to get Vector 
public static Vector<String> addVector(){
/* Vector of initial  */
 Vector<String> vectors = new Vector<String>();
 /* Adding elements to a vector*/
 vectors.addElement("Item1");
 vectors.addElement("Item2");
 vectors.addElement("Item3");
 vectors.addElement("Item4");
 vectors.addElement("Item5");
 return vectors;

}

}

// result :

Elements Vector : [Item1, Item2, Item3, Item4, Item5]
Vector Iterator  : Item1
Vector Iterator  : Item2
Vector Iterator  : Item3
Vector Iterator  : Item4
Vector Iterator  : Item5

Vector in Java

In this tutorial we learn Add Vector in Java . by using method add()

public boolean add(E e);

See Example :

import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

public class Example1 {
public static void main(String args[]) {
 
     Vector<String> vec = addVector();
   
     System.out.println("Elements Vector : "+vec);
     System.out.println("Vector size : "+vec.size());
  }
public static Vector<String> addVector(){
/* Vector of initial*/
 Vector<String> vectors = new Vector<String>();
 /* Adding elements to a vector*/
 vectors.addElement("Item1");
 vectors.addElement("Item2");
 vectors.addElement("Item3");
 vectors.addElement("Item4");
 vectors.addElement("Item5");
 return vectors;

}

}

//result :
Elements Vector : [Item1, Item2, Item3, Item4, Item5]
Vector size : 5

Remove Mapping From HashMap

In this section we will learn how to remove element from HashMap in Java .


See Example: 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Example2 {

public static void main(String[] args) {
HashMap<Integer, String> hMap = getHashMapKeyAndValue();
}
//Declare method 
public static  HashMap<Integer, String> getHashMapKeyAndValue(){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(1, "Orange");
hashMap.put(5, "Apple");
hashMap.put(3, "Mango");
hashMap.put(4, "Coconut");
hashMap.put(2, "Red Apple");
for(Map.Entry  map : hashMap.entrySet()){
System.out.println("Key :"+ map.getKey() + " & Value : " +map.getValue());
}
//remove  key&value pairs for key 2
Object removeObj = hashMap.remove(2);
System.out.println("HashMap Elements remove key = " + removeObj);
 
System.out.println("After remove element " );
for(Map.Entry  map : hashMap.entrySet()){
System.out.println("Key :"+ map.getKey() + " & Value : " +map.getValue());
}
return hashMap;
}
}
//result:

Key :1 & Value : Orange
Key :2 & Value : Red Apple
Key :3 & Value : Mango
Key :4 & Value : Coconut
Key :5 & Value : Apple
HashMap Elements remove key = Red Apple
After remove element 
Key :1 & Value : Orange
Key :3 & Value : Mango
Key :4 & Value : Coconut
Key :5 & Value : Apple

Get Size or Length of HashMap in Java

In the last tutorial we learnt Sort key and value of HashSet as well as remove all from HashSet .
Now we well learn how to get size from HashMap by use method size() in java to know size or length of collections.

See Example:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Example2 {


public static void main(String[] args) {

HashMap<Integer, String> hMap = getHashMapKeyAndValue();
System.out.println("Before Remove HashMap:");
        Set set = hMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
              Map.Entry me = (Map.Entry)iterator.next();
              System.out.print(me.getKey() + ": ");
              System.out.println(me.getValue());
        }
        System.out.println("Hashmap Size :"+set.size());
        set.clear();
        System.out.println("After Remove Hashmap size : "+set.size());
}
//Declare method 
public static  HashMap<Integer, String> getHashMapKeyAndValue(){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(1, "Orange");
hashMap.put(5, "Apple");
hashMap.put(3, "Mango");
hashMap.put(4, "Coconut");
hashMap.put(2, "Red Apple");
return hashMap;

}
}

//result :

Before Remove HashMap:
1: Orange
2: Red Apple
3: Mango
4: Coconut
5: Apple
Hashmap Size :5
After Remove Hashmap size : 0

Remove All from HashMap In java

In the tutorial we will learn HashMap and how to remove All from Hashmap, so to remove all from HashMap we use method clear(); to remove Key and values from HashMap.

See Example:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Example2 {

public static void main(String[] args) {
HashMap<Integer, String> hMap = getHashMapKeyAndValue();
System.out.println("Before Remove HashMap:");
        Set set = hMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
              Map.Entry me = (Map.Entry)iterator.next();
              System.out.print(me.getKey() + ": ");
              System.out.println(me.getValue());
        }
   
        set.clear();
        System.out.println("After Remove Hashmap :"+set);
}
//Declare method
public static  HashMap<Integer, String> getHashMapKeyAndValue(){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(1, "Orange");
hashMap.put(5, "Apple");
hashMap.put(3, "Mango");
hashMap.put(4, "Coconut");
hashMap.put(2, "Red Apple");
return hashMap;

}
}

//result :

Before Remove HashMap:
1: Orange
2: Red Apple
3: Mango
4: Coconut
5: Apple
After Remove Hashmap :[]

How to loop HashMap in java

In this tutorial we learn how to loop HashMap in java ,by For Loop and Iterator HashMap .

See Example:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Example {

public static void main(String[] args) {
HashMap<Integer, String>  hashMap = getHashMapKeyAndValue();
//For Loop hashMap
 System.out.println("For Loop:");
for(Map.Entry  map : hashMap.entrySet()){
System.out.println("Key :"+ map.getKey() + " & Value : " +map.getValue());
}
//WHILE LOOP & ITERATOR
        System.out.println("While Loop:");
     
        Iterator iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry map2 = (Map.Entry) iterator.next();
         System.out.println("Key: "+map2.getKey() + " & Value: " + map2.getValue());
       }

}
//Declare method
public static  HashMap<Integer, String> getHashMapKeyAndValue(){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(01, "Book1");
hashMap.put(05, "Book5");
hashMap.put(03, "Book3");
hashMap.put(04, "Book4");
hashMap.put(07, "Book7");
hashMap.put(02, "Book2");
hashMap.put(06, "Book6");
return hashMap;

}

}

// result :

For Loop:
Key :1 & Value : Book1
Key :2 & Value : Book2
Key :3 & Value : Book3
Key :4 & Value : Book4
Key :5 & Value : Book5
Key :6 & Value : Book6
Key :7 & Value : Book7
While Loop:
Key: 1 & Value: Book1
Key: 2 & Value: Book2
Key: 3 & Value: Book3
Key: 4 & Value: Book4
Key: 5 & Value: Book5
Key: 6 & Value: Book6
Key: 7 & Value: Book7

HashMap in java

In this tutorial we will learn HashMap .HashMap is the collection class .It is used to sort key and values pairs  HashMap <K, V> .HashMap implement Map Interface . HashMap not order key and values, also it allow Key null and values null .It is used for maintain Key and Value mapping in Java.

HashMap<K,V> hashmap = new HashMap<K,V>();

It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap. It neither does any kind of sorting to the stored keys and Values. You must need to import java.util.HashMap or its super class in order to use the HashMap class and methods.

See Example:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Example {

public static void main(String[] args) {
HashMap<Integer, String>  hashMap = getHashMapKeyAndValue();
//For Loop hashMap
 System.out.println("For Loop:");
for(Map.Entry  map : hashMap.entrySet()){
System.out.println("Key :"+ map.getKey() + " & Value : " +map.getValue());
}
//WHILE LOOP & ITERATOR
        System.out.println("While Loop:");
     
        Iterator iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry map2 = (Map.Entry) iterator.next();
         System.out.println("Key: "+map2.getKey() + " & Value: " + map2.getValue());
       }

}
//Declare method
public static  HashMap<Integer, String> getHashMapKeyAndValue(){
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
hashMap.put(01, "Book1");
hashMap.put(05, "Book5");
hashMap.put(03, "Book3");
hashMap.put(04, "Book4");
hashMap.put(07, "Book7");
hashMap.put(02, "Book2");
hashMap.put(06, "Book6");
return hashMap;

}

}

//result :
For Loop:
Key :1 & Value : Book1
Key :2 & Value : Book2
Key :3 & Value : Book3
Key :4 & Value : Book4
Key :5 & Value : Book5
Key :6 & Value : Book6
Key :7 & Value : Book7
While Loop:
Key: 1 & Value: Book1
Key: 2 & Value: Book2
Key: 3 & Value: Book3
Key: 4 & Value: Book4
Key: 5 & Value: Book5
Key: 6 & Value: Book6
Key: 7 & Value: Book7




TreeSet In java

In the last tutorial we learnt LinkedList and HashSet , TreeSet is similar Hashset , except.
Treeset sort the elements in ascending order and allow null element
Hashset doesn't maintain any orders  and doesn't not allow null and both no duplicate values.

import java.util.TreeSet;

public class Example1 {
 public static void main(String args[]) {
        // TreeSet of String Type
TreeSet<String> treeSet = getBookTreeSet();
TreeSet<Integer> treeNum = getTreeSetNum();
  }
  public static TreeSet<String> getBookTreeSet(){
  TreeSet<String> treeSet = new TreeSet<String>();

        // Adding elements to TreeSet<String>
  System.out.println("TreeSet of Book");
        treeSet.add("Book1");
        treeSet.add("Book1");
        treeSet.add("Book2");
        treeSet.add("Book2");
        treeSet.add("Ink");
        treeSet.add("Ink");
        treeSet.add("Cat");
        treeSet.add("null");
        //Displaying TreeSet
        System.out.println(treeSet);
     
        return treeSet;
  }
  public static TreeSet<Integer> getTreeSetNum(){
  // TreeSet of Integer Type
        TreeSet<Integer> treeSet2 = new TreeSet<Integer>();
        System.out.println("TreeSet of number");
        // Adding elements to TreeSet<Integer>
        treeSet2.add(88);
        treeSet2.add(88);
        treeSet2.add(7);
        treeSet2.add(101);
        treeSet2.add(101);
        treeSet2.add(101);
        treeSet2.add(222);
        System.out.println(treeSet2);
        return treeSet2;
  }
}
//result : 

TreeSet of Book
[Book1, Book2, Cat, Ink, null]
TreeSet of number
[7, 88, 101, 222]

we saw the result Treeset sorted and allow null as well as no duplicate values

LInkedHashset in java

In the last tutorial we learnt Hashset  and Treeset and now we will learn LinkedHashset , also it an implements from Set Interface , It's similar to the HashSet and TreeSet except the below mentioned differences .

1) HashSet doesn’t maintain any kind of order of its elements.
2) TreeSet sorts the elements in ascending order.
3) LinkedHashSet maintains the insertion order. Elements gets sorted in the same sequence in which they have been added to the Set.

See Example :

import java.util.LinkedHashSet;

public class Example1 {

public static void main(String[] args) {
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();

        // Adding elements of String type to the LinkedHashSet
linkedHashSet.add("Linke 3");
linkedHashSet.add("Linke 1");
linkedHashSet.add("Linke 5");
linkedHashSet.add("Linke 2");
linkedHashSet.add("Linke 4");
linkedHashSet.add("Linke 6");
        System.out.println(linkedHashSet);
     
        // LinkedHashSet of Integer Type
        LinkedHashSet<Integer> LinkedSetNum = new LinkedHashSet<Integer>();

        // Adding elements
        LinkedSetNum.add(14);
        LinkedSetNum.add(23);
        LinkedSetNum.add(11);
        LinkedSetNum.add(62);
        LinkedSetNum.add(81);
        LinkedSetNum.add(33);
        System.out.println(LinkedSetNum);
}

}
//see result
[Linke 3, Linke 1, Linke 5, Linke 2, Linke 4, Linke 6]
[14, 23, 11, 62, 81, 33]

we saw the result as the put in, put out , Both type of LinkedHashSet have preserved the insertion order.

what difference HashSet and TreeSet in java ?

In this section we will learn difference between HashSet and TreeSet in java .

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 sorted in ascending order by default.

Similarities:

1) Both HashSet and TreeSet does not hold duplicate elements, which means both of these are duplicate free.

2) If you want a sorted Set then it is better to add elements to HashSet and then convert it into TreeSet rather than creating a TreeSet and adding elements to it.

3) Both of these classes are non-synchronized that means they are not thread-safe and should be synchronized explicitly when there is a need of thread-safe operations.

import java.util.HashSet;
import java.util.TreeSet;

public class Example1 {
public static void main(String[] args) {
 // HashSet declaration
      HashSet<String> hashset  = getHashSet();
}
public static HashSet<String> getHashSet(){
HashSet<String>  hashset  =  new HashSet<String>();

// Adding elements to the HashSet
      hashset.add("Item4");
      hashset.add("Item2");
      hashset.add("Item6");
      hashset.add("Item1");
      hashset.add("Item3");
      hashset.add("Item5");
      //add duplicate value
      hashset.add("Item3");
      System.out.println("HashSet values ALL :"+hashset);
   
      //TreeSet Declaration
      TreeSet<String> treeset  =  new TreeSet<String>();
      treeset.add("Item4");
      treeset.add("Item2");
      treeset.add("Item6");
      treeset.add("Item1");
      treeset.add("Item3");
      treeset.add("Item5");
      //add duplicate value
      treeset.add("Item3");
      System.out.println("TreeSet values ALL :"+treeset);
return hashset;
}
 
}

//result :

HashSet values ALL :[Item1, Item2, Item5, Item6, Item3, Item4]
TreeSet values ALL :[Item1, Item2, Item3, Item4, Item5, Item6]


we saw treeset display elements sorted in ascending order.

HashSet VS HashMap in java

In this section we will learn and know what difference HashSet and HashMap in java .

Similarities:

1) Both HashMap and HashSet are not synchronized which means they are not suitable for thread-safe operations unitl unless synchronized explicitly. This is how you can synchronize them explicitly:

HashSet Example:

Set cset =  Collections.synchronizeSet( new HashSet(....));

HashMap Example: 

Map m = Collections.synchronizeMap(new HashMap());

2) Both of these classes do not guarantee that the order of their elements will remain constant over time.

3) If you look at the source code of HashSet then you may find that it is backed up by a HashMap. So basically it internally uses a HashMap for all of its operations.

4) They both provide constant time performance for basic operations such as adding, removing element etc.



See Example :

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class Example1 {
 public static void main(String args[]) {
     // HashSet declaration
     HashSet<String> hashset  =  new HashSet<String>();

     // Adding elements to the HashSet
     hashset.add("Item4");
     hashset.add("Item2");
     hashset.add("Item6");
     hashset.add("Item1");
     hashset.add("Item3");
     hashset.add("Item5");
     System.out.println("HashSet values :"+hashset);
   
     //HashMap Declaration
     Map<String, String> map = new HashMap<String, String>();
     map.put("1", "Book1");
     map.put("2", "Book2");
     map.put("3", "Book3");
     map.put("4", "Book4");
     System.out.println("HashMap values : "+map);
 
 }
}

//result:

HashSet values :[Item1, Item2, Item5, Item6, Item3, Item4]
HashMap values : {1=Book1, 2=Book2, 3=Book3, 4=Book4}











Convert HashSet to ArrayList in java

In this tutorial we will learn how to convert HashSet to Arraylist , Some junior developers have issues convert HashSet to Arraylist to in order to sort data or add to collection method to Arraylist .

See Example:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class Example1 {
 public static void main(String args[]) {
     HashSet<String> hashset = getHashSet();
   
 }

  public static   HashSet<String> getHashSet(){

 HashSet<String> hashset  =  new HashSet<String>();

      // Adding elements to the HashSet
      hashset.add("Item4");
      hashset.add("Item2");
      hashset.add("Item6");
      hashset.add("Item1");
      hashset.add("Item3");
      hashset.add("Item5");
      System.out.println("HashSet values : ");
      for(String str : hashset){
       System.out.println(str);
      }
      // Declare ArrayList to add Hashset to Arraylist
      List<String> arrList = new ArrayList<String>(hashset);
   
      System.out.println("ArrayList  values : ");
      for(String temp : arrList){
         System.out.println(temp);
      }
      //sort arrayList
      Collections.sort(arrList);
      System.out.println("After sort  Arraylist : ");
      for(String temp : arrList){
          System.out.println(temp);
       }
      return hashset;

}
}

//result :

HashSet values :
Item1
Item2
Item5
Item6
Item3
Item4
ArrayList  values :
Item1
Item2
Item5
Item6
Item3
Item4
After sort  Arraylist :
Item1
Item2
Item3
Item4
Item5
Item6

Convert HashSet to Treeset in java

In the last tutorial you learn a lot java collection ArrayList  ,now we will learn how to convert HashSet to TreeSet. we Convert TreeSet in order to sort values of HashSet .

See Example:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Example1 {
 public static void main(String args[]) {
              // HashSet declaration
HashSet<String> hashset = getHashSet();
 }
public static   HashSet<String> getHashSet(){
 HashSet<String> hashset  =  new HashSet<String>();

      // Adding elements to the HashSet
      hashset.add("Item4");
      hashset.add("Item2");
      hashset.add("Item6");
      hashset.add("Item1");
      hashset.add("Item3");
      hashset.add("Item5");
      System.out.println("HashSet values : ");
      for(String str : hashset){
       System.out.println(str);
      }
   // Creating a TreeSet of HashSet elements
      Set<String> treeset = new TreeSet<String>(hashset);
      // Displaying TreeSet elements
      System.out.println("TreeSet values : ");
      for(String temp : treeset){
         System.out.println(temp);
      }
      return hashset;

}
}

//result :
HashSet values : 
Item1
Item2
Item5
Item6
Item3
Item4
TreeSet values : 
Item1
Item2
Item3
Item4
Item5
Item6
we saw the values of TreeSet is sorted.

Convert Hashset to an Array

In this tutorial we will learn how to convert Hashset to an Array, And to convert Hashset to an Array we use toArray(String[] arg)

See Example:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class Example1 {
 public static void main(String args[]) {
                // HashSet declaration
HashSet<String> hashset = getHashSet();
 }
public static   HashSet<String> getHashSet(){
// Adding elements to the HashSet
HashSet<String> hashset  =  new HashSet<String>();
      hashset.add("Item4");
      hashset.add("Item2");
      hashset.add("Item6");
      hashset.add("Item1");
      hashset.add("Item3");
      hashset.add("Item5");
      System.out.println(" Values of Hashset = "+hashset);
      // Creating an Array (convert hashset to Array
      String[] array = new String[hashset.size()];
      hashset.toArray(array);
      System.out.println(" Result convert hashset to Array: ");
      for(String temp : array){
         System.out.println(temp);
      }
      return hashset;
}
}

//result:

  Values of Hashset = [Item1, Item2, Item5, Item6, Item3, Item4]
 Result convert hashset to Array: 
Item1
Item2
Item5
Item6
Item3
Item4


Iterator and loop HashSet in java

In this tutorial we will learn how to Iterator HashSet or loop Hashset in java.

In looping Hashset we use two ways.

1. Iterator,
2. for loop

See Example

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import port java.util.List;

public class Example1 {
 public static void main(String args[]) {
     // HashSet declaration
HashSet<String> hashset = getHashSet();
 
   }
  public static   HashSet<String> getHashSet(){
// Adding elements to the HashSet
HashSet<String> hashset  =  new HashSet<String>();
      hashset.add("Item4");
      hashset.add("Item2");
      hashset.add("Item6");
      hashset.add("Item1");
      hashset.add("Item3");
      hashset.add("Item5");
   
      // Creating an Array (convert hashset to Array
      System.out.println("--For Loop --");
      for(String str : hashset){
          System.out.println(str);
         }
      //Iterator HashSet
      System.out.println("Iterator hashset ");
      Iterator<String> it = hashset.iterator();
      while(it.hasNext()){
         System.out.println(it.next());
      }
      return hashset;
}
}

// result :

--For Loop --
Item1
Item2
Item5
Item6
Item3
Item4
Iterator hashset
Item1
Item2
Item5
Item6
Item3
Item4




Kategori

Kategori