1.已知文件 source.txt 中的内容如下
username=root , password= 1234, id=1, level= 10;username= adimin, mima= 1234 , id=2, level= 11;yonghu= xiaoming ,dengji= 12 ,password= 1234,id= 3
(1)读取文本中的数据,将每一行的数据封装成模型,存入 List 中
(2)去除集合中id重复的数据,只保留重复id的第一个数据
(3)计算这些用户的平均等级、最高等级、最低等级
(4) 将集合中的数据按照等级降序排序
public class User {
String username;
String password;
int id;
int level;
public User() {
super();
}
public User(String uesername, String password, int id, int level) {
super();
this.username = uesername;
this.password = password;
this.id = id;
this.level = level;
}
@Override
public String toString() {
return "uesername=" + username + ", password=" + password + ", id=" + id + ", level=" + level ;
}
//根据id去重复只比较id
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
public class Test {
public static void main(String[] args) throws IOException {
//1.将文件读取出来
File file = new File("source.txt");
FileReader reader = new FileReader(file);
char[] cs = new char[500];
int num = reader.read(cs);
System.out.println(Arrays.toString(cs));
//2.将字符数组中的数据转成字符串
String str = new String(cs,0,num);
System.out.println(str);
//3.将字符串中所有的空格全部替换
String str2 = str.replace("[ ]+", "");
// username=root,password=1234,id=1,level=10;
// username=adimin,mima=1234,id=2,level=11;
// yonghu=xiaoming,dengji=12,password=1234,id=3
//创建一个List集合
ArrayList<User> list = new ArrayList<>();
//4.将字符串进行拆分
String[] strings = str2.split(";");
for (String string : strings) {
String[] strings2 = string.split(",");
User user = new User();
String[] arr1 = strings2[0].split("=");
String[] arr2 = strings2[1].split("=");
String[] arr3 = strings2[2].split("=");
String[] arr4 = strings2[3].split("=");
setProperty(arr1, user);
setProperty(arr2, user);
setProperty(arr3, user);
setProperty(arr4, user);
//属性赋值后将数据放入list集合
list.add(user);
}
//遍历list集合
for (User user : list) {
System.out.println(user);
}
ArrayList<User> list2 = second(list);
int sum = 0;
for (User user : list2) {
sum+=user.getLevel();
}
System.out.println("平均等级:"+sum/list2.size());
/*
* int minLevel = list2.get(0).getLevel();
* for (int i = 0; i < list2.size();i++) {
* if(minLevel>list2.get(i).getLevel()) {
* minLevel = list2.get(i).getLevel();
* }
* }
* System.out.println("最小等级"+minLevel);
* int maxLevel = list2.get(0).getLevel();
* for (int i = 0; i < list2.size();i++) {
* if(maxLevel<list2.get(i).getLevel()) {
* maxLevel =list2.get(i).getLevel();
* }
* }
* System.out.println("最大等级:"+maxLevel);
*/
Collections.sort(list2,new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
//根据level升序排序
return -(o1.getLevel()-o2.getLevel());
}
});
System.out.println("最小等级"+list2.get(list2.size()-1).getLevel());
System.out.println("最大等级"+list2.get(0).getLevel());
}
public static void setProperty(String[] arr,User user) {
if(arr[0].equals("yonghu")||arr[0].equals("username")) {
user.setUsername(arr[1]);
}else if (arr[0].equals("password")||arr[0].equals("mima")) {
user.setPassword(arr[1]);
}else if (arr[0].equals("id")) {
user.setId(Integer.parseInt(arr[1]));
}else if (arr[0].equals("level")||arr[0].equals("dengji")) {
user.setLevel(Integer.parseInt(arr[1]));
}
}
public static ArrayList<User> second(ArrayList<User> list) {
//通过set集合去除重复元素
HashSet<User> users = new HashSet<>(list);
ArrayList<User> list2 = new ArrayList<>(users);
return list2;
}
}