What is the best data structure representing the following data type?

I have a data type in the following format:

Popularity:xx
Name:xx
Author:xx
Sales:xx
Date Published: xx

I can choose how I can store my data.

I will need to perform some data queries, for example

  • What are the top "N" books for the year "M"
  • What are the average sales of the best "N" songs for the author of "X"?

Keep in mind that additional queries may be added.

What will be the different ways of presenting data to execute queries (in Java)? What will be the virtues?

Note: (not looking for a solution for the database)

+4
source share
4 answers

JDK Java DB .

: , , , , . , .

, Apache Commons Collections, .

​​ , , , . . , , . . .

+1

Arraylist . , , , Arraylist . . :

//replace "ClassName" with the name of the class
ArrayList<"ClassName"> array = new ArrayList<"ClassName">();
ArrayList<"ClassName"> results = new ArrayList<"ClassName">();


for("ClassName" obj:array)
{
    if(obj.getAuthor().equals("Author Name"))
    {
         results.add(obj);
    }
}

, Collections.sort(); , , .

ArrayList

EDIT: , , . . , , , .

+1

Bean :

public class Record {

int popularity;
String name;
String author;
int sales;
int yearPublished;

public Record(int popularity, String name, String author, int sales, int yearPublished) {
    super();
    this.popularity = popularity;
    this.name = name;
    this.author = author;
    this.sales = sales;
    this.yearPublished = yearPublished;

}
//getter and setter...

public String toString(){
    return name;
}

java8:

Record Record1 = new Record(10,"Record 1 Title","Author 1 Record",10,1990);
Record Record2 = new Record(100,"Record 2 Title","Author 2 Record",100,2010);
Record Record3 = new Record(140,"Record 3 Title","Author 3 Record",120,2000);
Record Record4 = new Record(310,"Record 4 Title","Author 1 Record",130,2010);
Record Record5 = new Record(110,"Record 5 Title","Author 5 Record",140,1987);
Record Record6 = new Record(160,"Record 6 Title","Author 1 Record",15,2010);
Record Record7 = new Record(107,"Record 7 Title","Author 1 Record",4,1980);
Record Record8 = new Record(1440,"Record 8 Title","Author 8 Record",1220,1970);
Record Record9 = new Record(1120,"Record 9 Title","Author 9 Record",1123,2010);

List<Record> Records = Arrays.asList(Record1,Record2,Record3,Record4,Record5,Record6,Record7,Record8,Record9);
//top 2 record of year 2010
int m = 2;
int year = 2010;
System.out.println(Arrays.toString(Records.stream().filter(s -> s.getYearPublished() == year).sorted((r1, r2) -> Integer.compare(r2.popularity, r1.popularity)).limit(m).toArray()));
//average top 2 record of Author 1 Record
String author= "Author 1 Record";
int n = 2;
System.out.println(Records.stream().filter(s -> author.equals(s.getAuthor())).sorted((r1, r2) -> Integer.compare(r2.popularity, r1.popularity)).limit(n).mapToInt(Record::getSales).average().getAsDouble());

:

[Record 9 Title, Record 4 Title]
72.5
+1

With a collection of objects, you can use stream api to collect / filter / reduce your results. It's not that much. The main problem is not to load all objects into memory and to be able to retrieve them from any storage efficiently using indexes, reverse indexes. One of the structures that crossed my mind is Apache spark

0
source

Source: https://habr.com/ru/post/1660811/


All Articles