社交用户关系分析
数据样例:
冒号左边为用户id,冒号右边为用户的好友列表
A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E:B,C,D,M,L F:A,B,C,D,E,O,M G:A,C,D,E,F H:A,C,D,E,O I:A,O J:B,O K:A,C,D L:D,E,F M:E,F,G O:A,H,I,J |
需求:
1 获取所有用户对应的好友数量 取人缘最好的用户
2 获取制定两个用户的共同好友
3 获取所有人的好友信息
4 将以上三部的结果集写在本地磁盘
package Friends;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class BestUser {//求人缘最好的用户,即好友最多的用户
public static void main(String[] args) throws IOException {
//获取数据
BufferedReader br = new BufferedReader(new FileReader("DataFile\\Friednds.txt"));
//处理数据
String line = null;
Map<String, List<String>> userfriendsMap = new HashMap<String, List<String>>();
while((line=br.readLine())!=null){
String[] split = line.split(":");
String userId = split[0];
String friends = split[1];
String[] friend = friends.split(",");
List<String> frinedsList = Arrays.asList(friend);//数组转集合
userfriendsMap.put(userId, frinedsList);//key为用户,value为该用户的好友列表集合,存储在Map中
}
Set<Map.Entry<String, List<String>>> entries = userfriendsMap.entrySet();//一个个取出键值对,变成单列
List<Map.Entry<String, List<String>>> entrylist = new ArrayList<>(entries);//将单列存储在list集合中,以便后续排序
entrylist.sort(new Comparator<Map.Entry<String, List<String>>>() {//重写排序方法
@Override
public int compare(Map.Entry<String, List<String>> o1, Map.Entry<String, List<String>> o2) {
return o2.getValue().size()-o1.getValue().size();//取出list中的value进行降序排序
}
});
for (Map.Entry<String, List<String>> stringListEntry : entrylist) {//遍历输出list
System.out.println(stringListEntry);
}
System.out.println("人员最好的用户为"+entrylist.get(0).getKey()+",他的好友数量为:"+entrylist.get(0).getValue().size());
}
}
package Friends;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class ZhidingUser {//获取指定用户的共同好友
public static void main(String[] args) throws IOException {
//获取数据
BufferedReader br = new BufferedReader(new FileReader("DataFile\\Friednds.txt"));
//处理数据
String line = null;
Map<String, List<String>> userfriendsMap = new HashMap<String, List<String>>();
while ((line = br.readLine()) != null) {
String[] split = line.split(":");
String userId = split[0];
String friends = split[1];
String[] friend = friends.split(",");
List<String> frinedsList = Arrays.asList(friend);//数组转集合
userfriendsMap.put(userId, frinedsList);//key为用户,value为该用户的好友列表集合,存储在Map中
}
Set<Map.Entry<String, List<String>>> entries = userfriendsMap.entrySet();//一个个取出键值对,变成单列
List<Map.Entry<String, List<String>>> entrylist = new ArrayList<>(entries);//将单列存储在list集合中,以便后续排序
/* entrylist.sort(new Comparator<Map.Entry<String, List<String>>>() {//重写排序方法
@Override
public int compare(Map.Entry<String, List<String>> o1, Map.Entry<String, List<String>> o2) {
return o2.getValue().size() - o1.getValue().size();//取出list中的value进行降序排序
}
});*/
for (Map.Entry<String, List<String>> stringListEntry : entrylist) {//遍历输出list
System.out.println(stringListEntry);
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户1:");
String user1 = scanner.next();
String user_1 = user1.toUpperCase();
if(userfriendsMap.containsKey(user_1)){
System.out.println("请输入用户2:");
String user2 = scanner.next();
String user_2 = user2.toUpperCase();
List<String> list1 = userfriendsMap.get(user_1);//获取用户1的好友列表
List<String> list2 = userfriendsMap.get(user_2);//获取用户2的好友列表
List<String> list = new ArrayList<>(list1);//list为list1的副本
boolean b = list.retainAll(list2);//取交集
if(b){
System.out.println(list);
}else{
System.out.println("没有共同好友!!!");
}
}else{
System.out.println("输入错误!!!");
}
}
}
package Friends;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class AllInfo {//查看所有用户的共用好友
public static void main(String[] args) throws IOException {
//获取数据
BufferedReader br = new BufferedReader(new FileReader("DataFile\\Friednds.txt"));
//处理数据
String line = null;
Map<String, List<String>> userfriendsMap = new HashMap<String, List<String>>();
while ((line = br.readLine()) != null) {
String[] split = line.split(":");
String userId = split[0];
String friends = split[1];
String[] friend = friends.split(",");
List<String> frinedsList = Arrays.asList(friend);//数组转集合
userfriendsMap.put(userId, frinedsList);//key为用户,value为该用户的好友列表集合,存储在Map中
}
Set<Map.Entry<String, List<String>>> entries = userfriendsMap.entrySet();//一个个取出键值对,变成单列
List<Map.Entry<String, List<String>>> entrylist = new ArrayList<>(entries);//将单列存储在list集合中,以便后续排序
for (Map.Entry<String, List<String>> stringListEntry : entrylist) {//遍历输出list
System.out.println(stringListEntry);
}
for (int i = 0; i <entrylist.size() ; i++) {//外层控制行,用户
Map.Entry<String, List<String>> entryA = entrylist.get(i);//以谁为中心,其他用户和这个中心进行比对
String userAId = entryA.getKey();//获取中心用户的名字
List<String> userAfriendList = entryA.getValue();//获取中心用户的好友列表
for (int j = i+1; j < entrylist.size(); j++) {//内层控制列,相当于其他的用户
Map.Entry<String, List<String>> entryB = entrylist.get(j);//得到其他用户的键值对
String userBId = entryB.getKey();//得到其他用户的用户名字
List<String> userBfrinedList = entryB.getValue();
List<String> list = new ArrayList<>(userAfriendList);
//为什么要用副本?因为这是在第二个for循环内,此时会改变中心用户的好友列表,第一轮改变之后会影响后面的比较
list.retainAll(userBfrinedList);
if(list.size()!=0){
System.out.println(userAId+"和"+userBId+"的共同好友为:"+list);
}else{
System.out.println(userAId+"和"+userBId+"没有共同好友");
}
}
}
}
}
package Friends;
import java.io.*;
import java.util.*;
public class Write {//将前面三个结果写入到本地磁盘上
static Map<String, List<String>> userfriendsMap = new HashMap<String, List<String>>();
static List<Map.Entry<String, List<String>>> entrylist;
static BufferedWriter bw;
static {//静态代码块只需要加载一次
try {
bw = new BufferedWriter(new FileWriter("DataFile\\result.txt"));
} catch (IOException e) {
e.printStackTrace();
}
}
static {
try {
readFile();//静态代码块会随着类的加载而加载一次
} catch (IOException e) {
e.printStackTrace();
}
}
//读文件
public static void readFile() throws IOException {//读文件的方法
//获取数据
BufferedReader br = new BufferedReader(new FileReader("DataFile\\Friednds.txt"));
//处理数据
String line;
while((line=br.readLine())!=null){
String[] split = line.split(":");
String userId = split[0];
String friends = split[1];
String[] friend = friends.split(",");
List<String> frinedsList = Arrays.asList(friend);//数组转集合
userfriendsMap.put(userId, frinedsList);//key为用户,value为该用户的好友列表集合,存储在Map中
}
}
//需求1:求人缘最好的用户的方法,即好友最多的用户
public static void bestUser() throws IOException {
Set<Map.Entry<String, List<String>>> entries = userfriendsMap.entrySet();//一个个取出键值对,变成单列
entrylist = new ArrayList<>(entries);//将单列存储在list集合中,以便后续排序
entrylist.sort(new Comparator<Map.Entry<String, List<String>>>() {//重写排序方法
@Override
public int compare(Map.Entry<String, List<String>> o1, Map.Entry<String, List<String>> o2) {
return o2.getValue().size() - o1.getValue().size();//取出list中的value进行降序排序
}
});
String a = "人员最好的用户为" + entrylist.get(0).getKey() + ",他的好友数量为:" + entrylist.get(0).getValue().size();
writeFile(a);
}
//需求2:获取指定用户的共同好友
public static void zhiDingUser() throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户1:");
String user1 = scanner.next();
String user_1 = user1.toUpperCase();
if(userfriendsMap.containsKey(user_1)){
System.out.println("请输入用户2:");
String user2 = scanner.next();
String user_2 = user2.toUpperCase();
List<String> list1 = userfriendsMap.get(user_1);//获取用户1的好友列表
List<String> list2 = userfriendsMap.get(user_2);//获取用户2的好友列表
List<String> list = new ArrayList<>(list1);//list为list1的副本
boolean b = list.retainAll(list2);//取交集
if(b){
System.out.println(user_1+"和"+user_2+"的共同好友为:"+list);
String a = user_1+"和"+user_2+"的共同好友为:"+list;
writeFile(a);
}else{
System.out.println(user_1+"和"+user_2+"没有共同好友!!!");
String a = user_1+"和"+user_2+"没有共同好友!!!";
writeFile(a);
}
}else{
System.out.println("输入错误!!!");
String a = "输入错误!!!";
writeFile(a);
}
}
//需求3:查看所有用户的共用好友
public static void allInfo() throws IOException {
Set<Map.Entry<String, List<String>>> entries = userfriendsMap.entrySet();//一个个取出键值对,变成单列
List<Map.Entry<String, List<String>>> entrylist = new ArrayList<>(entries);//将单列存储在list集合中,以便后续排序
/*for (Map.Entry<String, List<String>> stringListEntry : entrylist) {//遍历输出list
System.out.println(stringListEntry);
}*/
for (int i = 0; i <entrylist.size() ; i++) {//外层控制行,用户
Map.Entry<String, List<String>> entryA = entrylist.get(i);//以谁为中心,其他用户和这个中心进行比对
String userAId = entryA.getKey();//获取中心用户的名字
List<String> userAfriendList = entryA.getValue();//获取中心用户的好友列表
for (int j = i+1; j < entrylist.size(); j++) {//内层控制列,相当于其他的用户
Map.Entry<String, List<String>> entryB = entrylist.get(j);//得到其他用户的键值对
String userBId = entryB.getKey();//得到其他用户的用户名字
List<String> userBfrinedList = entryB.getValue();
List<String> list = new ArrayList<>(userAfriendList);
//为什么要用副本?因为这是在第二个for循环内,此时会改变中心用户的好友列表,第一轮改变之后会影响后面的比较
list.retainAll(userBfrinedList);
String a ;
if(list.size()!=0){
a=userAId+"和"+userBId+"的共同好友为:"+list;
writeFile(a);
}else{
a = userAId+"和"+userBId+"没有共同好友";
writeFile(a);
}
}
}
}
//需求4:将以上三个需求的结果写入本地磁盘
public static void writeFile(String a) throws IOException {
bw.write(a);
bw.newLine();
bw.flush();
}
public static void closeBwStream() throws IOException {
bw.close();
bw=null;
}
public static void main(String[] args) throws IOException {
bestUser();//需求1:写人缘最好的用户
zhiDingUser();//需求2:求指定两个用户的共同好友
allInfo();//需求3:求每个用户的共同好友
}
}
额外的需求:
5. 将数据写入到数据库中,并且能够使用sql语句进行查询需求1、2、3
/*
* 需求:将数据写入数据库,并且能够使用sql语句完成查询需求
* 步骤:1、使用JDBC进行数据交互
* 2、根据数据的形态在数据库中建表
* 3、插入数据(需要DruidUtil,dbutils、connector、druid三个架包)
*/
public class WriteToSql {
/*具体实现步骤:1、先读文件,即下面的readFile()方法
* 2、进行插入,即下面的insertData()方法
* 3、从数据库中查询需求1,即下面的getBestUser()方法,此时需要写FriendBean,
* 封装String user;String result
* 4、从数据库中查询需求2,即下面的getTwoUserSameFriend()方法,此时需要写
* FriendBean,封装String user;String result
* 5、从数据库中查询需求2,即下面的getRandomUserSameFriend()方法,此时需要写
* FriendBean,封装String user1;String user2;String result
public static void readFile() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("D:\\JDBC\\DataFile\\Friednds.txt"));
String line =null;
while((line=br.readLine())!=null) {
String[] split = line.split(":");
String userA = split[0];
String friends = split[1];
String[] split1 = friends.split(",");
for (String userB : split1) {
insertData(userA,userB);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void insertData(String A,String B){
Connection connection = DruidUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
String sql = "insert into society (user,friend) values(?,?)";
try {
queryRunner.insert(connection,sql,new BeanListHandler<>(Write.class),A,B);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void getBestUser(){
Connection connection = DruidUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
String sql = "select user , count(user) as result from society group by user having result = (select count(user) as result from society group by user order by count(user) desc limit 1)";//查询人员最好的用户
//sql语句中的字段需要改为和Bean中的一样,否则取不到值
try {
List<FriendBean> query = queryRunner.query(connection, sql, new BeanListHandler<>(FriendBean.class));
for (FriendBean friendBean : query) {
System.out.println("人员最好的用户为"+friendBean.getUser()+",他的好友数量为:"+friendBean.getResult());
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void getTwoUserSameFriends(){
Connection connection = DruidUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
String sql = "select user, GROUP_CONCAT(friend) as result from society as s1 where s1.user = ? and s1.friend in (select friend from society where user = ?)";//查询人员最好的用户
//sql语句中的字段需要改为和Bean中的一样,否则取不到值
try {
List<FriendBean> query = queryRunner.query(connection, sql, new BeanListHandler<>(FriendBean.class), "A", "B");
System.out.println(query);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void getRandomUserSameFriend(){
Connection connection = DruidUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT s1.user as user1,s2.user as user2,GROUP_CONCAT(s1.friend) as result from society as s1 join society as s2 on s1.user != s2.user and s1.friend = s2.friend GROUP BY s1.user,s2.user;\n";//查询人员最好的用户
//sql语句中的字段需要改为和Bean中的一样,否则取不到值
try {
List<FriendBean1> query = queryRunner.query(connection, sql, new BeanListHandler<>(FriendBean1.class));
for (FriendBean1 same : query) {
System.out.println(same);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void main(String[] args) {
//readFile();
getBestUser();
}
}