1. Social user relationship analysis
Data example:
the left side of the colon is the user id, and the right side of the colon is the user's friend list
Requirement 1: The number of friends of each user (user) Tao Ge: 7 Yang Ge: 5
Steps: 1): Use io stream Read data
2): Divide each line of data
1; Use: Divide ---> User Friend List (String)
2; Divide the buddy list again (,)
3) Ask for the number
of friends 4) According to the number of friends Sort by quantity
//Completed
Requirement 2: Common friends between two pairs The common friends
of Brother Tao and Brother Yang are: Brother Yuan, Brother Xing The common friends of Brother
Tao and Brother Yuan are: . . . . . .
1: Read data
2: Data segmentation
1): Divide by colon User friend list
2): Divide the friend list ---> each friend
3: Load the data into the map set
4: Get the friend list (Two pairs of users seeking common friends)
5: Find common friends of two users
6: Output the result (save the result to a file) userId: friend 1, friend 2.. . . Brother Tao: Brother Yang, Brother Yuan, Handsome, Ding Ding, Brother Xing, Xiaopengpeng, Sister Na Brother Yang : Brother Tao, Brother Yuan, Brother Xing, Brother Hui, Brother Yang Yuan Brother: Brother Ding, Brother Tao, Handsome, Brother Handsome : Brother Tao, Brother Xing, Ding Ding, Brother Kan
Brother Xing: Brother Yang, Brother Yuan, Handsome, Brother Kai, Brother Kan
Ding Ding : Brother Tao, Brother Yang, Brother Yuan, Handsome, Brother Xing, Brother Xiaopeng
, Brother Kai Shuaishuai, Xingge,
Dingding Sister Na: Brother Tao, Brother Yuan, Shuaishuai, Brother Xing, Xiaopengpeng Xingge:
Brother Tao, Xiaopengpeng
Sister Ma : Brother Yang, Xiaopengpeng Brother
Hui: Brother Tao, Brother Yuan, Handsome Brother
Kan : Handsome, Brother Xing, Ding Ding Brother
Kai: Brother Xing, Ding Ding
, Brother Ma Xiaopeng Peng: Brother Tao, Sister Na, Brother Xing, Brother Ma
4 MainTest2
Data example:
the left side of the colon is the user id, and the right side of the colon is the user's friend list
Requirement 1: The number of friends of each user (user) Tao Ge: 7 Yang Ge: 5
Steps: 1): Use io stream Read data
2): Divide each line of data
1; Use: Divide ---> User Friend List (String)
2; Divide the buddy list again (,)
3) Ask for the number
of friends 4) According to the number of friends Sort by quantity
//Completed
Requirement 2: Common friends between two pairs The common friends
of Brother Tao and Brother Yang are: Brother Yuan, Brother Xing The common friends of Brother
Tao and Brother Yuan are: . . . . . .
1: Read data
2: Data segmentation
1): Divide by colon User friend list
2): Divide the friend list ---> each friend
3: Load the data into the map set
4: Get the friend list (Two pairs of users seeking common friends)
5: Find common friends of two users
6: Output the result (save the result to a file) userId: friend 1, friend 2.. . . Brother Tao: Brother Yang, Brother Yuan, Handsome, Ding Ding, Brother Xing, Xiaopengpeng, Sister Na Brother Yang : Brother Tao, Brother Yuan, Brother Xing, Brother Hui, Brother Yang Yuan Brother: Brother Ding, Brother Tao, Handsome, Brother Handsome : Brother Tao, Brother Xing, Ding Ding, Brother Kan
Brother Xing: Brother Yang, Brother Yuan, Handsome, Brother Kai, Brother Kan
Ding Ding : Brother Tao, Brother Yang, Brother Yuan, Handsome, Brother Xing, Brother Xiaopeng
, Brother Kai Shuaishuai, Xingge,
Dingding Sister Na: Brother Tao, Brother Yuan, Shuaishuai, Brother Xing, Xiaopengpeng Xingge:
Brother Tao, Xiaopengpeng
Sister Ma : Brother Yang, Xiaopengpeng Brother
Hui: Brother Tao, Brother Yuan, Handsome Brother
Kan : Handsome, Brother Xing, Ding Ding Brother
Kai: Brother Xing, Ding Ding
, Brother Ma Xiaopeng Peng: Brother Tao, Sister Na, Brother Xing, Brother Ma
Country: Tao Ge
implementation code
1 friend.txt
Brother Tao: Brother Yang, Brother Yuan, Shuaishuai, Dingding, Brother Xing, Xiaopengpeng, Sister Na Brother Yang: Brother Tao, Brother Yuan, Brother Xing, Brother Hui, Brother Yang Brother Yuan: Ding Ding, Brother Tao, Handsome, Brother Xing Handsome: Brother Tao, Brother Xing, Ding Ding, Brother Kan Brother Xing: Brother Yang, Brother Yuan, Shuaishuai, Brother Kai, Brother Kan Ding Ding: Brother Tao, Brother Yang, Brother Yuan, Shuaishuai, Brother Xing, Xiaopengpeng, Brother Kai Brother Ma: Brother Tao, Brother Yuan, Handsome, Brother Xing, Ding Ding Sister Na: Brother Tao, Brother Yuan, Shuaishuai, Brother Xing, Xiaopengpeng Brother Xing: Brother Tao, Xiao Pengpeng Sister Ma: Brother Yang, Xiao Pengpeng Brother Hui: Brother Tao, Brother Yuan, Handsome Kan brother: handsome, star brother, ding ding Brother Kai: Brother Xing, Ding Ding, Brother Ma Xiaopengpeng: Brother Tao, Sister Na, Brother Xing, Sister Ma Country: Tao Ge
FileToArrayList.java
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class FileToArrayList { public static void main(String[] args) throws IOException { // Read data from a text file (one string per row) into the collection, and traverse the collection String srcPath = "D:\\New Folder(18)\\Case 1\\src\\ch01\\Friends.txt"; List<String> list = fileToArrayList(srcPath); for (String s : list) { System.out.println(s); } InputStream is =null; is = new FileInputStream("D:\\New Folder(18)\\Case 1\\src\\ch01\\Friends.txt"); InputStreamReader isr = new InputStreamReader(is,"utf-8"); BufferedReader bfReader = new BufferedReader(isr); String line = null; line = bfReader.readLine(); while(line!=null){ String[] user = line.split(":"); } } /** * Read the content from the specified file into the collection * @param srcPath * @return */ private static List<String> fileToArrayList(String srcPath) { List<String> list = new ArrayList<String>(); try (BufferedReader br = new BufferedReader(new FileReader(srcPath));) { String line;// is used to save the content of the line read each time while ((line = br.readLine()) != null) { list.add(line); } return list; } catch (Exception e) { e.printStackTrace (); return null; } } }3 MainTest
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 1. Use IO stream to read data (bufferedReader), each line of data is read * 2. Use string segmentation (1) Use colon to split, username, friend list * (2) The friend list is divided, and each friend Map<user and friend list> Map<user name and length> is converted into list sorting * 3. Obtain the number of friends corresponding to the user (the length behind) * 4. Sort by quantity */ public class MainTest { public static void main(String[] args) { Map<String, List<String>> map = getUserInfo(); HashMap<String, Integer> map2 = new HashMap<>(); Set<Entry<String,List<String>>> entrySet = map.entrySet(); for(Entry<String, List<String>> entry : entrySet){ map2.put(entry.getKey(),entry.getValue().size()); } //System.out.println(map2); List<UserNameCount> list=new ArrayList<>(); for(Entry<String,Integer> entry : map2.entrySet()){ UserNameCount user = new UserNameCount(); user.setUserName(entry.getKey()); user.setCount(entry.getValue()); list.add(user); } //sort Collections.sort( list, new Comparator<UserNameCount>(){ @Override public int compare(UserNameCount o2, UserNameCount o1) { return o1.getCount()-o2.getCount(); } }); for(UserNameCount s : list){ System.out.println("User name:"+s.getUserName()+" "+"Number of friends:"+s.getCount()); } } public static Map<String,List<String>> getUserInfo() { Map<String,List<String>> map = new HashMap<>(); try (BufferedReader br = new BufferedReader(new FileReader("D:\\好友.txt"));) { String line = null; while ((line = br.readLine()) != null) { //System.out.println(line); //Use string split and save to array String[] split = line.split(":"); String username = split[0]; String[] fs = line.split(","); //System.out.println(Arrays.toString(fs)); //Array.asList cannot add or delete operations map.put(username, Arrays.asList(fs)); } } catch (Exception e) { e.printStackTrace (); } return map; } }
4 MainTest2
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 1. Use IO stream to read data (bufferedReader), each line of data is read over 2. Use string division (1) Use colon to divide, user name, friend list * (2) The friend list is divided, and each friend Map<user and friend list> Map<user name and length> is converted into list sorting 3. Find the number of friends corresponding to the user (the length behind) * 4. Sort code optimization according to quantity */ public class MainTest2 { public static void main(String[] args) { Map<String, List<String>> map = getUserInfo(); HashMap<String, Integer> map2 = new HashMap<>(); Set<Entry<String, List<String>>> entrySet = map.entrySet(); for (Entry<String, List<String>> entry : entrySet) { map2.put(entry.getKey(), entry.getValue().size()); } // System.out.println(map2); // Directly use generics to unify a format, put them in the set collection, and output the key and value directly, Set<Entry<String, Integer>> entrySet2 = map2.entrySet(); ArrayList<Entry<String, Integer>> list = new ArrayList<>(entrySet2); Collections.sort(list, new Comparator<Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o1.getValue() - o2.getValue(); } }); for (Entry<String, Integer> s : list) { System.out.println(s); } } public static Map<String, List<String>> getUserInfo() { Map<String, List<String>> map = new HashMap<>(); try (BufferedReader br = new BufferedReader(new FileReader("D:\\好友.txt"));) { String line = null; while ((line = br.readLine()) != null) { // System.out.println(line); // Use string split and save to array String[] split = line.split(":"); String username = split[0]; String[] fs = line.split(","); // System.out.println(Arrays.toString(fs)); // Array.asList cannot add or delete map.put(username, new ArrayList<>(Arrays.asList(fs))); //map.put(username, Arrays.asList(fs)); } } catch (Exception e) { e.printStackTrace (); } return map; } }
5.MainTest3
import java.util.List; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import java.util.ArrayList; import java.util.Map; import java.util.Set; public class MainTest3 { public static void main(String[] args) throws Exception { BufferedWriter bf = new BufferedWriter(new FileWriter("D:\\lijian.txt")); // get friend list Map<String, List<String>> userInfo = MainTest2.getUserInfo(); // get user list Set<String> userSet = userInfo.keySet(); // save to list ArrayList<String> userList = new ArrayList<>(userSet); // print list length // System.out.println(userList.size()); // Loop comparison, similar to bubbling String user1 = null; String user2 = null; List<String> list3 = null; for (int i = 0; i < userList.size() - 1; i++) {// Control the number of times // get the first user user1 = userList.get(i); List<String> list1 = userInfo.get(user1); for (int j = i + 1; j < userList.size(); j++) { // get the second user user2 = userList.get(j); // as a reference, cannot operate on raw data List<String> list2 = userInfo.get(user2); // must operate on raw data list3 = new ArrayList(list2); // take the intersection list3.retainAll(list1); if (list3 != null && list3.size() > 0) { // can be spliced according to the required data System.out.println(user1 + " and " + user2 + " mutual friends " + list3); String str = user1 + " mutual friend with " + user2 + " " + list3; // save to file try { bf.write(str); bf.newLine(); bf.flush(); } catch (Exception e) { e.printStackTrace (); } } } } bf.close(); } }
6.
import java.util.ArrayList; import java.util.List; import java.util.Map; public class TestComFs { public static void main(String[] args) { ComFs(); } private static void ComFs() { Map<String,List<String>> userInfo = MainTest2.getUserInfo(); String name1 = "涛哥"; String name2 = "远哥"; //Get user 1's friend list List<String> list1 = userInfo.get(name1); List<String> list2 = userInfo.get(name2); ArrayList<String> arraylist = new ArrayList<String>(list1); arraylist.retainAll(list2); System.out.println(arraylist); } }
7.
public class UserBean { MainTest3 user1 = new MainTest3(); MainTest3 user2 = new MainTest3(); MainTest3 list3 = new MainTest3(); }
8.
public class UserNameCount { private String userName; private Integer count; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } @Override public String toString() { return "UserNameCount [userName=" + userName + ", count=" + count + "]"; } }