<?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: Shahed Abdulwahab</title>
    <description>The latest articles on DEV Community by Shahed Abdulwahab (@shahed96).</description>
    <link>https://dev.to/shahed96</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%2F604863%2Fd3f746d9-6803-4ecd-9226-d0308318a8e1.png</url>
      <title>DEV Community: Shahed Abdulwahab</title>
      <link>https://dev.to/shahed96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shahed96"/>
    <language>en</language>
    <item>
      <title>Java abstract classes and interfaces</title>
      <dc:creator>Shahed Abdulwahab</dc:creator>
      <pubDate>Sat, 25 Dec 2021 10:18:06 +0000</pubDate>
      <link>https://dev.to/shahed96/java-abstract-classes-and-interfaces-p2p</link>
      <guid>https://dev.to/shahed96/java-abstract-classes-and-interfaces-p2p</guid>
      <description>&lt;h6&gt;
  
  
  It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.
&lt;/h6&gt;

&lt;h6&gt;
  
  
  Abraham Lincoln.
&lt;/h6&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%2Fwupisab1nxldsz1gxhwu.jpg" 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%2Fwupisab1nxldsz1gxhwu.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Abstract class:&lt;/em&gt;&lt;/strong&gt; Class that has some methods without complete definition and has the modifier &lt;code&gt;abstract&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can not create an object using an abstract class constructor.&lt;/li&gt;
&lt;li&gt;You can use an abstract class as a base class to define a derived class.&lt;/li&gt;
&lt;li&gt;The abstract class has at least one abstract method.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Abstract method:&lt;/em&gt;&lt;/strong&gt; has heading just like an ordinary method, but without body, and it requires the modifier &lt;code&gt;abstract&lt;/code&gt; and a semicolon.&lt;/li&gt;
&lt;li&gt;An abstract method can not be private.&lt;/li&gt;
&lt;li&gt;An abstract class can be a type.
Ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Example&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getPay&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Interface:&lt;/em&gt;&lt;/strong&gt; specifies a set of methods that any class implements that interface must have.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An interface is a type.&lt;/li&gt;
&lt;li&gt;It contains method headings without definition, and no instance variables:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Interface1&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;method1&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;method2&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To implement an interface, the class must do two things:
&lt;ol&gt;
&lt;li&gt; Include

&lt;code&gt;implements&lt;/code&gt;

&lt;em&gt;InterfaceName&lt;/em&gt;.&lt;/li&gt; 
&lt;li&gt; The class must implements &lt;strong&gt;all&lt;/strong&gt; the method headings listed in the interface.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Implementer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Interface1&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;method1&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//definition   &lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;method2&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//definition   &lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The method headings declared to be &lt;em&gt;public&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;An abstract class can implement an interface as well, this class gives definitions for &lt;strong&gt;some&lt;/strong&gt; of the method headings in the interface.&lt;/li&gt;
&lt;li&gt;Java interfaces can holds &lt;strong&gt;constants&lt;/strong&gt; as well, ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Constant&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;JANUARY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FEBRUARY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;MARCH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Any class that implements the

&lt;code&gt;Constant&lt;/code&gt;

interface will automatically have these constants, ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Constants&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Constant&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;JANUARY&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can mix the uses of interfaces by including both constants and method headings in a single interface.&lt;/li&gt;
&lt;li&gt;Multiple inheritance is not supported in Java, so that a class can extends only one base class. however, using interfaces, a class can implements several interfaces:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Implementer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Interface1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Interface2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt; &lt;span class="nc"&gt;InterfaceN&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The reason that a Java class can extends only one base class is if Java allowed two base classes, the two classes may have the same method heading with different definition and that will cause inconsistency. &lt;/li&gt;
&lt;li&gt;Rare phenomena, two interface can be inconsistent by defining two constants with the same name and different values:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Interface1&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ANSWEAR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Interface1&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;ANSWEAR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
    </item>
    <item>
      <title>File I/O in Java</title>
      <dc:creator>Shahed Abdulwahab</dc:creator>
      <pubDate>Tue, 13 Jul 2021 18:33:42 +0000</pubDate>
      <link>https://dev.to/shahed96/file-i-o-in-java-2kbm</link>
      <guid>https://dev.to/shahed96/file-i-o-in-java-2kbm</guid>
      <description>&lt;h6&gt;
  
  
  Content:
&lt;/h6&gt;

&lt;h6&gt;
  
  
  a. Reading/Writing to a text file
