gpt4 book ai didi

java - 多参数搜索,java集合选择建议

转载 作者:行者123 更新时间:2023-12-02 07:38:31 24 4
gpt4 key购买 nike

在下面描述的情况下我应该使用什么数据结构:

我有一个简单的bean:

 public class Points { 

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

//getters+setters

}

我想创建几个 bean 并将它们存储在某种数据结构中。并且能够使用两个参数进行搜索 - 名称和地址。

例如,用户输入“7” - 它会返回几个对象,哪个名称或地址包含该字符?

我应该使用什么数据结构以及如何搜索它?

如果它很重要,我实际上需要将其实现到我的 Android 应用程序中 -我想在 map 上搜索我的点另外,到目前为止我还不想创建数据库,因为只有 20 个数据库。

提前非常感谢您。

最佳答案

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

关于java - 多参数搜索,java集合选择建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11881630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com