めも

技術メモとその他

log4j to console

log4j.rootCategory=INFO, consoleLog

 

log4j.category.jp.terasoluna=DEBUG

log4j.category.org.springframework=INFO

log4j.category.org.apache.struts=INFO

 

log4j.appender.consoleLog=org.apache.log4j.ConsoleAppender

log4j.appender.consoleLog.Target=System.out

log4j.appender.consoleLog.layout=org.apache.log4j.PatternLayout

log4j.appender.consoleLog.layout.ConversionPattern=[%d{yyyy/MM/dd HH:mm:ss}][%p][%c{1}] %m%n

using Set not to duplicate values

sample code below

 

package homeWork;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

public class distinctSample2 {

 

public static void main(String[] args) {

 

Set<Map<String,Object>>  countries = new HashSet<Map<String, Object>>();

 

 

Map<String, Object> country;

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201801" );

country.put( "population", 1000 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201802" );

country.put( "population", 1200 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201804" );

country.put( "population", 800 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201803" );

country.put( "population", 1000 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201803" );

country.put( "population", 1000 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201804" );

country.put( "population", 800 );

countries.add( country );

 

countries.stream().forEach(System.out::println);

 

List<Map<String, Object>> Listcountries=new ArrayList<Map<String, Object>>();

 

Listcountries.addAll(countries);

 

System.out.println("Set to List");

Listcountries.stream().forEach(System.out::println);

 

}

 

}

 

result :

f:id:annazola:20191018020915p:plain

 

another way(Set to List)

List<Map<String, Object>> Listcountries2=new ArrayList<Map<String, Object>>(countries);

 

 

how stream().distinct() works - list of maps

sample code below:

 

package homeWork;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;

 

public class distinctSample {

 

public static void main(String[] args) {

 

List<Map<String, Object>> countries = new ArrayList<Map<String, Object>>();

 

Map<String, Object> country;

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201801" );

country.put( "population", 1000 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201802" );

country.put( "population", 1200 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201804" );

country.put( "population", 800 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201803" );

country.put( "population", 1000 );

countries.add( country );

 

country = new HashMap<String, Object>();

country.put( "collectYM",   "201803" );

country.put( "population", 1000 );

countries.add( country );

 

countries.stream().forEach(System.out::println);

 

System.out.println("total of list:" + countries.size());

 

System.out.println("distinct count:" + countries.stream().distinct().count());

 

 

 

List<Map<String, Object>> countries_distinct=new ArrayList<Map<String, Object>>();

countries_distinct.addAll(countries.stream().distinct().collect(Collectors.toList()));

 

countries_distinct.stream().forEach(System.out::println);

 

}

 

}

result:

 

f:id:annazola:20191016225657p:plain

 

awk like sql

1)sample data

tk-no-MacBook-Pro:~ tk$ cat test.csv

01,8888888,9999999999

09,7777777,6666666666

02,2777333,9999992222

03,8888888,6567637476

2)select col2,col3 from ... where col1 = 09

tk-no-MacBook-Pro:~ tk$ awk -F, '{if ($1 == 09) print $2","$3;}' test.csv

7777777,6666666666

tk-no-MacBook-Pro:~ tk$ 

 

3)if you need format 

tk-no-MacBook-Pro:~ tk$ cat test.csv

01,8888888,9999999999

09,7777777,6666666666

02,2777333,9999992222

03,8888888,6567637476

09,667,8987233598

4)

tk-no-MacBook-Pro:~ tk$ awk -F, '{if ($1 == 09) printf "%07d%s%07d\n",$2,",",$3}' test.csv

7777777,6666666666

0000667,8987233598

how to grouping and maxBy (list of maps)

1) list of maps : 

f:id:annazola:20190701011158p:plain

sample list of maps

2) grouping and maxBy

 

f:id:annazola:20190701011524p:plain

main codes

3) before and results

 

f:id:annazola:20190701012001p:plain

 

 appendix) to copy

 

List<Optional<Map<String, Object>>>  aggrList = 

mockList.stream().collect(Collectors.groupingBy(p -> (String)p.get("niveau_1")+ (String)p.get("niveau_2")+(String)p.get("franchise"),

Collectors.maxBy(Comparator.comparingLong(p->Long.valueOf(p.get("cnt").toString())))))

.values().stream().collect(Collectors.toList());

 

List<Map<String, Object>>  endList= new ArrayList<>();

aggrList.stream().forEach(s->{

endList.add(s.get());

});

System.out.println("result list");

endList.stream().forEach(System.out::println);