0

インデックス検索用の単純なデータベースのような構造が必要です。これはアプレットで使用されるため、追加の組み込みデータベース jar を使用したくありません。

要約: 類推で説明しようと思います。たとえば、学生テーブル:

Students
------------------
Score
Gender
Name

SCORE > 40 AND SCORE < 80 AND GENDER = MALE テーブル全体をスキャンすることがわかったソリューションをフィルター処理したいと考えています。しかし、私は TreeMap のようなソリューションを探しています。

TreeMap を使用してパーティショニングのような簡単なソリューションを作成しました。

package multikey;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;


public class MultiKey {




    public MultiKey() {

        final Integer MALE   = 1;
        final Integer FEMALE = 2;

        List<Student>                                    students = new ArrayList<Student>();
        TreeMap<Integer, TreeMap<Integer, List<String>>> table;

        students.add(new Student(80, MALE, "John"));
        students.add(new Student(80, FEMALE, "Monica"));
        students.add(new Student(70, MALE, "Michael"));
        students.add(new Student(60, MALE, "Brad"));
        students.add(new Student(60, FEMALE, "Angelina"));
        students.add(new Student(50, MALE, "Steve"));
        students.add(new Student(40, MALE, "Alex"));

        table = new TreeMap<Integer, TreeMap<Integer, List<String>>>();

        for (Student student : students) {
            Integer                        score     = student.score;
            Integer                        gender    = student.gender;
            TreeMap<Integer, List<String>> genderMap;
            List<String>                   names;

            if ((genderMap = table.get(score)) == null) {
                genderMap = new TreeMap<Integer, List<String>>();
                table.put(score, genderMap);
            }

            if ((names = genderMap.get(gender)) == null) {
                names = new ArrayList<String>();
                genderMap.put(gender, names);
            }

            names.add(student.name);
        }

        // 50 - 70 arasında alan erkekler
        SortedMap<Integer, TreeMap<Integer, List<String>>> students5070 = table.subMap(50, 71);
        List<String>                                       filtered     = new ArrayList<String>();

        for (Integer score : students5070.keySet()) {
            filtered.addAll(students5070.get(score).get(MALE));
        }

        System.out.println(filtered);
    }



    public static void main(String[] args) {

        new MultiKey();
    }




    public class Student {




        private int    gender;
        private String name;
        private int    score;




        public Student(int score, int gender, String name) {

            this.score  = score;
            this.gender = gender;
            this.name   = name;
        }




        public int getGender() {

            return gender;
        }



        public String getName() {

            return name;
        }




        public int getScore() {

            return score;
        }




        @Override
        public String toString() {

            return name;
        }
    }
}
4

1 に答える 1

1

必要なものが表だけの場合は、参照用としてのみでも、グアバ表が役立つ場合があります。SQL のような構文がさらに必要な場合は、 HSQLDBのような純粋な hava SQL データベースのようなものを使用することを避けることはできないと思います

于 2012-05-07T06:21:54.240 に答える