简体   繁体   English

值更改后,从地图上打印出条目

[英]Print out the entries from the map when the value gets changed

I am working on a project in which I need to print out the data from the database in a certain way. 我正在一个项目中,我需要以某种方式从数据库中打印出数据。 Let's take an example, suppose in my database, I have below entries only- 让我们举个例子,假设在我的数据库中,我只有以下条目-

Framework 1.0.0
BundleB 1.0.0
BundleC 1.0.0

Then my Java method that will make a call to the database which will return me a map of above data. 然后,我的Java方法将调用数据库,这将返回上述数据的映射。

My map will have above data as below- 我的地图将具有以下数据-

Key as Framework, Value as 1.0.0
Key as BundleB, Value as 1.0.0
Key as BundleC, Value as 1.0.0

Suppose, I started my program for the first time, then it will print out like this with the below code I have, which is perfectly fine. 假设我是第一次启动程序,那么它将使用下面的代码像这样打印出来,这非常好。

Framework - 1.0.0

And then I am running background thread every 2 seconds that will make a call to the database and get all the data again from the database. 然后,我每2秒运行一次后台线程,该线程将调用数据库并再次从数据库中获取所有数据。 And every two seconds, it will print out the same thing as below- (which is not what I want) 每两秒钟,它将打印出与下面相同的内容(这不是我想要的)

Framework - 1.0.0

I want to print out Framework - 1.0.0 for the first time when I am running my program but second time when the background thread is running, it should print out only when the version gets changed for that Framework, otherwise don't print out anything. 我想在我运行程序时第一次打印出Framework - 1.0.0 ,但是第二次在后台线程运行时打印出,仅当该Framework的版本更改时,它才应该打印出,否则不要打印出任何东西。

Meaning after some time, if somebody changes the version information in the database like this- 意思是一段时间后,如果有人像这样更改数据库中的版本信息,

Framework 1.0.1
BundleB 1.0.0
BundleC 1.0.0

then only it should print out like this- 那么只有它应该像这样打印出来-

Framework - 1.0.1

I hope the questions is clear enough. 我希望问题足够清楚。 Below is my code that I have so far. 下面是我到目前为止的代码。

public class Test {

    public static Map<String, String> bundleList = new LinkedHashMap<String, String>();

    private static Map<String, String> oldBundleList = new LinkedHashMap<String, String>();


    public static void main(String[] args) {

        getAttributesFromDatabase();

        loggingAfterEveryXMilliseconds();

    }


    private static void getAttributesFromDatabase() {

        Map<String, String> bundleInformation = new LinkedHashMap<String, String>();

        bundleInformation = getFromDatabase();
        if(!bundleInformation.isEmpty()) {
            oldBundleList = bundleList;
            bundleList = bundleInformation;
        }

        final Map<String, MapDifference.ValueDifference<String>> entriesDiffering = Maps.difference(oldBundleList, bundleList).entriesDiffering();
        if (!entriesDiffering.isEmpty()) {
            for (String key : entriesDiffering.keySet()) {
                bundleList.put(key, bundleList.get(key));
            } 
        }

        String version = bundleList.get("Framework");
        printOutZeroInformation("Framework", version);
    }

    private static void printOutZeroInformation(String string, String version) {
        System.out.println(string+" - "+version);       
    }

    private static Map<String, String> getFromDatabase() {

        Map<String, String> hello = new LinkedHashMap<String, String>();

        String version0 = "1.0.0";
        String version1 = "1.0.0";
        String version2 = "1.0.0";

        hello.put("Framework", version0);
        hello.put("BundleA", version1);
        hello.put("BundleB", version2);

        return hello;
    }

    private static void loggingAfterEveryXMilliseconds() {
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {

                    }
                    getAttributesFromDatabase();
                }
            }
        }.start();
    }
}

Any help will be appreciated on this. 任何帮助将不胜感激。

I didn't run it. 我没有运行。 I'm just giving you an idea. 我只是给你一个主意。 Try something same with the print as well. 尝试与打印件相同。 Let me know if you don't get it. 如果您不明白,请告诉我。

private static void getFromDatabase() {

        Map<String, String> hello = new LinkedHashMap<String, String>();            

        String version0 = "1.0.0";
        String version1 = "1.0.0";
        String version2 = "1.0.0";

        hello.put("Framework", version0);
        hello.put("BundleA", version1);
        hello.put("BundleB", version2);

        //The following code will update bundleList only when it's different from hello
        if (!bundleList.isEmpty()) {
            if (bundleList.get("Framework") != hello.get("Framework"))
                bundleList.put("Framework", version0)
            if (bundleList.get("BundleA") != hello.get("BundleA"))
                bundleList.put("BundleA", version1)
            if (bundleList.get("BundleB") != hello.get("BundleB"))
                bundleList.put("BundleB", version2)
        }
        else { //if this is the first time
            bundleList.put("Framework", version0)
            bundleList.put("BundleA", version1)
            bundleList.put("BundleB", version2)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM