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