冒号左边为用户id,冒号右边为用户的好友列表
friends.txt
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.1 需求一:获取每个人的好友个数并降序排列.
1.2 需求二:获取任意两个人的共同好友(Scanner)
1.3 需求三:获取所有人两两共同好友
----------------------------------------------------------------------------------------------------------------------------
1.1获取每个人的好友个数并降序排列
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.*;
/**
* 统计好友个数并降序输出
*/
public class FriendDemo1 {
public static void main(String[] args) throws Exception {
//1.读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\friends.txt"));
//2.逐行处理
String line = null;
HashMap<String, Integer> resMap = new HashMap<>();
while ((line=br.readLine())!=null){
//2.1 读进来一行 A:B,C,D,F,E,O
String[] split = line.split(":");
String userId = split[0];
String[] friends = split[1].split(",");
resMap.put(userId,friends.length);
}
//3.排序
ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(resMap.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
//4.输出
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry);
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
1.2 需求二:获取任意两个人的共同好友(Scanner)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
* @author XXXX
*/
public class FriendDemo2 {
public static void main(String[] args) throws IOException {
Map<String,List<String>> map = new HashMap<>();
//1.读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\friends.txt"));
//2.逐行处理
String str;
while ((str = br.readLine()) != null){
//2.1 读入了一行数据 A:B,C,D,F,E,O
String[] s1 = str.split(":");
String key = s1[0];
String[] arr = s1[1].split(",");
List<String> value = new ArrayList<>();
for (String s : arr) {
value.add(s);
}
//2.2 存到一个Map中
map.put(key, value);
}
//3.用户输入要求谁的共同好友
Scanner sc = new Scanner(System.in);
System.out.println("请输出第一个id");
String id1 = sc.nextLine();
System.out.println("请输出第二个id");
String id2 = sc.nextLine();
//4.根据用户输入的id拿到对应的好友列表
if (map.containsKey(id1) && map.containsKey(id2)){
List<String> list1 = map.get(id1);
List<String> list2 = map.get(id2);
list1.retainAll(list2);
if (list1.size() ==0){
System.out.println(id1 + "和" + id2 + "没有共同好友");
}else{
System.out.println(id1 + "和" + id2 + "的共同好友为" + list1);
}
}else{
System.out.println("ID有错误请检查");
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
1.3 需求三:获取所有人两两共同好友
提示:类似九九乘法表
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class sss {
public static void main(String[] args) throws IOException {
//1.读文件
BufferedReader br = new BufferedReader(new FileReader("D:\\friends.txt"));
//2.逐行处理
String line = null;
HashMap<String, ArrayList<String>> resMap = new HashMap<>();
while ((line = br.readLine()) != null) {
//2.1 读入了一行数据 A:B,C,D,F,E,O
String[] split = line.split(":");
String userId = split[0];
String[] fris = split[1].split(",");
ArrayList<String> friends = new ArrayList<>();
for (String s : fris) {
friends.add(s);
}
//2.2 存到一个Map中
resMap.put(userId, friends);
}
//3.求两两共同好友 遍历id
ArrayList<String> keyList = new ArrayList<>(resMap.keySet());
for (int i = 0; i < keyList.size() - 1; i++) {
String id1 = keyList.get(i);
ArrayList<String> list1 = resMap.get(id1);
for (int j = i + 1; j < keyList.size(); j++) {
String id2 = keyList.get(j);
ArrayList<String> list2 = resMap.get(id2);
ArrayList<String> resultList = new ArrayList<>(list1);
resultList.retainAll(list2);
System.out.println(id1 + "和" + id2 + "的共同好友为" + resultList);
}
}
}
}