在java中为下面的数据选择完美的数据结构 [英] Choosing the perfect data structure for the below data in java

查看:93
本文介绍了在java中为下面的数据选择完美的数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为我的需要选择一个数据结构,我正在解释条件有以下值

I have to choose one data structure for my need below i am explaining the conditions there are following values

abc,def,rty,ytr,dft   which all are map to row R1B1 (actully key is combination of R1+B1)
abEERc,dFFFef,rGGty   which all are map to row R1B2 (actully key is combination of R1+B2)


  KEY                      VALUE
abc,def,rty,ytr,dft --->    R1B1
abEERc,dFFFef,rGGty --->    R1B2

code>然后我将能够检索 R1B1
或者让我们说,我得到的值 rGGty 然后我将能够检索 R1B2

now, for example, let's say, if i get ytr then i would be able to retrieve R1B1 or, let's say, i get the value rGGty then i would be able to retrieve R1B2

现在的情况是重要的是搜索,复杂性并且按照顺序执行的时间

now the case is that matters is of search, complexity and the time taken as the things have to go in sequence

,它将首先选择第一行搜索 ytr ,它将首先与 abc 匹配,否则将与 def 匹配不再匹配,那么它将与 rty 匹配,这不匹配,那么它将最终与 ytr 匹配,最后它将找到密钥 R1B1 终于

for example, it will first pick the first line to search ytr, it will first match it with abc which will not match then will have to match with def it will not again match then it will match with rty which will not also match then it will finally match with ytr and finally it will find the key R1B1 finally

类似地,如果第二个字符串需要被搜索,可以说 rGGty 然后它会扫描第一行,它将找不到该值,然后搜索将继续第二行和als o在第三个元素的第二行,它将获得 rGGty 作为元素,然后它将检索 R1B2 作为值

similarly if the second string need to be searched lets say rGGty then it would scan first row in which it will not find the value then search would continue to second row and also in second row in the third element it would get rGGty as element then it would retrieve R1B2 as value

如果把这个东西放在地图中,那么一个序列搜索将继续进行,然后只有我们能够找到相应的值

let's say, if put this thing in map then a sequence search will go on key and then only we will be able to find the corresponding value

人们请指教哪些是我可以在java中实现的最佳数据结构,其中我将不得不在非常快的时间内搜索密钥项来找到相应的值,这也不会影响性能
,数据结构的性能应该非常高

Folks please advise which will be the best data structure i can implement in java in which i will have to search the keys items to find the corresponding value in very fast time also which will not hit the performance too ,the kind of data structure performance should be very high

请指教人士

推荐答案

我认为最好保持简单,直到表现不佳,需要进行某种改进。即使许多地图值是相同的字符串,这应该是确定的,因为Java在堆中仅存储一个副本。如果映射到单个字符串的键集合变得非常大,则可以通过执行两次查找来实现一些性能改进 - 首先确定集合成员资格,然后再次检索与集合相关联的密钥的值。这将很容易实现。现在这里是一个立即简单的方法:

I think its best to keep it simple until poor performance shows a need for some kind of improvement. Even if many map values are the same string, that should be ok since Java stores only one copy in heap. If the sets of keys mapped to a single string gets very large some performance improvement may be possible by doing two lookups -- first to determine set membership and second to retrieve the value of the key associated with the set. That would be easy to implement. For now here is an immediately straightforward approach:

import java.util.*;

public class HashMapDemo {

    static HashMap<String, String> map = new HashMap<String, String>();

    public static void lookup(String key, String value) {
        if (map.get(key) == value) {
            System.out.println(key + " lookup ok");
        } else {
            System.out.println(key + " lookup produced" + map.get(key));
        }
    }

    public static void main(String[] args) {
        // requirements:
        // abc,def,rty,ytr,dft ---> R1B1
        // abEERc,dFFFef,rGGty ---> abEERc

        Set<String> kset1 = new HashSet<String>(Arrays.asList("abc", "def",
                "rty", "ytr", "dft"));

        Set<String> kset2 = new HashSet<String>(Arrays.asList("abEERc",
                "dFFFef", "rGGty"));

        for (String s : kset1) {
            map.put(s, "R1B1");
        }

        for (String s : kset2) {
            map.put(s, "abEERc");
        }

        // testing value lookup with key

        for (String s : kset1) {
            lookup(s, "R1B1");
        }

        // prints:
        // abc lookup ok
        // dft lookup ok
        // def lookup ok
        // rty lookup ok
        // ytr lookup ok

        for (String s : kset2) {
            lookup(s, "abEERc");
        }

        // prints:
        // rGGty lookup ok
        // abEERc lookup ok
        // dFFFef lookup ok

        // change key "R1B1" to "XYZ"

        for (String s : kset1) {
            map.put(s, "XYZ");
        }

        // test the change

        for (String s : kset1) {
            lookup(s, "XYZ");
        }

        // prints:
        // abc lookup ok
        // dft lookup ok
        // def lookup ok
        // rty lookup ok
        // ytr lookup ok

    }
}

这篇关于在java中为下面的数据选择完美的数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