Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当值发生更改时,打印地图中的条目_Java_Database_Map - Fatal编程技术网

Java 当值发生更改时,打印地图中的条目

Java 当值发生更改时,打印地图中的条目,java,database,map,Java,Database,Map,我正在做一个项目,在这个项目中,我需要以某种方式打印出数据库中的数据。让我们举一个例子,假设在我的数据库中,我只有以下条目- Framework 1.0.0 BundleB 1.0.0 BundleC 1.0.0 然后我的Java方法将调用数据库,数据库将返回上述数据的映射 我的地图将有以下数据- Key as Framework, Value as 1.0.0 Key as BundleB, Value as 1.0.0 Key as BundleC, Value as 1.0.0 假设,

我正在做一个项目,在这个项目中,我需要以某种方式打印出数据库中的数据。让我们举一个例子,假设在我的数据库中,我只有以下条目-

Framework 1.0.0
BundleB 1.0.0
BundleC 1.0.0
然后我的Java方法将调用数据库,数据库将返回上述数据的映射

我的地图将有以下数据-

Key as Framework, Value as 1.0.0
Key as BundleB, Value as 1.0.0
Key as BundleC, Value as 1.0.0
假设,我第一次启动我的程序,然后它会像这样打印出来,下面的代码我有,这是非常好的

Framework - 1.0.0
然后我每2秒运行一次后台线程,它将调用数据库并再次从数据库中获取所有数据。每两秒钟,它就会打印出与下面相同的内容-(这不是我想要的)

我想在第一次运行程序时打印出
Framework-1.0.0
,但第二次后台线程运行时,它应该只在该框架的版本更改时打印,否则不要打印任何内容

也就是说,一段时间后,如果有人像这样更改数据库中的版本信息-

Framework 1.0.1
BundleB 1.0.0
BundleC 1.0.0
Framework - 1.0.1
只有这样才能打印出来-

Framework 1.0.1
BundleB 1.0.0
BundleC 1.0.0
Framework - 1.0.1
我希望问题足够清楚。下面是我到目前为止的代码

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();
    }
}
公共类测试{
public static Map bundleList=新建LinkedHashMap();
私有静态映射oldBundleList=新LinkedHashMap();
公共静态void main(字符串[]args){
getAttributesFromDatabase();
LoggingAftereveryx毫秒();
}
私有静态void getAttributesFromDatabase(){
Map bundleInformation=新建LinkedHashMap();
bundleInformation=getFromDatabase();
如果(!bundleInformation.isEmpty()){
oldBundleList=捆绑列表;
bundleList=bundleInformation;
}
final Map EntriesDifferenting=Maps.differenting(旧bundleList,bundleList).EntriesDifferenting();
如果(!EntriesDifferenting.isEmpty()){
for(字符串键:EntriesDifferenting.keySet()){
bundleList.put(key,bundleList.get(key));
} 
}
String version=bundleList.get(“框架”);
打印输出零信息(“框架”,版本);
}
私有静态void打印输出零信息(字符串、字符串版本){
System.out.println(字符串+“-”+版本);
}
私有静态映射getFromDatabase(){
Map hello=新建LinkedHashMap();
字符串version0=“1.0.0”;
字符串version1=“1.0.0”;
字符串version2=“1.0.0”;
hello.put(“框架”,版本0);
hello.put(“BundleA”,version1);
hello.put(“BundleB”,version2);
回复你好;
}
私有静态无效日志Aftereveryx毫秒(){
新线程(){
公开募捐{
while(true){
试一试{
《睡眠》(2000年);
}捕获(中断异常例外){
}
getAttributesFromDatabase();
}
}
}.start();
}
}

对此,我们将不胜感激。

我没有运行它。我只是给你一个想法。试着用同样的印刷品。如果你不明白,请告诉我

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)
        }
    }
private static void getFromDatabase(){
Map hello=新建LinkedHashMap();
字符串version0=“1.0.0”;
字符串version1=“1.0.0”;
字符串version2=“1.0.0”;
hello.put(“框架”,版本0);
hello.put(“BundleA”,version1);
hello.put(“BundleB”,version2);
//以下代码将仅在bundleList与hello不同时更新它
如果(!bundleList.isEmpty()){
if(bundleList.get(“Framework”)!=hello.get(“Framework”))
bundleList.put(“框架”,版本0)
if(bundleList.get(“BundleA”)!=hello.get(“BundleA”))
bundleList.put(“BundleA”,版本1)
if(bundleList.get(“BundleB”)!=hello.get(“BundleB”))
bundleList.put(“BundleB”,版本2)
}
否则{//如果这是第一次
bundleList.put(“框架”,版本0)
bundleList.put(“BundleA”,版本1)
bundleList.put(“BundleB”,版本2)
}
}

你想让线程每次都从数据库中获取所有数据,而不管是否有更新?这就是我的想法。有没有办法在每次更新时都能得到什么?这意味着它第一次应该从数据库中获取所有信息,但之后应该只获取更新的信息?这可能吗?我将使用Cassandra作为Astyanax客户机的数据库。为什么不第一次填充映射并保留它呢。现在,只有当数据库中的值与map中的值不同时,线程才应该更新数据库中的值?然后在旧地图中,你可以拥有上一张地图,只打印更新后的地图。我明白了。。你能在我上面的代码的基础上提供一个简单的例子吗?我越来越糊涂了。。