Java 如何检索存储在树地图集合中的值?
如何将这些值和从数据库检索到的值进行比较,从而得出一个结果 我是个编程新手,请善待我 以下是我的完整功能:-Java 如何检索存储在树地图集合中的值?,java,search,dictionary,Java,Search,Dictionary,如何将这些值和从数据库检索到的值进行比较,从而得出一个结果 我是个编程新手,请善待我 以下是我的完整功能:- public String startpgm(String x){ String p = null; double West = 2; double North = 3; double South = 4; double East = 4; String w =null; Stri
public String startpgm(String x){
String p = null;
double West = 2;
double North = 3;
double South = 4;
double East = 4;
String w =null;
String n =null;
String s =null;
String e =null;
TreeMap<Double, Collection<String>> names = new TreeMap<Double, Collection<String>>();
put(names, West, "west");
put(names, North, "north");
put(names, South, "south");
put(names, East, "east");
Collection<String> maximumPriorityValues = names.lastEntry().getValue();
for(String value : maximumPriorityValues){
System.out.println("Priority:"+value);
p=value;
while(value.equals(maximumPriorityValues)){
w= value.substring("west");
n= value.substring("north");
s= value.substring("south");
e= value.substring("east");
}
}
System.out.println("Current Priority:"+w+","+n+","+s+","+e);
String qrypriority = "select priority from tbl_priority order by updated_at ASC LIMIT 1";
Statement stmt = (Statement) conn.createStatement();
ResultSet PastPriority=stmt.executeQuery(qrypriority);
if (PastPriority.equals(w)){
String qryupdate = "update tbl_priority set active=1 where priority='west'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(n)){
String qryupdate = "update tbl_priority set active=1 where priority='north'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(s)){
String qryupdate = "update tbl_priority set active=1 where priority='south'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(e)){
String qryupdate = "update tbl_priority set active=1 where priority='east'";
int b=stmt.executeUpdate(qryupdate);
}
String qrystatus = "select priority from tbl_priority order by updated_at ASC LIMIT 1";
ResultSet status=stmt.executeQuery(qrystatus);
if (!PastPriority.next()){
String qrypriority2 = "select priority from tbl_priority order by updated_at ASC LIMIT 2";
ResultSet PastPriority2=stmt.executeQuery(qrypriority);
if (PastPriority.equals(w)){
String qryupdate = "update tbl_priority set active=1 where priority='west'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(n)){
String qryupdate = "update tbl_priority set active=1 where priority='north'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(s)){
String qryupdate = "update tbl_priority set active=1 where priority='south'";
int b=stmt.executeUpdate(qryupdate);
}
if (PastPriority.equals(e)){
String qryupdate = "update tbl_priority set active=1 where priority='east'";
int b=stmt.executeUpdate(qryupdate);
}
}
return p;
}
公共字符串startpgm(字符串x){
字符串p=null;
双西=2;
双北=3;
双南=4;
双东=4;
字符串w=null;
字符串n=null;
字符串s=null;
字符串e=null;
TreeMap name=newtreemap();
放置(名称,西部,“西部”);
放置(名称,北,“北”);
放置(名称,南部,“南部”);
放置(名称,东,“东”);
集合maximumPriorityValues=names.lastEntry().getValue();
for(字符串值:maximumPriorityValues){
System.out.println(“优先级:”+值);
p=数值;
while(value.equals(maximumPriorityValues)){
w=值。子字符串(“西”);
n=值。子字符串(“北”);
s=值。子字符串(“南”);
e=值。子字符串(“东”);
}
}
System.out.println(“当前优先级:+w+”、“+n+”、“+s+”、“+e”);
String qrypriority=“根据ASC限制1更新的tbl\U优先级顺序选择优先级”;
语句stmt=(语句)conn.createStatement();
ResultSet PastPriority=stmt.executeQuery(qrypriority);
如果(优先级等于(w)){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级为=”西';
int b=stmt.executeUpdate(qryupdate);
}
if(passpriority.equals(n)){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级为“北”;
int b=stmt.executeUpdate(qryupdate);
}
如果(优先权等于){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级=”南';
int b=stmt.executeUpdate(qryupdate);
}
如果(优先权等于(e)){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级为=”东';
int b=stmt.executeUpdate(qryupdate);
}
String qrystatus=“通过ASC限制1处的更新从tbl\U优先级顺序中选择优先级”;
结果集状态=stmt.executeQuery(qrystatus);
如果(!PastPriority.next()){
String qrypriority2=“通过ASC限制2的更新从tbl\U优先级顺序中选择优先级”;
结果Set PastPriority2=执行时间(qrypriority);
如果(优先级等于(w)){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级为=”西';
int b=stmt.executeUpdate(qryupdate);
}
if(passpriority.equals(n)){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级为“北”;
int b=stmt.executeUpdate(qryupdate);
}
如果(优先权等于){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级=”南';
int b=stmt.executeUpdate(qryupdate);
}
如果(优先权等于(e)){
字符串qryupdate=“更新tbl\U优先级设置活动=1,其中优先级为=”东';
int b=stmt.executeUpdate(qryupdate);
}
}
返回p;
}
我是编程新手,请不要让我失望!!
我的问题是,我无法获取存储在“value”中的数据,而只是将其打印出来。我想将在中获得的每个数据存储到变量中,以便进一步处理
我想用这个程序做的是:-
提前谢谢。以下是我要做的
NavigableMap<Double, List<String>> priorityMap = new TreeMap<Double, List<String>>();
// create a map that contains priorities and names
因为如果使用priorityMap.put(4,“String1”)代码>后接优先映射put(4,“String2”)代码>覆盖第一个put
因此:
不能将字符串
与集合
或列表
进行比较。因为它永远不会是真实的。您不必检查highestPriorityNames.contains(name)
,因为您可以循环遍历此列表中的所有名称。至少可以帮助我将数据从“value”检索到字符串中……您为什么不使用TreeMap names=new TreeMap()代码>?为什么需要一个集合
?@LordAnomander编辑了我的程序,当有相同的最高优先级时,我需要获得多个值。您可以在获得多个优先级的集合下看到for循环。但是,该代码是由其他一些程序建议的。在“NavigableMap”中声明的“double”显示错误。我必须在那里输入优先级值吗?如果是,我必须如何填写列表?&在(String name:'highestPriorityNames'){//的最后一部分中,我需要在sting中获得最高优先级的名称,我必须在另一个数据库查询中使用它,如果我可以计算字符串的数量,这也很有帮助}-希望你得到meuser5778177是我的朋友计数String“name”的数量,我的意思是计算元素的数量(北、南、东、西)出现在从1到4的字符串中。我只需要继续执行条件2和条件3。@jasim My bad您必须在地图中使用Double
,而不是Double
。是的,键值表示您希望输入的优先级。您填写列表,如我所示:priorityMap.get(whateverpriority)
将返回该优先级的字符串的列表
,使用添加(whatevername)
可以
double[] priorities = new double[] {2, 3, 4, 4};
String[] names = new String[] {"west", "north", "south", "east"};
for (int i = 0; i < priorities.length; i++) {
// if the priority, i.e., the String is no key so far
if (!priorityMap.contains(priorities[i])
// we create a new List that saves the names containing
// those which belong to the priority.
priorityMap.put(priorities[i], new ArrayList<String>());
// afterwards we add the name to the priority list
priorityMap.get(priorities[i]).add(names[i]);
}
List<String> highestPriorityNames = priorityMap.lastEntry();
for (String name : highestPriorityNames) {
// do whatever you want with the high priority name
}
while(value.equals(maximumPriorityValues)){
w= value.substring("west");
n= value.substring("north");
s= value.substring("south");
e= value.substring("east");
}