<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://nozaki.me/roller/roller-ui/styles/rss.xsl" media="screen"?><rss version="2.0" 
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
  <title>Kohei Nozaki&apos;s blog</title>
  <link>https://nozaki.me/roller/kyle/</link>
      <atom:link rel="self" type="application/rss+xml" href="https://nozaki.me/roller/kyle/feed/entries/rss?cat=Java" />
    <description>Notes of my experiments</description>
  <language>en-us</language>
  <copyright>Copyright 2024</copyright>
  <lastBuildDate>Wed, 1 May 2024 08:28:13 +0000</lastBuildDate>
  <generator>Apache Roller 5.2.0</generator>
        <item>
    <guid isPermaLink="true">https://nozaki.me/roller/kyle/entry/array-sorting-with-java8-lambdas</guid>
    <title>Array sorting with Java8 idioms</title>
    <dc:creator>Kohei Nozaki</dc:creator>
    <link>https://nozaki.me/roller/kyle/entry/array-sorting-with-java8-lambdas</link>
        <pubDate>Sun, 28 Jun 2015 07:14:06 +0000</pubDate>
    <category>Java</category>
    <category>java8</category>
            <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Thanks to Java8, now we can write sorting with comparators in very simple idiom. assume we have a JavaBean named &lt;code&gt;Stock&lt;/code&gt; as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;public class Stock {
    private final String symbol;
    private final LocalDate baseDate;
    private final int price;

    public Stock(String symbol, LocalDate baseDate, int price) {
        this.symbol = symbol;
        this.baseDate = baseDate;
        this.price = price;
    }
...&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;We have following dataset:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;final Stock[] data = {
        new Stock(&quot;ORCL&quot;, LocalDate.of(2015, 3, 1), 10),
        new Stock(&quot;ORCL&quot;, LocalDate.of(2015, 3, 2), 11),
        new Stock(&quot;ORCL&quot;, LocalDate.of(2015, 3, 3), 12),
        new Stock(&quot;JAVA&quot;, LocalDate.of(2015, 3, 1), 100),
        new Stock(&quot;JAVA&quot;, LocalDate.of(2015, 3, 2), 110),
        new Stock(&quot;JAVA&quot;, LocalDate.of(2015, 3, 3), 120),
        new Stock(&quot;GOOG&quot;, LocalDate.of(2015, 3, 1), 1000),
        new Stock(&quot;GOOG&quot;, LocalDate.of(2015, 3, 2), 1001),
        new Stock(&quot;GOOG&quot;, LocalDate.of(2015, 3, 3), 1002),
};&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;How to get an array that sorted by &lt;code&gt;symbol&lt;/code&gt; in ascending order, but &lt;code&gt;baseDate&lt;/code&gt; in descending order when they have same &lt;code&gt;symbol&lt;/code&gt;? the following code works:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;Comparator&amp;lt;Stock&amp;gt; p1 = Comparator.comparing(Stock::getSymbol);
Comparator&amp;lt;Stock&amp;gt; p2 = Comparator.comparing(Stock::getBaseDate).reversed();
Comparator&amp;lt;Stock&amp;gt; p3 = p1.thenComparing(p2);
Arrays.sort(data, p3);

System.out.println(Arrays.toString(data));&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The variable &lt;code&gt;p1&lt;/code&gt; stores a &lt;code&gt;Comparator&lt;/code&gt; that sorts beans with &lt;code&gt;symbol&lt;/code&gt;, and &lt;code&gt;p2&lt;/code&gt; does the same for &lt;code&gt;baseDate&lt;/code&gt; but in reversed order, and &lt;code&gt;p3&lt;/code&gt; stores a composit comparator for these two. also you can add more comparators with code like &lt;code&gt;.thenComparing(p4).thenComparing(p5)&amp;#8230;&amp;#8203;&lt;/code&gt; as well. this yields the following output:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;[Stock{symbol=&apos;GOOG&apos;, baseDate=2015-03-03, price=1002}
, Stock{symbol=&apos;GOOG&apos;, baseDate=2015-03-02, price=1001}
, Stock{symbol=&apos;GOOG&apos;, baseDate=2015-03-01, price=1000}
, Stock{symbol=&apos;JAVA&apos;, baseDate=2015-03-03, price=120}
, Stock{symbol=&apos;JAVA&apos;, baseDate=2015-03-02, price=110}
, Stock{symbol=&apos;JAVA&apos;, baseDate=2015-03-01, price=100}
, Stock{symbol=&apos;ORCL&apos;, baseDate=2015-03-03, price=12}
, Stock{symbol=&apos;ORCL&apos;, baseDate=2015-03-02, price=11}
, Stock{symbol=&apos;ORCL&apos;, baseDate=2015-03-01, price=10}
]&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</description>          </item>
    <item>
    <guid isPermaLink="true">https://nozaki.me/roller/kyle/entry/what-i-learned-from-the</guid>
    <title>What I learned from the book Core Java for the Impatient</title>
    <dc:creator>Kohei Nozaki</dc:creator>
    <link>https://nozaki.me/roller/kyle/entry/what-i-learned-from-the</link>
        <pubDate>Fri, 19 Jun 2015 09:07:22 +0000</pubDate>
    <category>Java</category>
    <category>book</category>
            <description>&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;I&amp;#8217;m reading a book named &lt;a href=&quot;http://horstmann.com/javaimpatient/&quot;&gt;Core Java for the Impatient&lt;/a&gt;. I leave some notes that what I learned from that book.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_array_construction&quot;&gt;Array construction&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You can omit the &lt;code&gt;new int[]&lt;/code&gt; statement when you declare a variable as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;int[] array = {1, 2, 3};&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You can&amp;#8217;t omit it when you don&amp;#8217;t declare a variable at the same time (in other words, when you use an existing variable).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;For example, the following is illegal:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;int[] array;
