Pages

Saturday, 24 December 2016

Problem 62 - Size of Java

 Get Size of Java HashSet adalah cara untuk kita menghitung ada berapa input an di suatu variable tersebut.

Dengan Code Di bawah ini

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. import java.util.HashSet;
  2. public class GetSizeOfJavaHashSetExample{
  3.   public static void main(String[] args) {
  4.    
  5.     //create HashSet object
  6.     HashSet hSet = new HashSet();
  7.    
  8.     /*
  9.       To get the size of HashSet use
  10.       int size() method of HashSet class. It returns the number of elements
  11.       stored in HashSet object.    
  12.     */
  13.     System.out.println("Size of HashSet : " + hSet.size());
  14.    
  15.     //add elements to HashSet object
  16.     hSet.add(new Integer("1"));
  17.     hSet.add(new Integer("2"));
  18.     hSet.add(new Integer("3"));
  19.     System.out.println("Size of HashSet after addition : " + hSet.size());
  20.    
  21.     //remove one element from HashSet using remove method
  22.     hSet.remove(new Integer("1"));
  23.     hSet.remove(new Integer("3"));
  24.     System.out.println("Size of HashSet after removal : " + hSet.size());
  25.   }
  26. }

Pertama input an kosong dan saya input 3 angka yaitu 1,2,3 dan disini saya remove 1 dan 3.... jadi tinggal 1 input yang ada

0 comments:

Post a Comment