&lt;/h6&gt;

&lt;h6&gt;
  
  
  b. The &lt;code&gt;file&lt;/code&gt; class
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;What are Streams?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Stream&lt;/strong&gt; is an object that allows flow of data between your program and I/O devices or some files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two kinds of streams: &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;inputStream&lt;/strong&gt;: if the flow is &lt;strong&gt;into&lt;/strong&gt; the program from (keyboard or file), and the operation is called &lt;strong&gt;"Reading from"&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;outputStream&lt;/strong&gt;: if the flow is &lt;strong&gt;out of&lt;/strong&gt; the program to (screen or file), and the operation is called &lt;strong&gt;"Writing to"&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Two kinds of files to deal with:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Text files&lt;/strong&gt;: Files contain sequence of characters when viewed in text editors or read by a program, ex: files contain java program.

&lt;ul&gt;
&lt;li&gt;They also called &lt;strong&gt;ASCII files&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Can be read by human.&lt;/li&gt;
&lt;li&gt;Can move from one computer to other.&lt;/li&gt;
&lt;li&gt;Can read or write to them using text editors.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binary files&lt;/strong&gt;: Files contain sequence of binary digits.

&lt;ul&gt;
&lt;li&gt;Can be read only by programs.&lt;/li&gt;
&lt;li&gt;In java, binary files can move from one type of computer to another.&lt;/li&gt;
&lt;li&gt;Must read or write to them using only by a program.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A. Reading From A text file:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  Lord Polonius: What do you read, my lord?
&lt;/h6&gt;
&lt;h6&gt;
  
  
  Hamlet: Words, words, words. &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%2F6h1uhz3pjyyb3nm07l34.jpg" alt="Alt Text"&gt;
&lt;/h6&gt;

&lt;p&gt;The two main stream classes for reading from a text file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scanner&lt;/li&gt;
&lt;li&gt;BufferedReader&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a. Using Scanner&lt;br&gt;
Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Opening an inputStream and convert the file name to FileInputStream object&lt;/span&gt;
&lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileInputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"marks.txt"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasNextInt&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//Returns true if there is next int in the file&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;nextInt&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;//Scans the next token of the input as an int.&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The class Scanner does not care if the stream comes from the the keyboard or from a file.&lt;/li&gt;
&lt;li&gt;The Scanner class has no constructor takes a file name as it's argument (We need to convert the file name to object).&lt;/li&gt;
&lt;li&gt;A richer class to read the files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;b. Using BufferedReader&lt;br&gt;
Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;//The program opens the text file&lt;/span&gt;
&lt;span class="nc"&gt;BufferedReader&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BufferedReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileReader&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"quots.txt"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//FileReader to convert the file name to object&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
             &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readLine&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;123 hello
