有效地找到对象数组中的元素

时间:2014-03-09 18:42:01

标签: java arrays search

我有这么长的一系列对象:

public static class __Location {
    public __Location(int locationId, String locationDesc) {
        this.LocationId = locationId;
        this.LocationDesc = locationDesc;
    }

    public int LocationId;
    public String LocationDesc;
}


public static __Location[] LOCATIONS = { new __Location(100, "Afghanistan"), new __Location(110, "Albania"), new __Location(120, "Algeria"),
        new __Location(130, "American Samoa"), new __Location(140, "Andorra"), new __Location(150, "Angola"), new __Location(160, "Anguilla"),
        new __Location(170, "Antarctica"), new __Location(180, "Antigua And Barbuda"), new __Location(190, "Argentina"), new __Location(200, "Armenia"),
        new __Location(210, "Aruba"), new __Location(220, "Australia"), new __Location(230, "Austria"), new __Location(240, "Azerbaijan"),
        new __Location(250, "Bahamas"), new __Location(260, "Bahrain"), new __Location(270, "Bangladesh"), new __Location(280, "Barbados"),
        new __Location(290, "Belarus"), new __Location(300, "Belgium"), new __Location(310, "Belize"), new __Location(320, "Benin"),
        new __Location(330, "Bermuda"), new __Location(340, "Bhutan"), new __Location(350, "Bolivia"), new __Location(360, "Bosnia And Herzegovina"),
        new __Location(370, "Botswana"), new __Location(380, "Bouvet Island"), new __Location(390, "Brazil"),
        new __Location(400, "British Indian Ocean Territory"), ...

我的问题是如何有效地搜索这个长数组中的项目(根据其键值,即LocationId)。

4 个答案:

答案 0 :(得分:2)

使用HashMap,您可以有效地访问该项目,时间复杂度为O(1)

Map<Integer, __Location> map = new HashMap<Integer, __Location>();

key的{​​{1}}为HashMapLocationId为对应的value

答案 1 :(得分:0)

如果您使用密钥,为什么不尝试使用地图?

http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

这样,你可以通过它的键来查找一个值,从上下文我假设你想做什么?

答案 2 :(得分:0)

考虑使用HashSet

Set<_location> locationSet = new HashSet<_location>();
locationSet.add(new __Location(130, "American Samoa"));
...
_location searchLocation = //some _location instance;
if (locationSet.contains(searchLocation)) {
  //found it
}

您需要覆盖_location类的hashcode和equals方法,以便相等由locationID确定。

这比需要遍历的数组搜索更有效。你有O(1)搜索HashSet vs O(n)数组

答案 3 :(得分:0)

首先:将__Location重命名为Location

第二:你谈到“有效搜索”,很好。现在回答这两个问题:

  • 您想要搜索的这个位置来自哪里?这是new Location()的结果吗?这个位置的ID?这个位置的字符串?
  • 在要查找的容器中,现有位置是否已排序?如果是,反对什么?身份证?字符串?

如果对这个问题没有明确答案,就无法回答你的问题。

我们假设,因为这是最简单的情况,LocationlocationId标识;您知道,对于给定的locationIdlocationString将是唯一的。

为了获得最高效率,您应该在.equals()中实施.hashCode() / Location合同并使用HashSet

public static class Location {
    private final int locationId;
    private final String locationDesc;

    public Location(final int locationId, final String locationDesc)
    {
        this.locationId = locationId;
        this.locationDesc = locationDesc;
    }

    @Override
    public int hashCode()
    {
        return locationId;
    }

    @Override
    public boolean equals(final Object obj)
    {
        if (obj == null)
            return false;
        if (this == obj)
            return true;
        if (getClass() != obj.getClass())
            return false;
        final Location other = (Location) obj;
        return locationId == other.locationId;
    }
}

然后使用HashSet<Location>插入Location并使用此Set的{​​{1}}方法。