Showing posts with label combine. Show all posts
Showing posts with label combine. Show all posts

Sunday, 29 March 2020

TableSaw: where: filter the elements that matches given criteria

‘where’ function is used to filter the elements that matches given criteria.
IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30));

Above statement return new filter that contains the elements that are < 30.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 2, 4, 103, 24, 53, 23, 12, 11, 56, 6 };

  IntColumn intColumn = IntColumn.create("My Numbers", numbers);

  IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30));


  System.out.println(intColumn.print() + "\n");
  
  filteredElements.setName("intColumn < 30");
  System.out.println(filteredElements.print());
 }
}

Output
Column: My Numbers
2
4
103
24
53
23
12
11
56
6


Column: intColumn < 30
2
4
24
23
12
11
6

One advantage of ‘where’ function is, you can combine the filters.

Example
IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30).and(intColumn.isGreaterThan(20)));

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 2, 4, 103, 24, 53, 23, 12, 11, 56, 6 };

  IntColumn intColumn = IntColumn.create("My Numbers", numbers);

  IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30).and(intColumn.isGreaterThan(20)));

  System.out.println(intColumn.print() + "\n");

  filteredElements.setName("intColumn < 30 && intColumn > 20");
  System.out.println(filteredElements.print());
 }
}

Output

Column: My Numbers
2
4
103
24
53
23
12
11
56
6


Column: intColumn < 30 && intColumn > 20
24
23


Previous                                                    Next                                                    Home

Thursday, 12 March 2020

Combine hashmaps with same type

Below snippet copies multiple maps to other map of same type.
private static <K, V> Map<K, V> combineMaps(Map<K, V>... maps) {
	if (maps == null || maps.length == 0) {
		return Collections.EMPTY_MAP;
	}

	Map<K, V> result = new HashMap<>();

	for (Map<K, V> map : maps) {
		result.putAll(map);
	}
	return result;
}

App.java
package com.sample.app;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class App {

	private static <K, V> Map<K, V> combineMaps(Map<K, V>... maps) {
		if (maps == null || maps.length == 0) {
			return Collections.EMPTY_MAP;
		}

		Map<K, V> result = new HashMap<>();

		for (Map<K, V> map : maps) {
			result.putAll(map);
		}
		return result;
	}

	public static void main(String[] args) {
		Map<Integer, String> map1 = new HashMap() {
			{
				put(1, "Ram");
				put(2, "Rahim");
				put(3, "Joel");
			}
		};

		Map<Integer, String> map2 = new HashMap() {
			{
				put(3, "JaiDeep");
				put(4, "Krishna");
				put(6, "Naveed");
			}
		};

		Map<Integer, String> combinedMap = combineMaps(map1, map2);

		for (int key : combinedMap.keySet()) {
			System.out.println(key + " : " + combinedMap.get(key));
		}
	}

}

Output
1 : Ram
2 : Rahim
3 : JaiDeep
4 : Krishna
6 : Naveed

You may like