1 23d I am on console!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;BufferedReader&lt;/code&gt; class has no constructor takes a file name as it's argument (We need to convert the file name to object).&lt;/li&gt;
&lt;li&gt;There are &lt;strong&gt;only&lt;/strong&gt; two methods to read from the text file: &lt;code&gt;readLine&lt;/code&gt; and &lt;code&gt;read&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;read&lt;/code&gt; method reads a single character, and returns an &lt;code&gt;int&lt;/code&gt; that corresponds to the character you read.(You need to cast that int to get the character), ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;inputStram&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;read&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To signal the end of the file, the &lt;code&gt;readLine&lt;/code&gt; method returns &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;read&lt;/code&gt; method returns -1.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unlike class &lt;code&gt;Scanner&lt;/code&gt;, the class &lt;code&gt;BufferedReader&lt;/code&gt; has no &lt;br&gt;
constructor to deal with numbers. You have to read the numbers as a string then convert the string to a numeric type, ex:&lt;br&gt;
&lt;code&gt;Integer.parseInt();&lt;/code&gt; &lt;code&gt;Double.pareDouble();&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If there are multiple numbers in a single line:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a. Read the line using &lt;code&gt;readLine()&lt;/code&gt;&lt;br&gt;
 b. Use &lt;code&gt;StringTokenizer&lt;/code&gt; to decompose the string into tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;B. Writing to a text file:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The class &lt;code&gt;PrintWriter&lt;/code&gt; is the preferred class to write to a text file.&lt;br&gt;
Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;        &lt;span class="c1"&gt;//Opening the file&lt;/span&gt;
     &lt;span class="nc"&gt;PrintWriter&lt;/span&gt; &lt;span class="n"&gt;outputStream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrintWriter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileOutputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"shahed.txt"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;//use FileOutputStream to convert the file name to object&lt;/span&gt;

        &lt;span class="c1"&gt;//writing to the file &lt;/span&gt;
        &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"this line written from a java program"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;//close the connection&lt;/span&gt;
        &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this line written from a java program
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The program start with &lt;strong&gt;empty&lt;/strong&gt; file and if the file shahed.txt already exist, the old content will be lost.&lt;/li&gt;
&lt;li&gt;To test if the file is already exist, we can use the &lt;code&gt;File&lt;/code&gt; class.
&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;PrintWriter&lt;/code&gt; class has no constructor takes a file name as it's argument (We need to convert the file name to object).&lt;/li&gt;
&lt;li&gt;When the program finish writing, it should &lt;strong&gt;&lt;em&gt;close&lt;/em&gt;&lt;/strong&gt; the the stream connected to that file.&lt;/li&gt;
&lt;li&gt;To &lt;strong&gt;flush&lt;/strong&gt; the content of the buffer (temporary location for the data to be written) use the method &lt;code&gt;flush()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The method &lt;code&gt;close()&lt;/code&gt; include an invocation of &lt;code&gt;flush()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To append new text to old text, then you have to open the file with the following manner:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;PrintWriter&lt;/span&gt; &lt;span class="n"&gt;outputStreamToAppend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrintWriter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FileOutputStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"shahed.txt"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way the old content will remain and the new content will place after the old content.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;PrintWriter&lt;/code&gt; class throw &lt;code&gt;FileNotFoundException&lt;/code&gt; and it indicates that file can't be created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The &lt;code&gt;File&lt;/code&gt; class&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;File&lt;/code&gt; class constructor takes a string name (&lt;strong&gt;&lt;em&gt;Abstract name&lt;/em&gt;&lt;/strong&gt;) and checks the properties if that name, ex &lt;code&gt;exists&lt;/code&gt; methods checks if name is exists, and &lt;code&gt;isDirectory()&lt;/code&gt; tests if the name the name of a directory.
ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fruits.txt"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;canRead&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The file can not be read!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Some methods in File class:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setReadOnly()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;canWrite()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;delete()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getName()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;getPath()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;length()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Generics in java</title>
      <dc:creator>Shahed Abdulwahab</dc:creator>
      <pubDate>Tue, 15 Jun 2021 19:33:19 +0000</pubDate>
      <link>https://dev.to/shahed96/generics-in-java-1p5m</link>
      <guid>https://dev.to/shahed96/generics-in-java-1p5m</guid>
      <description>&lt;h6&gt;
  
  
  You can have this dish prepared with any type of meat or fish.
&lt;/h6&gt;

&lt;h6&gt;
  
  
  -Entry on a restaurant menu.
&lt;/h6&gt;

&lt;p&gt;Classes and methods can have &lt;em&gt;Type parameter&lt;/em&gt; T, and the type parameter may then have any reference type or class type plugged in for the type parameter.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The class with type parameter is called &lt;strong&gt;Generic class&lt;/strong&gt; or &lt;strong&gt;Parameterized class&lt;/strong&gt;,
example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;//T is a parameter for a type&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Sample&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;getData&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A generic constructor name has no type parameter with angular brackets  with it's name, But it can use the type parameter T with it's parameters.&lt;/li&gt;
&lt;li&gt;You can not plug in a primitive type for a type parameter, for example: if we want &lt;strong&gt;instantiate&lt;/strong&gt; the generic class Sample with integer type, we would say:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Sample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sample&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Sample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;(&lt;/span&gt;&lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A type parameter cannot be used everywhere a type name can be used. you can not use the type parameter T with

&lt;code&gt;new&lt;/code&gt;

operator to create an object.
example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//the first T is legal while the  second is illegal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A generic class definition can have any number of type parameter, example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sample2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;T2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="no"&gt;T1&lt;/span&gt; &lt;span class="n"&gt;data1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="no"&gt;T2&lt;/span&gt; &lt;span class="n"&gt;data2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
     &lt;span class="o"&gt;.&lt;/span&gt;
     &lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A generic class can not be a an exception class, for example the following exception class will generate a compiler error:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sample&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;//illegal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-To use &lt;strong&gt;compareTo&lt;/strong&gt; method in your generic class, you need to ensure that the class implements &lt;strong&gt;comparable&lt;/strong&gt; interface  and begin with the following definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sample&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Comparable&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the part &lt;code&gt;extends Comparable&lt;/code&gt; is called &lt;strong&gt;bound&lt;/strong&gt; on the type parameter T, and a &lt;strong&gt;bound&lt;/strong&gt; may be a class name, for example the following says that &lt;strong&gt;only&lt;/strong&gt; descendent classes of &lt;em&gt;Employee&lt;/em&gt; may be plugged in for T:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Sample&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Generic Methods&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the generic method can be member of a generic class or nongeneric class.
ex:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;getMidPoint&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>generic</category>
    </item>
    <item>
      <title>Collections in java</title>
      <dc:creator>Shahed Abdulwahab</dc:creator>
      <pubDate>Sun, 28 Mar 2021 18:48:33 +0000</pubDate>
      <link>https://dev.to/shahed96/collections-in-java-o45</link>
      <guid>https://dev.to/shahed96/collections-in-java-o45</guid>
      <description>&lt;h2&gt;
  
  
  &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NvtGosTt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdv78nxvc7n3u7ttsa8i.png" alt="Alt Text"&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Collections:
&lt;/h2&gt;

&lt;h6&gt;
  
  
  Put all your eggs in one basket and watch that basket.
&lt;/h6&gt;

&lt;h6&gt;
  
  
  -Mark Twain
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;Collection&lt;/strong&gt;: is a data structure for holding elements.&lt;br&gt;
&lt;strong&gt;Collection&amp;lt;T&amp;gt;&lt;/strong&gt;: is the highest level of the java's framework for collection. &lt;/p&gt;

&lt;p&gt;-Two main interfaces that extend &lt;em&gt;Collection&amp;lt;T&amp;gt;&lt;/em&gt; interface:&lt;br&gt;
1.&lt;strong&gt;Set&amp;lt;T&amp;gt;&lt;/strong&gt; interface: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Does not allow duplication&lt;/li&gt;
&lt;li&gt;Does not impose an order on the elements &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.&lt;strong&gt;List&amp;lt;T&amp;gt;&lt;/strong&gt; interface: (Allow duplication)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Allow duplication&lt;/li&gt;
&lt;li&gt;Impose an order on the elements
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K86WoIWS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uq4qa2smu95hs0gclghy.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;-Two Abstract classes used (for convenience) to implement &lt;em&gt;Set&amp;lt;T&amp;gt;&lt;/em&gt; and &lt;em&gt;List&amp;lt;T&amp;gt;&lt;/em&gt; interface:&lt;br&gt;
1.&lt;strong&gt;AbstractSet&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;br&gt;
2.&lt;strong&gt;AbstractList&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--73PWzUh3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/400q36w6rh2orgj52r9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--73PWzUh3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/400q36w6rh2orgj52r9i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-Another Abstract class implements &lt;em&gt;Collection&amp;lt;T&amp;gt;&lt;/em&gt; interface which is  &lt;strong&gt;AbstractCollection&amp;lt;T&amp;gt;&lt;/strong&gt;, and it is a &lt;em&gt;skeleton&lt;/em&gt; class for the &lt;em&gt;Collection&amp;lt;T&amp;gt;&lt;/em&gt; interface&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mDni1qAr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l371dfokcjebhvgem2k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mDni1qAr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l371dfokcjebhvgem2k.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-If you want a class that implement The Set&amp;lt;t&amp;gt; interface, use the class &lt;strong&gt;HashSet&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;HashSet&amp;lt;T&amp;gt;&lt;/strong&gt;: is the &lt;em&gt;concrete&lt;/em&gt; class implements the Set interface.&lt;br&gt;
It can be used as a base class if you want to implement the Set interface.&lt;br&gt;
-It is implemented using  a hash table.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7dkM3uvv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tduihu02ek8mv51aa954.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7dkM3uvv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tduihu02ek8mv51aa954.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-If you want a &lt;em&gt;concrete&lt;/em&gt; class that implement The List&amp;lt;t&amp;gt; interface, use &lt;br&gt;
 either the class &lt;strong&gt;ArrayList&amp;lt;T&amp;gt;&lt;/strong&gt; or &lt;strong&gt;Vector&amp;lt;T&amp;gt;&lt;/strong&gt;&lt;br&gt;
They can be used as a base class if you want to implement the Set interface using your own class.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--etZRQ-WB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41b2mgr5xr85hl2e8fhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--etZRQ-WB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41b2mgr5xr85hl2e8fhw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>interface</category>
    </item>
  </channel>
</rss>
