CSV: How to sort by several columns in Java8



CSV_SortByThreeColumns.java

package example.stream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Comparator;
import java.util.stream.Stream;
/**
 * Let's say there is a comma delimited file with the first line describing column names:
 * Name, State, Years of experience
 * Here is how to sort lines from the most experienced people, then by state and name
 */

public class CSV_SortByThreeColumns {
    private static void printSortedByExperienceAndStateAndNameFromStreamedLines(Stream<String> stream){
        Comparator<String> byExperience = Comparator.comparing(
                line -> Integer.parseInt(line.split(",")[2]), Comparator.reverseOrder()
        );// by Years of experience - reversed
        Comparator<String> byExperienceAndState = byExperience.thenComparing(
                line -> line.split(",")[1]
        );// by State
        Comparator<String> comparator = byExperienceAndState.thenComparing(
                line -> line.split(",")[0]
        );// by Name
        stream
                .skip(1L)
                .sorted(comparator)
                .forEach(System.out::println);
    }
    public static void main(String[] args) throws IOException {
        //BufferedReader reader = Files.newBufferedReader(Paths.get("a.csv"));
        BufferedReader reader = new BufferedReader(new StringReader(
                        "Name,State,Years of experience\n" +
                        "John,MN,1\n" +
                        "Zita,CA,2\n" +
                        "Adam,CA,5\n" +
                        "Dough,CA,20\n" +
                        "Richard,MN,6\n" +
                        "Robert,CT,6"));
        printSortedByExperienceAndStateAndNameFromStreamedLines(reader.lines());
    }
}

Output:

Dough,CA,20
Robert,CT,6
Richard,MN,6
Adam,CA,5
Zita,CA,2
John,MN,1

Links: stream(package summary), Stream<T>, Comparator<T>

Posted in Java8, workday | Leave a comment

CSV: How to skip the first line in Java8



CSV_SkipFirstLine.java

package example.stream;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.stream.Stream;

/**
 * Let's say there is a comma delimited file with the first line describing column names.
 * In this example a person name and state.
 * Here is how to skip the first line using Java8 stream
 */
public class CSV_SkipFirstLine {
    private static void skipFirstAndPrintRestOfLinesFromStream(Stream<String> stream){
        stream
                .skip(1L)
                .forEach(System.out::println);
    }
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader(
                        "Name,State\n" +
                        "John,MN\n" +
                        "Zita,CA\n" +
                        "Adam,CA\n" +
                        "Dough,CA\n" +
                        "Richard,MN\n" +
                        "Robert,CT"));
        skipFirstAndPrintRestOfLinesFromStream(reader.lines());
    }
}

Output:

John,MN
Zita,CA
Adam,CA
Dough,CA
Richard,MN
Robert,CT
Posted in Java8, workday | Leave a comment

Handy notepad in Chrome tab or window

There is nice option to create editable tab on window in Chrome browser with URL like this:
data:text/html, <html contenteditable>

It is possible to save it into the file after pressing CTRL+<S>

Posted in workday | Leave a comment

Sky above from freezing cold nights

Nights are long in the Winter, with many clear nights, but with very cold temperatures as well. It looks like Kakslauttanen Arctic Resort in Finland built good solution how to observe beauty of the sky in Winter.

Posted in family | Leave a comment

Bubble tent to look at stars

Hmm, maybe bubble tent is right idea for fidget weather of Minnesota. One could be warm and look at stars, maybe even observe by telescope.

Posted in workday | Leave a comment

SVG: Elliptical arc path

Scalable Vector Graphics allows elliptical paths. Here is rendered image, which can help to realize meaning of the parameters. In case below all elipses and circles are have end points [0,0] and [70.71, 70.71]. Second point is on the circle with radius 100. Arc with small “a” means relative dimensions.

Posted in workday | Leave a comment

find a files with -maxdepth and -mindepth

Unix utility find allows many arguments. Here is how one can limit searched directories. Arguments -maxdepth and -mindepth should be used first before let’s say -name or -type.

Let’s created four sub directories to investigate how it works:

$ mkdir -p a/b/c/d

$ find a
a
a/b
a/b/c
a/b/c/d

$ find a -maxdepth 2
a
a/b
a/b/c

$ find a -maxdepth 2 -mindepth 2
a/b/c

$ find a -mindepth 2
a/b/c
a/b/c/d

See also find a files with mmin

Posted in workday | Leave a comment

Eclipse weather

Weather for Solar eclipse on August 21, 2017 across USA determined how people could experience such rare event.

Posted in workday | Leave a comment

Eclipse August 21, 2017

Eclipse on August 21, 2017 will be visible in Minnesota only as a partial eclipse. Total eclipse can be visible south of Minnesota, probably good place to go will be places in Nebraska, Kansas or Missouri. Totality will be around 1 PM CDT.
See more at NASA page SE2017Aug21Tgoogle or interactive map.

Next closest opportunity will be on 4 minutes of totality August 4, 2024 in Indianopolis at 2:08 PM CDT.

Posted in workday | Leave a comment

Sexagesimal one character digits

Sexagesimal (base 60) is a numeral system with sixty as its base. In some form with help of decimal digits  is used for measuring time, angles, and geographic coordinates. There are no special symbols for it’s digits like in binary system (0,1) or decimal system (0,1,2,3,4,5,6,7,8,9). For certain application, there could be an option to use existing characters from ASCII table and assign them value of sexagesimal digits [0-9A-Za-x]. Value of such digit is monotonically growing, so it possible to use it for ordering and sorting.

Transformation table  could look like this:

0: 0123456789
1: ABCDEFGHIJ
2: KLMNOPQRST
3: UVWXYZabcd
4: efghijklmn
5: opqrstuvwx

Here are an example transformations from decimal system into such sexagesimal system with it’s digits defined above:
00:00 -> 00, 01:01 -> 11, 05:10 -> 5A, 10:10 -> AA, 12:10 -> CA, 12:30 -> CU, 12:59 -> Cx, 12:33 -> CX, 23:50 -> No, 10:36 -> Aa, 11:59:59 -> Bxx, 18:30:00 -> IU0.

/* Example of JAVA method for n60 0..59 */
public static char sexagesimal_char(int n60) {
   if (n60 < 10) return (char) (48 + n60);//or (‘0’ + n60)
   if (n60 < 36) return (char) (55 + n60);//65-10 or (‘A’ + n60 – 10)
   return (char) (61 + n60);              //97-36 or (‘a’ + n60 – 36)
}

Posted in workday | Leave a comment