我正在开发一个项目,我需要以某种方式从数据库中打印出数据。让我们举一个例子,假设在我的数据库中,我只有以下条目 -
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.0
,但第二次运行后台线程时,只有在该框架的版本发生变化时才打印出来,否则不要打印出来打印出任何东西。
一段时间后的意思,如果有人像这样更改数据库中的版本信息 -
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();
}
}
任何帮助都将受到赞赏。
答案 0 :(得分:0)
我没有运行它。我只是想给你一个主意。尝试与打印相同的东西。如果你没有得到它,请告诉我。
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)
}
}