array = {2, 3, 4};&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You need to write like the following instead:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;int[] array;
array = new int[]{2, 3, 4};&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_numberformat&quot;&gt;NumberFormat&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The &lt;code&gt;NumberFormat&lt;/code&gt; class provides some useful text formatting functions for numeric variables.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;_currency_formatting&quot;&gt;Currency formatting&lt;/h3&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;final BigDecimal val1 = new BigDecimal(&quot;0.126&quot;);
final NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(currencyInstance.format(val1));&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Yields:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;$0.13&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect2&quot;&gt;
&lt;h3 id=&quot;_percentage_formatting&quot;&gt;Percentage formatting&lt;/h3&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;final BigDecimal val2 = new BigDecimal(&quot;0.126&quot;);
final NumberFormat percentInstance = NumberFormat.getPercentInstance(Locale.US);
System.out.println(percentInstance.format(val2));&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Yields:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;13%&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;They automatically adds the symbol and &lt;strong&gt;round up on 6 and round down on 5&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_wildcard_for_the_classpath&quot;&gt;Wildcard for the classpath&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Consider a class which depends on the class &lt;code&gt;corejava.jar1.Foo&lt;/code&gt; and &lt;code&gt;corejava.jar2.Bar&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;package corejava.classpath;

public class Main {
    public static void main(String[] args) {
        System.out.println(new corejava.jar1.Foo());
        System.out.println(new corejava.jar2.Bar());
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You have these classes in separated three jar files in the &lt;code&gt;/tmp/jar&lt;/code&gt; directory as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;$ ls -l /tmp/jar
total 24
-rw-r--r--  1 kyle  wheel  2192 Jun 19 16:06 classpath-1.0-SNAPSHOT.jar
-rw-r--r--  1 kyle  wheel  1962 Jun 19 16:04 jar1-1.0-SNAPSHOT.jar
-rw-r--r--  1 kyle  wheel  1962 Jun 19 16:04 jar2-1.0-SNAPSHOT.jar&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;In this setup, you can specify all of three jar files with the wildcard &lt;code&gt;*&lt;/code&gt; as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;$ java -cp &apos;/tmp/jar/*&apos; corejava.classpath.Main
corejava.jar1.Foo@511d50c0
corejava.jar2.Bar@5e2de80c&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Note that you can&amp;#8217;t use complex wildcard that smarter UNIX shells recognize. in other words, it doesn&amp;#8217;t recognize patterns like &lt;code&gt;*.jar&lt;/code&gt;. for example, the following command won&amp;#8217;t work:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;$ java -cp &apos;/tmp/jar/*.jar&apos; corejava.classpath.Main
Error: Could not find or load main class corejava.classpath.Main&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_including_an_image_to_javadoc&quot;&gt;Including an image to javadoc&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Consider you have a class named &lt;code&gt;Dog&lt;/code&gt; in a Maven project. you want to include an image of a dog named &lt;code&gt;chihuahua.jpg&lt;/code&gt; to the javadoc of the class. in such case you can write the class as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;package corejava.main;

/**
 * The class represents a dog. &amp;lt;br&amp;gt; &amp;lt;img src=&quot;doc-files/chihuahua.jpg&quot; alt=&quot;a cute chihuahua dog&quot;&amp;gt;
 */
public class Dog {

    /**
     * The dog will say woof.
     */
    public void bark() {
        System.out.println(&quot;Woof!&quot;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Next, put the &lt;code&gt;chihuahua.jpg&lt;/code&gt; in &lt;code&gt;src/main/javadoc/corejava/main/doc-files&lt;/code&gt;. structure under the &lt;code&gt;src/main&lt;/code&gt; will be:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;.
|-- java
|   `-- corejava
|       `-- main
|           `-- Dog.java
|-- javadoc
|   `-- corejava
|       `-- main
|           `-- doc-files
|               `-- chihuahua.jpg
`-- resources&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Then execute &lt;code&gt;mvn javadoc:javadoc&lt;/code&gt; in the top of the project directory. javadoc will be produced in the &lt;code&gt;target/site/apidocs&lt;/code&gt; directory. open &lt;code&gt;index.html&lt;/code&gt; in that directory and go to the documentation of &lt;code&gt;Dog&lt;/code&gt; class, you will see the image of a dog as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/roller/kyle/mediaresource/a0e5b322-584d-4e5d-a246-e6f824d1085c&quot; alt=&quot;a0e5b322 584d 4e5d a246 e6f824d1085c&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_including_a_link_to_relevant_part_of_javadoc&quot;&gt;Including a link to relevant part of javadoc&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Consider you have a class named &lt;code&gt;Cat&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;package corejava.main;

/**
 * Represents a cat.
 */
public class Cat {

    /**
     * The cat starts meowing.
     *
     * @see corejava.main.Dog#bark()
     * */
    public void meow() {
        System.out.println(&quot;Meow!&quot;);
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;This produces the following javadoc:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;imageblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;img src=&quot;/roller/kyle/mediaresource/34963f24-9e7f-4367-a56f-b9a464553211&quot; alt=&quot;34963f24 9e7f 4367 a56f b9a464553211&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;There are some of more useful syntaxes such as link to external URLs.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_method_references&quot;&gt;Method references&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;The following three statements produce identical &lt;code&gt;Comparator&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;Comparator&amp;lt;String&amp;gt; c1 = new Comparator&amp;lt;String&amp;gt;() {
    @Override
    public int compare(String x, String y) {
        return x.compareTo(y);
    }
};
Comparator&amp;lt;String&amp;gt; c2 = (x, y) -&amp;gt; x.compareTo(y);
Comparator&amp;lt;String&amp;gt; c3 = String::compareTo;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_constructor_references&quot;&gt;Constructor references&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;Constructor references are useful with Streams. consider you have a class named &lt;code&gt;Dog&lt;/code&gt;:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;public class Dog {
    private String name;

    public Dog(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return &quot;Dog{&quot; + &quot;name=&apos;&quot; + name + &apos;\&apos;&apos; + &apos;}&apos;;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;You can convert a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;/code&gt; that contains name of dogs to a &lt;code&gt;Stream&amp;lt;Dog&amp;gt;&lt;/code&gt; as follows:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;List&amp;lt;String&amp;gt; dogs = Arrays.asList(&quot;Snoopy&quot;, &quot;Spike&quot;, &quot;Olaf&quot;);
Stream&amp;lt;Dog&amp;gt; dogStream = dogs.stream().map(Dog::new);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;paragraph&quot;&gt;
&lt;p&gt;If you need to create a &lt;strong&gt;typed&lt;/strong&gt; array from that &lt;code&gt;Stream&amp;lt;Dog&amp;gt;&lt;/code&gt;, you can use following idiom:&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;literalblock&quot;&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;pre&gt;Dog[] dogArray = dogStream.toArray(Dog[]::new);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;sect1&quot;&gt;
&lt;h2 id=&quot;_references&quot;&gt;References&lt;/h2&gt;
&lt;div class=&quot;sectionbody&quot;&gt;
&lt;div class=&quot;ulist&quot;&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&quot;http://kastork.github.io/2011/02/SVG-Images-in-javadocs/&quot;&gt;Kirk&amp;#8217;s Blog; Adding SVG Images to Javadocs in a Maven Project&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>          </item>
  </channel>
</rss>