社交用户关系数据分析案例拓展

public class ReadFile3 {
//静态代码块

static Map<String, List<String>> map=null;
static {    map = getUserInfo("E:\\java增强\\date\\day01\\好友.txt");

    }

public static void main(String[] args) {
    Listlist=getSameFriends("A","B");
    for (String string : list) {
        System.out.println(string);
    }
}

//方法1/求2集合的交集


public static List<String>  getSameFriends(String uid1,String uid2){
    Map<String, List<String>> map = getUserInfo("E:\\java增强\\date\\day01\\好友.txt");

    List<String> list1 = map.get(uid1);
    List<String> list2 = map.get(uid2);

    list1.retainAll(list2);
    //求2个集合的交集,结果数据返回给调用者集合
    //System.out.println(list1);

    return list1;
    }

//方法2/遍历好友


public static Map<String,List<String>>getUserInfo(String path){

    Map<String,List<String>>map=new HashMap<>();
        try(
            BufferedReader br = new BufferedReader(new FileReader(path));   
                ) {
            String line=null;
            while((line=br.readLine())!=null) {
                String[]arr1=line.split(":");
                String name=arr1[0];
                String[]arr2=arr1[1].split(",");
            //将数组转成集合List**Array的集合只读
                List<String>list2=Arrays.asList(arr2);
                ArrayList<String> newList = new ArrayList<>(list2);
                map.put(name,newList);
            }

        } catch (Exception e) {

            e.printStackTrace();
        }
        return map;
    }
}

你可能感兴趣的:(社交用户关系数据分析案例拓展)