如何检索存储在树图集合中的值?

时间:2016-01-14 14:35:37

标签: java search dictionary

如何将值与从数据库中检索到的值进行比较,得出只有1个结果?

我是编程新手,请善待。

下面是我的完整功能: -

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;
}

我是编程的初学者,请不要让我失望!! 我的问题是,我无法获得存储在&#34; value&#34;中的数据。而不仅仅是打印它。我想将获得的每个数据存储到变量中以进行进一步的处理

我想用这个程序做的是: -

  1. 在北部,东部,西部和南部找到最高优先级的值。 (确定最高优先级,如果在方法下面返回多个值,则应用)
  2. 在数据库中找到相同方的现有优先级,其中最后一个更新方的优先级很高。
  3. 如果获得的优先级与上次更新的一方不匹配,则会考虑最后更新的两方。
  4. 我的目标简而言之: - 确定优先级最高的一方&amp;当返回多个值时,比较现有的优先级,并得出必须为哪一方提供优先级的结论,即更新&#39; tbl_priroty&#39;使用&#39; active = 1&#39;各方。

    提前致谢。

1 个答案:

答案 0 :(得分:1)

这就是我要做的事。

NavigableMap<Double, List<String>> priorityMap = new TreeMap<Double, List<String>>();
// create a map that contains priorities and names

然后填写地图。为此,我建议使用不同的数据结构进行阅读,例如,

double[] priorities = new double[] {2, 3, 4, 4};
String[] names = new String[] {"west", "north", "south", "east"};

因为如果您使用priorityMap.put(4, "String1");后跟priorityMap.put(4, "String2");,则会覆盖第一个放置。

所以:

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");     
}

您无法将StringCollectionList进行比较。因为它永远不会是true。您不必检查是否highestPriorityNames.contains(name),因为您遍历此列表中的所有名称。