めも

技術メモとその他

java : filter list of map sample


package homeWork;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class filterSample {

public static void main(String[] args) {

List> countries = new ArrayList>();
Map country;
country = new HashMap();
country.put( "name", "japan" );
country.put( "currency", "JPY" );
country.put( "population", 127156000 );
countries.add( country );

country = new HashMap();
country.put( "name", "france" );
country.put( "currency", "EUR" );
country.put( "population", 65073482 );
countries.add( country );

country = new HashMap();
country.put( "name", "japan" );
country.put( "currency", "JPY" );
country.put( "population", 56000 );
countries.add( country );


country = new HashMap();
country.put( "name", "spain" );
country.put( "currency", "EUR" );
country.put( "population", 44904000 );
countries.add( country );

country = new HashMap();
country.put( "name", "russia" );
country.put( "currency", "RUB" );
country.put( "population", 141903979 );
countries.add( country );

for(Mapct:countries) {
System.out.println(ct.get("name")+":"+ct.get("population"));
}
System.out.println("-----------------------");

List> filterdList
= countries.stream().filter(m -> ( ( Integer ) m.get("population")) < 100000).collect(Collectors.toList());

for(Mapct:filterdList) {
System.out.println(ct.get("name")+":"+ct.get("population"));
}
}
}


japan:127156000
france:65073482
japan:56000
spain:44904000
russia:141903979

                                            • -

japan:56000