Samarland
Samarland

Reputation: 581

build a java list from properties file

I have an a list of stores(getStore(..))s, which is getting all the stores from DB

what I'm trying to do is to add additional stores from properties file.(trying to build the list)

here's how my properties file look like

yard=1085:BELF:BELGRADE FIXTURES
yard=3238:SKWS:SKOKIE WAREHOUSE
yard=3239:PLDC:PLANO DC
yard=3339:HCDC:HOLIDAY CITY DC

here's how I'm trying to get the list.

@Value("#{'${yard}'.split(',')}#{T(java.util.Collections).emptyList()}") 
private List<String> myList;

here how I'm trying to add the additional stores to the list

    public List<Store> additionalYardList() {
    List<Store> stores = storesApi.getStores();
    for (String yard : myList) {
        String[] yardArray = yard.split(":");
        Store store = new Store();
        store.setNumber(Integer.parseInt(yardArray[0]));
        store.setAbbr(yardArray[1]);
        store.setName(yardArray[2]);
        LOG.debug(store.getAbbr());
        stores.add(store);
    }
    return stores;
}

Then will do a Stream to merge

    public List<Store> getAllStores() {
    return Stream.of(additionalYardList(), storesApi.getStores()).flatMap(List::stream).collect(Collectors.toList());
}

}

My Store object fields are: (number- abbr - name )

Nothing is been added to that list, Any idea what am I missing?

Upvotes: 0

Views: 2554

Answers (1)

Vikram Palakurthi
Vikram Palakurthi

Reputation: 2496

Duplicate yard keys, please correct properties file to have unique keys.

Change it from

yard=1085:BELF:BELGRADE FIXTURES
yard=3238:SKWS:SKOKIE WAREHOUSE
yard=3239:PLDC:PLANO DC
yard=3339:HCDC:HOLIDAY CITY DC

To

yard=1085:BELF:BELGRADE FIXTURES,3238:SKWS:SKOKIE WAREHOUSE,3239:PLDC:PLANO DC,3339:HCDC:HOLIDAY CITY DC

And then you could load them with below code.

@Value("#{'${yard}'.split(',')}") 
 private List<String> myList;

This has been already answered here.

Upvotes: 1

Related Questions