Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:32:22+00:00 2026-05-27T00:32:22+00:00

In our applications we are using property files very much. Since a few months

  • 0

In our applications we are using property files very much. Since a few months I have started to learn Guava and I liked it a lot actually.

What is the best way to create a Map<String, Datasource> ?

The property file format is not strict. It can be changed if It can be expressed better with another format?

Sample property file:

datasource1.url=jdbc:mysql://192.168.11.46/db1
datasource1.password=password
datasource1.user=root
datasource2.url=jdbc:mysql://192.168.11.45/db2
datasource2.password=password
datasource2.user=root
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:32:23+00:00Added an answer on May 27, 2026 at 12:32 am

    Properties class is a subclass of HashTable, which in turn implements Map.

    You load it as usual with:

    Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("filename.properties"));
        } catch (IOException e) { 
    }
    

    edit: Ok so you want to transform it to Map<String, Datasource> 😉

    //First convert properties to Map<String, String>
    Map<String, String> m = Maps.fromProperties(properties);
    
    //Sort them so that password < url < user for each datasource and dataSource1.* < dataSource2.*. In your case default string ordering is ok so we can take a normal treemap
    Map<String, String> sorted = Maps.newTreeMap();
    sorted.putAll(m);
    
    //Create Multimap<String, List<String>> mapping datasourcename->[password,url, user ]
    
        Function<Map.Entry<String, String>, String> propToList = new Function<String, Integer>() {
            @Override
            public String apply(Map.Entry<String, String> entry) {
                return entry.getKey().split("\\.")[0];
            }
        };
    
    Multimap<Integer, String> nameToParamMap = Multimaps.index(m.entrySet(), propToList);
    
    //Convert it to map
    Map<String, Collection<String>> mm = nameToParamMap.asMap();
    
    //Transform it to Map<String, Datasource>
    Map<String, Datasource> mSD = Maps.transformEntries(mm, new EntryTransformer<String, Collection<String>, DataSource>() {
             public DataSource transformEntry(String key, Collection<String> value) {
                // Create your datasource. You know by now that Collection<String> is actually a list so you can assume elements are in order: [password, url, user]
                return new Datasource(.....)
             }
           };
    
    //Copy transformed map so it's no longer a view
    Map<String, Datasource> finalMap = Maps.newHashMap(mSD);
    

    There’s probably an easier way, but this should work 🙂

    Still you’re better off with json or xml. You can also load properties of different datasources from different files.

    edit2: with less guava, more java:

    //Sort them so that password < url < user for each datasource and dataSource1.* < dataSource2.*. In your case default string ordering is ok so we can take a normal SortedSet
    SortedSet <String> sorted = new SortedSet<String>();
    sorted.putAll(m.keySet);
    
    //Divide keys into lists of 3
    Iterable<List<String>> keyLists = Iterables.partition(sorted.keySet(), 3);
    
    
    Map<String, Datasource> m = new HashMap<String, Datasource>();
    for (keyList : keyLists) {
        //Contains datasourcex.password, datasroucex.url, datasourcex.user
        String[] params = keyList.toArray(new String[keyList.size()]);
        String password = properties.get(params[0]);
        String url = properties.get(params[1]);
        String user = properties.get(params[2]);
        m.put(params[0].split("\\.")[0], new DataSource(....)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some performance problems with one of our applications. I thought about using
We are currently using Fitnesse for sub system testing. [All our applications are in
We are looking at switching from using WCF for our service layer in applications
So we have our J2EE application using Log4j like this public class CustomerController {
In our application, we are using RMI for client-server communication in very different ways:
We've been using Unity for our ASP.NET MVC3 applications. The configuration is done solely
We currently have developed an application using WCF. Our clients make connections to different
The problem we have are as follows: We are using ANT to build our
In our various applications, we have a hodge-podge of different methods used to read
We have lot of configuration files used in our application. Atleast 100 different xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.