【问题简述】:
要求用Redis完成以下功能,根据功能使用合适的数据结构:
(1)add(Stu), 增加学生时给个独一无二的id,username、phone手机号、java成绩、math成绩可以随意赋个合适值(不要从控制台输入),username有可能重名的情况,增加时无需特殊处理。phone必须保证唯一,如果不唯一,给出错误信息
(2) findByUsername(String),按照姓名查询,考虑查询到和查询不到,也要考虑重名情况,有多少个显示多少个。显示查询到学生信息。
(3)sortAvg(),用sorted set实现按平均成绩降序排序,并分页,分页大小为2,每页显示两个学生信息。
(4)sortAvgBySoringParam(int page),用SortingParams实现按平均成绩降序排序
【代码示例】:
package org.example;
public class Student {
private String username;
private String phone;
private int javaScore;
private int mathScore;
private long id;
private double averageScore;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getJavaScore() {
return javaScore;
}
public void setJavaScore(int javaScore) {
this.javaScore = javaScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public long getId(){
return id;
}
public void setId(long id) {
this.id=id;
}
public void setAvgScore(double averageScore) {
this.averageScore=averageScore;
}
public double getAvgScore(){
return averageScore;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", username='" + username + ''' +
", phone='" + phone + ''' +
", javaScore=" + javaScore +
", mathScore=" + mathScore +
", averageScore=" + averageScore +
'}';
}
}
package org.example;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import java.util.*;
public class StudentManager {
private static Jedis jedis;
private static final String stuPrefix = "student:";
private static final String phoneKey = "phone_set";
public StudentManager() {
jedis = new Jedis("localhost");
}
// (1)增加
public void add(Student stu) {
long studentId = jedis.incr("student_id_counter");
String studentKey = stuPrefix + studentId;
String phone = stu.getPhone();
if (jedis.sismember(phoneKey, phone)) {
//throw new IllegalArgumentException("PhoneNumber already exists.");
System.out.println("电话号码已经存在,增加学生失败。");
System.out.println();
}
else{
System.out.println("添加成功!");
}
jedis.hset(studentKey, "username", stu.getUsername());
jedis.hset(studentKey, "phone", phone);
jedis.hset(studentKey, "java", Integer.toString(stu.getJavaScore()));
jedis.hset(studentKey, "math", Integer.toString(stu.getMathScore()));
jedis.sadd(phoneKey, phone);
}
//(2)
public List findByUsername(String username) {
List result = new ArrayList();
Set studentKeys = jedis.keys(stuPrefix + "*");
for (String key : studentKeys) {
String storedUsername = jedis.hget(key, "username");
if (storedUsername.equals(username)) {
Map studentData = jedis.hgetAll(key);
Student student = new Student();
student.setId(Long.parseLong(key.split(":")[1]));
student.setUsername(studentData.get("username"));
student.setPhone(studentData.get("phone"));
student.setJavaScore(Integer.parseInt(studentData.get("java")));
student.setMathScore(Integer.parseInt(studentData.get("math")));
result.add(student);
}
}
return result;
}
//(3)
public List sortAvg() {
List result = new ArrayList();
Set studentIds = jedis.zrevrangeByScore("students_average_score", "+inf", "-inf");
for (String studentId : studentIds) {
String studentKey = stuPrefix + studentId;
Map studentData = jedis.hgetAll(studentKey);
Student student = new Student();
student.setId(Long.parseLong(studentId));
student.setUsername(studentData.get("username"));
student.setPhone(studentData.get("phone"));
student.setJavaScore(Integer.parseInt(studentData.get("java")));
student.setMathScore(Integer.parseInt(studentData.get("math")));
int j服务器托管网avaScore = student.getJavaScore();
int mathScore = student.getMathScore();
double avgScore = (javaScore + mathScore) / 2.0;
student.setAvgScore(avgScore);
result.add(student);
}
return result;
}
//(4)
public static List sortAvgBySortingParam(int page) {
List result = new ArrayList();
try (Jedis jedis = new Jedis("localhost")) {
int pageSize = 2;
int start = (page - 1) * pageSize;
int end = start + pageSize - 1;
Set sortedStudents = jedis.zrevrangeWithScores("students_average_score", start, end);
for (Tuple sortedStudent : sortedStudents) {
String studentId = sortedStudent.getElement();
String username = jedis.hget("student:" + studentId, "username");
String phone = jedis.hget("student:" + studentId, "phone");
String javaScore = jedis.hget("student:" + studentId, "java");
String mathScore = jedis.hget("student:" + studentId, "math");
Student student = new Student();
student.setId(Long.parseLong(studentId));
student.setUsername(username);
student.setPhone(phone);
student.setJavaScore(Integer.parseInt(javaScore));
student.setMathScore(Integer.parseInt(mathScore));
result.add(student);
}
}
return result;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
StudentManager StuMan=new StudentManager();
//(1)
Student stu=new Student(); //当电话号码重复时,应该抛出异常
stu.setId(3); //如果不指定学生的id,那么增加学生的过程中,之前的学生信息会被覆盖掉
stu.setUsername("zhaoliu");
stu.setPhone("200207216799");
stu.setJavaScore(93);
stu.setMathScore(88);
StuMan.add(stu);//调用add方法增加学生
//(2)
System.out.print("请输入您要查找的学生的名字(如:lisi):");
String findUsername=scanner.next();
List students=StuMan.findByUsername(findUsername); //调用findbyUsername函数找到符合条件的学生信息
if(students.isEmpty()){ //输出结果
System.out.println("未找到用户名为"+findUsername+"的学生。");
}
else{
System.out.println("用户名为"+findUsername+"的学生如下所示:");
for(Student student:students){
System.out.println("学生id:"+student.getId());
System.out.println("学生姓名:"+student.getUsername());
System.out.println("电话号码: " + student.getPhone());
System.out.println("Java 分数: " + student.getJavaScore());
System.out.println("数学分数: " + student.getMathScore());
System.out.println();
}
}
//(3)
List sortStu=StuMan.sortAvg();
System.out.println("用sorted set按平均成绩降序排序:");
for(Student student:sortStu){
System.out.println(student.toString()); /服务器托管网/如果不使用toString(),那么输出的结果是对象哈希码
System.out.println();
}
//(4)
List Stu = sortAvgBySortingParam(1);
System.out.println("用SortingParams实现按平均成绩降序排序:");
for (Student student : Stu) {
System.out.println(student);
}
}
}
【问题简述】:
需要注意点就是使用toString(),因为在将student中的数据输出时,如果不使用toString()方法,那么输出的结果将会是student对象哈希码,那么我们将无法看到正确的输出结果。
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 性能分析5部曲:瓶颈分析与问题定位,如何快速解决瓶颈?
一、引言 很多做性能测试的同学都问过我这样一个问题:鱼哥(Carl_奕然),你说性能测试的重点是什么? 我的回答很简单:瓶颈分析与问题定位。 在性能项目的整个周期,不管是脚本设计,脚本编写还是脚本执行,都还算简单。 难点在于如何定位瓶颈,分析瓶颈,解决瓶颈。 …