0
색인이 생성 된 검색을위한 구조와 같은 간단한 데이터베이스가 필요합니다. 그것은 애플릿에서 사용될 것이므로 추가로 내장 된 데이터베이스 jar를 사용하고 싶지는 않습니다.표는 색인 된 컬렉션 필터링과 비슷합니다.
총계 : 나는 anology로 설명하려고 노력한다. 인스턴스 A 학생 테이블 :
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;
}
}
}
얼마나 많은 레코드가 있습니까? – sjr
최대 10000 개의 레코드 –
매번 선형 스캔으로 필터를 걸 수 있습니다. 10,000은 매우 작은 목록입니다. 심지어 레코드 수가 10 배 이상이라면 다른 솔루션보다 빠를 것입니다. 10^5 개의 레코드가 있다고 가정하면 각 레코드는 1000 개의 명령어 (높은 추정치)를 사용하여 일치하는지 확인합니다. 1GHz 프로세서는 약 0.1 초 내에이를 수행합니다. 충분히 빨리해야할까요? – sjr