Find each column in a table in my java 8 stream

I am using HtmlUnit to get an HtmlTable. I am trying to get a List of cells of each column.

So far, in the code I've tried I can find the first column. How can I iterate through each column and run some code in them?

I'd like to make sure they're all sorted alphabetically, but I just need to figure out where to place that code.

EDIT: I found my answer. I think I worded the question wrong. I needed to get each column and put them into their own collection. In the original example, I showed only column1. But I need EACH column (based upon how many cells are in each row). Below is some code that worked. But it may be able to be optimized better.

HtmlPage htmlPage = webClient.getPage("http://localhost:8080/myurl");

    HtmlTable myTable = htmlPage.getHtmlElementById("mytable");

    // find the number of columns by grabbing the first row and returning the number
    // of cells within the first row
    int numberOfColumns = myTable.getRows().stream().map(row -> row.getCells()).findFirst().get()
            .size();

    // initialize columns
    List<List<String>> columns = new ArrayList<List<String>>(numberOfColumns);

    // initialize new arraylists for each column based upon the number of columns
    for (int i = 0; i < numberOfColumns; i++)
        columns.add(new ArrayList<>());

    // iterate through each column
    for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {

        // iterate through each row
        for (int rowIndex = 0; rowIndex < myTable.getRows().size(); rowIndex++) {

            String asText = myTable.getCellAt(rowIndex, columnIndex).asText();
            columns.get(columnIndex).add(asText);
        }
    }

    //iterate through the columns and do stuff!
    columns.forEach(a -> {
        //do stuff to the column such as verify it was sorted, or sort it yourself etc
        System.out.println("column" + a.toString());
        a.forEach(b -> {
            //do stuff 
            LOG.info(b);
        });
    });

You can just do this as part of your join as single stream:

webClient.getPage("http://localhost:8080/myUrl")
         .getHtmlElementById("myTable")
         .getRows()
         .stream()
         .map(row -> row.getCells().stream().findFirst().get().asText())
         .sort((o1, o2) -> o1.compareTo(o2)) // make alphabetical
         .collect(Collectors.joining("|"));

Stream In Java, is not a data structure instead it takes input from the Collections, Arrays or I/O channels. In this article, we will show you how to use Java 8 Stream Collectors to group by, count, sum and sort a List.. 1. Group By, Count and Sort. 1.1 Group by a List and display the total count of it.

You can collect it to a List of List's:

List<List<HtmlTableCell>> columns = 
                          myTable.getRows()
                                 .stream()
                                 .map(row -> row.getCells()
                                                .stream()
                                                .collect(Collectors.toList())
                                 .collect(Collectors.toList());

And then when you need to log:

LOG.info(columns.stream()
                .flatMap(List::stream)                    
                .map(m -> m.asText())
                .sorted()         //Sort the list
                .collect(Collectors.joining("|")));

What is stream pipelining in Java 8?, Java Streams can be defined as a sequence of elements from a source that supports Table of Contents 1. A Collection is an in-memory data structure, which holds all the values that the data return the stream itself so you can chain multiple method calls in a row. Java 8 – Find or remove duplicates in Stream · Java 8  We create a stream of Widget objects via Collection.stream(), filter it to produce a stream containing only the red widgets, and then transform it into a stream of int values representing the weight of each red widget. Then this stream is summed to produce a total weight.

If you want to get the table as a list of lists (List<List<HtmlTableCell>>) this will do it

List<List<HtmlTableCell>> table = myTable.getRows().stream()
        .map(row -> row.getCells().stream().collect(Collectors.toList())
        .collect(Collectors.toList());

Or, if you won't need the List later, you can skip collecting to list and execute your code instead

List<List<HtmlTableCell>> table = myTable.getRows().stream()
        .map(row -> row.getCells().stream().collect(Collectors.toList())
        .forEachOrdered(cellList -> System.out.println(cellList));

Java 8 Stream - Java Stream Example, In other words, all the separate streams that were generated when using map(​Arrays::stream) get amalgamated or “flattened” into one single stream. Figure 2  The point is you use just a small amount of memory and don't hold all of the data in memory at once. (Like, when you "stream" a song.) Of course, Java 8 morphed the definition entirely to make it be "a stateless process that's easy to parallelize," which is why you find nothing ironic about "using streams" to index into a big List.

As you want a List<HtmlTableCell> representing the data in a given column of your html table you need to use getCellAt which takes a row index and column index.

IntStream.range(0, numberOfColumns)
         .mapToObj(colIndex -> IntStream.range(0, numberOfRows)
             .mapToObj(rowIndex -> myTable.getCellAt(rowIndex, colIndex)).collect(toList())) 
         .collect(toList()); 

where numberOfColumns should be replaced with the number of columns in your HTML table and the numberOfRows should be replaced with the number of rows in your HTML table.

This will yield a List<List<HtmlTableCell>> where each List<HtmlTableCell> is all the cells for each column.


For completeness here is how you can sort each List<HtmlTableCell> i.e. each columns data.

List<List<HtmlTableCell>> result = 
     IntStream.range(0, numberOfColumns)
              .mapToObj(colIndex -> IntStream.range(0, numberOfRows)
                     .mapToObj(rowIndex -> myTable.getCellAt(rowIndex, colIndex))
                     .sorted(Comparator.comparing(HtmlTableCell::asText))
                     .collect(toList())) 
              .collect(toList()); 

and log it:

// concatenate each cell data of each column separated by a pipe and then separate each column data by a line separator.
String joined = result.stream()
      .map(l -> l.stream().map(HtmlTableCell::asText).collect(Collectors.joining("|")))
      .collect(Collectors.joining(System.lineSeparator()));
// log it! 
LOG.info(joined);

Note that if all you're doing is logging then it's not worth collecting to an intermediate List<List<HtmlTableCell>> rather, you can get the result you require as:

 String joined = IntStream.range(0, numberOfColumns)
                          .mapToObj(colIndex -> IntStream.range(0, numberOfRows)
                                .mapToObj(rowIndex -> myTable.getCellAt(rowIndex, colIndex).asText())
                                .sorted()
                                .collect(Collectors.joining("|")))
                        .collect(Collectors.joining(System.lineSeparator()));
LOG.info(joined);

Part 2: Processing Data with Java SE 8 Streams, 01503, The column <columnName> on table <tableName> has been modified by adding a not null constraint. See the next exception. 08001, Unable to open stream on socket: ' <error> '. 22005, Unrecognized Java SQL type <​datatypeName> . For each column, at least one of the rows must have a non-​parameter. Using a powerful API – Java 8 Stream Map; Now let’s do details with examples! Related posts: – Java 8 – Java 8 Streams. Java Transform a List with Traditional Solution by Looping Examples. Before Java 8, for mapping a List Objects, we use the basic approach with looping technical solution.

SQL error messages and exceptions, Lets take a simple example first and then we will see the examples of stream filter with other methods of the stream. A Simple Example of Java Stream Filter(). In  Stream.reduce() is a terminal operation that performs a reduction on the elements of the stream. It applies a binary operator (accumulator) to each element in the stream, where the first operand is the return value of the previous application, and the second one is the current stream element.

Java 8 Stream Filter with examples, Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence the Stream in detail: Stream forEach(Consumer action): This method performs an action for each element of the stream. Get the stream. As of Java 8, we can also use the Stream API to find an element in a List. To find an element matching specific criteria in a given list, we: invoke stream() on the list

How to print elements of a Stream in Java 8, Learn how to use Cucumber data tables to include mock data in a readable manner. When I search for books by author Erik Larson. Then I find 1 The meaning of each column is not always so obvious, though. 8. Scenario: Correct non-zero number of books found by author return books.stream(). In this tutorial, we will show you few Java 8 examples to demonstrate the use of Streams filter(), collect(), findAny() and orElse(). 1. Streams filter() and collect() 1.1 Before Java 8, filter a List like this :

Hot Questions

  • Regular expression to check whitespace in the beginning and end of a string9989
  • GCC compiler error: "redefinition...previously defined"2314
  • Why do objects of the same class have access to each other's private data?8525
  • I need help formatting decimal places in a Text Field using Swift 41227
  • Testing Facebook Share Dialog with localhost - "Unable to resolve object at URL http://localhost"6665
  • turning a *pointer elements to an 1D array374
  • NetworkX: draw graph in layers5356
  • How to set a fragment as default fragment for BottomNavigation?8422
  • How do I Select All Checkboxes When First Radio Input Selected & Uncheck All Checkboxes when Second radio input selected? (Javascript)9283
  • How to plot data from multiple two column text files with legends in Matplotlib?3387
  • Pandas dataframe: Check if data is monotonically decreasing9640
  • java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String2996
  • jQuery to add CSS class to selected radio button3349
  • How do I reference the containing class from within a static method when "this" is already taken by the calling object?9094
  • JTextField to display only 2 decimal places3765
  • php: Unable to take value from the user by using submit button6975
  • Random "An existing connection was forcibly closed by the remote host." after a TCP reset4765
  • Can't find android.support.design.widget.Snackbar in support design library1002
  • Filter input text only accept number and dot vue.js8057
  • Convert latitude/longitude point to a pixels (x,y) on mercator projection1304