为java矩阵编制索引

为java矩阵编制索引,java,indexing,matrix,Java,Indexing,Matrix,我想知道是否有可能对矩阵的多个列进行索引以加快排序。在MySQL中,您可以在多个列上建立索引,这使得在表中查找元素的速度更快,但我不知道在标准java矩阵中是否可能做到这一点。例如,我的数据是一个3列矩阵,它有一个id、名字和姓氏,然后在这个表上有许多条目。现在,我可以说mat[5]之类的话,为id为5的个人获取条目,但我也希望能够按姓氏列搜索条目。在Java中如何才能最有效地做到这一点?如果是Java,您可以设置一个哈希表,将姓氏与矩阵行索引数组相关联,以便这些行上的人都有姓氏 或者您可以有一

我想知道是否有可能对矩阵的多个列进行索引以加快排序。在MySQL中,您可以在多个列上建立索引,这使得在表中查找元素的速度更快,但我不知道在标准java矩阵中是否可能做到这一点。例如,我的数据是一个3列矩阵,它有一个id、名字和姓氏,然后在这个表上有许多条目。现在,我可以说mat[5]之类的话,为id为5的个人获取条目,但我也希望能够按姓氏列搜索条目。在Java中如何才能最有效地做到这一点?

如果是Java,您可以设置一个哈希表,将姓氏与矩阵行索引数组相关联,以便这些行上的人都有姓氏

或者您可以有一个多级哈希表,这样您就可以执行
m[index.get(lastName).get(firstName)]

此外,如果您想按字典顺序迭代名称,可以用
树映射替换哈希表

例如:

import java.util.*;
class Test{
    public static void main(String[]args){

        Object[][] m = new Object[][]{
            {1, "Smith", "John"},
            {2, "Stone", "Jack"},
            {3, "Stein", "Robert"},
            {4, "Stone", "Bob"}
        };

        //index.get(lastName) will return a map between
        //first names and matrix row indices. 
        //index.get(lastName).get(firstName) returns the index
        //in the matrix of the row pertaining to person (lastName, firstName)
        TreeMap<String, TreeMap<String, Integer>> index = 
            new TreeMap<String, TreeMap<String, Integer>>();

        //create index
        for(int i=0;i<m.length;i++){
            Object[]o = m[i];
            String last = o[1].toString();
            String first = o[2].toString();
            TreeMap<String,Integer> index2 = index.get(last);
            if (index2==null){
                index2=new TreeMap<String,Integer>();
                index.put(last, index2);
            }
            index2.put(first, i);
        }

        System.out.print("Smith, John -> ");
        System.out.println(Arrays.toString(m[index.get("Smith").get("John")]));

        System.out.print("Stone -> ");
        System.out.println(index.get("Stone"));

        System.out.print("Full index: ");
        System.out.println(index); 
    }
}
在我给出的示例中,仅将姓氏映射到行索引是不够的,因为您可能有两个姓氏相同的人。然而,我确实假设你不会有两个名字完全相同的人。否则,您将需要类似于
TreeMap
,以便能够找到具有给定(姓、名)的每个人。 如果要按ID搜索,只需创建第二个索引,将ID映射到行索引,可以是
HashMap


对于这个小例子,索引并没有什么好处,因为它们可能比矩阵本身占用更多的空间,但如果您的记录很大,它可能会带来回报。

一般来说,如果您希望使用多种方式访问Java数据结构,则必须创建额外的“并行”结构

例如,您可以让您的“主表”——无论是数组、列表还是其他什么——按名称排序或键入。然后,您将创建第二个表,按客户编号排序,该表中的每个条目都可以包含第一个表的索引,或者对象句柄的另一个副本。然后,您可以使用第三个表,该表按出生日期排序,其条目也指向第一个表,以此类推

例如:

class Customer
{
  public String name;
  public int customerNumber;
  public int shoeSize;
  ... whatever ...
}
class byName implements Comparator<Customer>
{
  public int compareTo(Customer c1, Customer c2)
  {
    return c1.name.compareTo(c2.name);
  }
}
class byShoeSize implements Comparator<Customer>
{
  public int compareTo(Customer c1, Customer c2)
  {
    return c1.shoeSize-c2.shoeSize;
  }
}

... elsewhere ...
Customer[] nameOrder=new Customer[100];
nameOrder[0]=new Customer("Fred Smith", 10001, 9);
nameOrder[1]=new Customer("Mary Jones", 10002, 7);
... etc, however we get the list initialized ...
Arrays.sort(nameOrder, byName);

Customer[] shoeSizeOrder=new Customer[100];
for (int n=0;n<customerList.length;++n)
  byNumber[n]=customerList[n];
Arrays.sort(shoeSizeOrder, byShoeSize);
class客户
{
公共字符串名称;
公共int客户编号;
公共鞋类;
无论什么
}
类byName实现了Comparator
{
公共内部比较(客户c1、客户c2)
{
返回c1.name.compareTo(c2.name);
}
}
类byShoeSize实现了比较器
{
公共内部比较(客户c1、客户c2)
{
返回c1.shoeSize-c2.shoeSize;
}
}
... 在别处
客户[]名称订单=新客户[100];
nameOrder[0]=新客户(“Fred Smith”,10001,9);
nameOrder[1]=新客户(“玛丽·琼斯”,10002,7);
... 等等,但是我们初始化了列表。。。
Arrays.sort(nameOrder,byName);
客户[]shoeSizeOrder=新客户[100];

对于(int n=0;nhey抱歉,我不完全理解您将如何实现您的建议。如果我有一个数组,并且每个插槽指向一个哈希,而这些哈希中的每个键指向其他哈希,那么我如何按姓氏搜索呢?假设条目是
1-Smith,John
2-Stone,Jack
3-Stein,Robert
。您能告诉我如何使用您的代码通过id(2)和姓氏(Stone)搜索第二个条目吗?
class Customer
{
  public String name;
  public int customerNumber;
  public int shoeSize;
  ... whatever ...
}
class byName implements Comparator<Customer>
{
  public int compareTo(Customer c1, Customer c2)
  {
    return c1.name.compareTo(c2.name);
  }
}
class byShoeSize implements Comparator<Customer>
{
  public int compareTo(Customer c1, Customer c2)
  {
    return c1.shoeSize-c2.shoeSize;
  }
}

... elsewhere ...
Customer[] nameOrder=new Customer[100];
nameOrder[0]=new Customer("Fred Smith", 10001, 9);
nameOrder[1]=new Customer("Mary Jones", 10002, 7);
... etc, however we get the list initialized ...
Arrays.sort(nameOrder, byName);

Customer[] shoeSizeOrder=new Customer[100];
for (int n=0;n<customerList.length;++n)
  byNumber[n]=customerList[n];
Arrays.sort(shoeSizeOrder, byShoeSize);