<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shashi Preetham</title>
    <description>The latest articles on DEV Community by Shashi Preetham (@preetham).</description>
    <link>https://dev.to/preetham</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F603951%2Fb90206db-cc64-4a61-ac30-606a3cc96629.png</url>
      <title>DEV Community: Shashi Preetham</title>
      <link>https://dev.to/preetham</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/preetham"/>
    <language>en</language>
    <item>
      <title>Writing Data to a Binary File in Java</title>
      <dc:creator>Shashi Preetham</dc:creator>
      <pubDate>Fri, 26 Mar 2021 10:19:55 +0000</pubDate>
      <link>https://dev.to/preetham/writing-data-to-a-binary-file-in-java-45m1</link>
      <guid>https://dev.to/preetham/writing-data-to-a-binary-file-in-java-45m1</guid>
      <description>&lt;h3&gt;
  
  
  Binary Files
&lt;/h3&gt;

&lt;p&gt;Computers are based on the binary numbering system, which consists of just two unique numbers, &lt;strong&gt;0&lt;/strong&gt; and &lt;strong&gt;1&lt;/strong&gt;. All operations that are possible in the decimal system (addition, subtraction, multiplication, division) are equally possible in the binary system.&lt;br&gt;&lt;br&gt;
A binary file is computer-readable but not human-readable. All executable programs are stored in binary files, as are most numeric data files. In contrast, text files are stored in a form (usually ASCII) that is human-readable.&lt;/p&gt;
&lt;h4&gt;
  
  
  Writing Data to A Binary File
&lt;/h4&gt;

&lt;p&gt;Accepting the data values from the console and sending them from memory buffer to a secondary storage device. It deals with all types of primitive data viz. short, int, long, float, double, char, boolean, and String. In this system, the data are stored in terms of bytes in the secondary storage devices.&lt;br&gt;
Follow the following steps while creating a binary file:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1.&lt;/strong&gt; Create the file in output stream mode along with the stream object.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;code&gt;FileOutputStream &amp;lt;object&amp;gt; = new FileOutputStream &amp;lt;file name&amp;gt;&lt;/code&gt;&lt;br&gt;&lt;br&gt;
For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FileOutputStream fout = new FileOutputStream("example.dat");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The extension of the file name &lt;strong&gt;(.dat)&lt;/strong&gt; signifies the type of data that is stored in the output stream mode in the hard disk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; Create a link between the object of the FileOutputStream with the buffer output stream.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;code&gt;DataOutputStream &amp;lt;object&amp;gt; = new DataOutputStream(FileOutputStream object);&lt;/code&gt;&lt;br&gt;&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DataOutputStream dout = new DataOutputStream(fout);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; Accept the required data from the console in one of the two ways:&lt;br&gt;&lt;br&gt;
(a) Using stream reader by importing &lt;code&gt;java.io.*&lt;/code&gt;&lt;br&gt;
(b) Using Scanner class by importing &lt;code&gt;java.util.*&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.&lt;/strong&gt; Write the data in the secondary storage with the help of these commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To write short type data -  &lt;code&gt;&amp;lt;object&amp;gt;.writeShort&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To write integer type data - &lt;code&gt;&amp;lt;object&amp;gt;.writeInt&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To write long type data - &lt;code&gt;&amp;lt;object&amp;gt;.writeLong&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To write float type data - &lt;code&gt;&amp;lt;object&amp;gt;.writeFloat&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To write double type data - &lt;code&gt;&amp;lt;object.writeDouble&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To write character type data - &lt;code&gt;&amp;lt;object&amp;gt;.writeChar&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To write string type data - &lt;code&gt;&amp;lt;object&amp;gt;.writeUTF&amp;lt;variable name&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(&lt;strong&gt;UTF&lt;/strong&gt; stands for &lt;strong&gt;Unicode Transformed Format&lt;/strong&gt; string)  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.&lt;/strong&gt; Close the data file using FileOutputStream object as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;object&amp;gt;.close();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Sample Program
&lt;/h4&gt;

&lt;p&gt;An Electronic Store wants to make an inventory of all types of products in a binary file "inventory.dat" to input product name, product code, quantity of product and price.&lt;br&gt;
Following is a program to create a binary file to perform the above task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.*;
public class sample {
    public static void main(String args[]) throws IOException {
        BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));

        // Creating binary file
        FileOutputStream fout=new FileOutputStream("inventory.dat");
        DataOutputStream dout=new DataOutputStream(fout);

        int n, q, pr;
        String str, pc;
        System.out.println("Enter number of products:");
        n=Integer.parseInt(ob.readLine());
        for(int i=1;i&amp;lt;=n;i++) {
            System.out.println("Enter product name");
            str=ob.readLine();
            System.out.println("Enter product code");
            pc=ob.readLine();
            System.out.println("Enter product quantity");
            q=Integer.parseInt(ob.readLine());
            System.out.println("Enter price");
            pr=Integer.parseInt(ob.readLine());

            // Writing data values from memory to binary file
            dout.writeUTF(str);
            dout.writeUTF(pc);
            dout.writeInt(q);
            dout.writeInt(pr);
        }
        // Closing binary file object
        fout.close();
        dout.close();
        ob.close();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>filehandling</category>
      <category>binaryfile</category>
    </item>
    <item>
      <title>Implementing Min Heap in Java</title>
      <dc:creator>Shashi Preetham</dc:creator>
      <pubDate>Fri, 26 Mar 2021 09:18:30 +0000</pubDate>
      <link>https://dev.to/preetham/implementing-min-heap-in-java-3bil</link>
      <guid>https://dev.to/preetham/implementing-min-heap-in-java-3bil</guid>
      <description>&lt;p&gt;A &lt;strong&gt;Min-Heap&lt;/strong&gt; is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node.&lt;br&gt;&lt;br&gt;
Mapping the elements of a heap into an array is trivial: if a node is stored an index &lt;strong&gt;n&lt;/strong&gt;, then its left child is stored at index &lt;strong&gt;2n+1&lt;/strong&gt; and its right child at index &lt;strong&gt;2n+2&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2m0p24w67wxie4h8pf3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh2m0p24w67wxie4h8pf3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Min Heap Representation
&lt;/h4&gt;

&lt;p&gt;A Min heap is typically represented as an array.&lt;br&gt;
Consider an array &lt;code&gt;Arr[]&lt;/code&gt; with root at &lt;code&gt;Arr[0]&lt;/code&gt;. For any &lt;strong&gt;i&lt;sup&gt;th&lt;/sup&gt;&lt;/strong&gt; node, i.e., &lt;strong&gt;Arr[i]&lt;/strong&gt;:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Arr[(i -1) / 2]&lt;/code&gt; returns its parent node.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Arr[(2 * i) + 1]&lt;/code&gt; returns its left child node.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Arr[(2 * i) + 2]&lt;/code&gt; returns its right child node.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Operations on Min Heap
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;getMin()&lt;/strong&gt;: It returns the root element of Min Heap. Time Complexity of this operation is &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;extractMin()&lt;/strong&gt;: Removes the minimum element from MinHeap. Time Complexity of this Operation is &lt;strong&gt;O(Log n)&lt;/strong&gt; as this operation needs to maintain the heap property (by calling heapify()) after removing root.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;insert()&lt;/strong&gt;: Inserting a new key takes &lt;strong&gt;O(Log n)&lt;/strong&gt; time. We add a new key at the end of the tree. If new key is larger than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Implementation of Min Heap in Java using Priority Queues
&lt;/h4&gt;

&lt;p&gt;Here’s how you can implement a min heap using &lt;code&gt;PriorityQueue&lt;/code&gt; class in Java. By default Min Heap is implemented by this class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.*;
public class MinHeap {
    static PriorityQueue&amp;lt;Integer&amp;gt; minHeap = new PriorityQueue&amp;lt;Integer&amp;gt;();
    public static void main(String args[]) {

        // Adding elements using add()
        minHeap.add(8);
        minHeap.add(5);
        minHeap.add(13);
        minHeap.add(2);
        minHeap.add(23);
        minHeap.add(16);

        // Displaying minHeap elements
        print();

        // Find number of elements using size()
        System.out.println("Size of heap = "+minHeap.size());

        // View head using peek()
        System.out.println("Head = "+minHeap.peek());

        // Remove head and modify Heap using poll()
        minHeap.poll();
        System.out.println("Heap after removing head");
        print();

        // Remove specific element using remove()
        minHeap.remove(8);
        System.out.println("Heap after removing 8");
        print();

        // Check if an element is present using contains()
        boolean flag=minHeap.contains(15);
        System.out.println("Does heap contain element 15? " +flag);

        System.out.println("Size of heap = "+minHeap.size());
    }
    public static void print() {
        System.out.print("Min Heap : ");
        for(Integer i:minHeap)
            System.out.print(i+" ");
        System.out.println("\n");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Min Heap : 2 5 13 8 23 16 

Size of heap = 6
Head = 2
Heap after removing head
Min Heap : 5 8 13 16 23 

Heap after removing 8
Min Heap : 5 16 13 23 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>heap</category>
      <category>datastructure</category>
      <category>priorityqueue</category>
      <category>java</category>
    </item>
  </channel>
</rss>
