めも

技術メモとその他

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);

 

 

spring batch : junit test sample

1.project files are below(use terasolua spring batch sample)

f:id:annazola:20190513073109p:plain

2.test codes(test class)

package batch;

 

 

import static org.junit.Assert.fail;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.junit.runners.JUnit4;

import org.springframework.batch.core.JobParameters;

import org.springframework.batch.test.JobLauncherTestUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:test-context.xml" ,

"classpath:job01.xml"})

 

public class testJob01 {

 

@Autowired

    private JobLauncherTestUtils jobLauncherTestUtils;

 

@Test

    public void testJob() throws Exception {

jobLauncherTestUtils.launchJob();

    }

 

}

 *if you code "JUnit4.class" after @RunWith annotation, @Autowired

failed(NPE)

 

3.test codes(setting file: test-context.xml)

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:p="http://www.springframework.org/schema/p"

       xmlns:batch="http://www.springframework.org/schema/batch"

       xmlns:util="http://www.springframework.org/schema/util"

       xmlns:jdbc="http://www.springframework.org/schema/jdbc"

       xmlns:c="http://www.springframework.org/schema/c"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

            http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd

            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

 

    <bean id="jobLauncherTestUtils" class="org.springframework.batch.test.JobLauncherTestUtils">

        <property name="job" ref="job01" />

        <property name="jobRepository" ref="jobRepository" />

        <property name="jobLauncher" ref="jobLauncher" />

    </bean>

    </beans>