簡體   English   中英

用多個參數搜索,java集合選擇建議

[英]search with multiple parameters, java collection choice advise

在下面描述的情況下,我應該使用什么數據結構:

我有一個簡單的bean:

 public class Points { 

    private String name; 
    private String address; 
    private int phone; 
    private int coord1; 
    private int coord2; 

    //getters+setters 

    }

我想創建幾個bean並將它們存儲在某種數據結構中。 並且能夠使用兩個參數進行搜索 - 名稱和地址。

例如,用戶輸入“7” - 並且它給了他幾個對象,哪個名稱或地址包含這個字符?

我應該使用什么數據結構以及如何搜索它?

如果它很重要,我實際上需要這個實現到我的Android應用程序 - 我想在地圖上搜索我的點我也不想創建一個數據庫到目前為止,因為只有20個。

非常感謝你提前。

特里似乎很合適。 查找具有特定前綴的所有字符串是一種高效的數據結構。

如果你想使用現有的java集合之一,你可以使用TreeSet ,並使用它的floor()方法在需要的前綴之前獲取元素 - 然后在它仍然匹配時開始迭代集合。

如果您正在尋找子字符串搜索 ,而不僅僅是前綴 - 您可能希望使用后綴樹
使用java現有容器的(低效)替代方法是將鍵的所有子串存儲在SetMap ,但它需要兩倍的空間。

試試java的集合,例如hashmap。 雖然我在PC上運行了10000個項目,搜索返回了3440個結果,但它耗時76ms。

    class Points {

        String name;
        String address;
        int phone;
        int coord1;
        int coord2;

        // getters+setters
    };

    class PointsIdentifier {

        private String name;
        private String address;

        public PointsIdentifier(String name, String address) {
            this.name = name;
            this.address = address;

        }

        public boolean contains(String seq) {
            return name.contains(seq) || address.contains(seq);
        }

        @Override
        public boolean equals(Object obj) {
            Points other = (Points) obj;
            return name.equals(other.name) && address.equals(other.address);
        }

        @Override
        public int hashCode() {
            return name.hashCode() + address.hashCode();
        }
    };

    class PointsCollection {
        private Map<PointsIdentifier, Points> map;

        public PointsCollection() {
            map = new HashMap<PointsIdentifier, Points>();
        }

        public void add(Points p) {
            map.put(new PointsIdentifier(p.name, p.address), p);
        }

        public List<Points> findIdsContaining(String seq) {
            List<Points> resultList = new ArrayList<Points>();
            for (Entry<PointsIdentifier, Points> entry : map.entrySet()) {
                if (entry.getKey().contains(seq)) {
                    resultList.add(entry.getValue());
                }
            }
            // optionally cache result
            return resultList;
        }
    }

    public class Question_11881630 {

        public static void main(String[] args) {
            PointsCollection places = createCollection(10000);
            System.out.println("Collection created");
        String seq = "1";

        System.out.format("Searching for: \"%s\"\n", seq);
        List<Points> verifySearch = verifySearch(places, seq);
        //show(verifySearch);
    }

    private static void show(List<Points> verifySearch) {
        int i = 1;
        for (Points p : verifySearch) {
            System.out.println(i + ": " + p.name + ", " + p.address);
            i++;
        }
    }

    private static List<Points> verifySearch(PointsCollection places, String seq) {
        long start = System.currentTimeMillis();
        List<Points> searchResult = places.findIdsContaining(seq);
        System.out.println("Search results: " + searchResult.size());
        long end = System.currentTimeMillis();
        System.out.println("Operation time: " + formatTime(end - start));
        return searchResult;
    }

    private static String formatTime(long elapsed) {
        return elapsed + " miliseconds";
    }

    private static PointsCollection createCollection(int number) {
        PointsCollection coll = new PointsCollection();
        while (number > 0) {
            coll.add(createSamplePoint(number));
            number--;
        }
        return coll;
    }

    private static Points createSamplePoint(int number) {
        Points p = new Points();
        p.name = "VeryVeryLongName: " + number;
        p.address = "VeryVeryLongLongAddress: " + number;
            p.coord1 = 123;
            p.coord2 = 456;
            return p;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM