一个java中HashMap和HashSet的应用实例

2018-3-27

老师让我们写代码实现这样的功能:
1.给你好多组关系

12
34
23
15
...

表示1和2有联系,3和4有联系…
2.得到和任意一个节点有联系的所有节点
3.判断任意两个节点之间是否有联系

我们可以用一个二维数组,x[i][j]为true表示i节点与j节点之间有关系,但是如果给的数特别大呢,又或者不为int类型数据呢?

我们这里采用的是HashMap和HashSet,其中HashMap是键值对的格式,HashSet有一种集合的感觉,不会出现重复的元素。

package email;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;

public class t1 {
    public static void main(String[] args)throws IOException{
        String filename="C:\\Documents and Settings\\Administrator\\桌面\\email.txt";
        ArrayList<Integer[]> arrayResult=getData(filename);
        System.out.println(arrayResult.size());
        HashMap<Integer,HashSet> hashmapResult= new HashMap<Integer,HashSet>();
        hashmapResult=setRelation(arrayResult);
        GetRelation(hashmapResult,1);
        System.out.println(hashmapResult.size());
        hasRelation(hashmapResult,1,33);
        hasRelation(hashmapResult,1,23);
    }

    private static ArrayList<Integer[]> getData(String Filename) throws IOException{
        ArrayList<Integer[]> myarraylist=new ArrayList<Integer[]>();
        File file =new File(Filename);
        BufferedReader reader=new BufferedReader(new FileReader(file));
        String pair=null;
        String[] tmpArray=null;
        Integer[] intArray=null;
        while ((pair=reader.readLine())!=null){
           tmpArray = pair.split(",");
           intArray = new Integer[]{Integer.parseInt(tmpArray[0]),
                   Integer.parseInt(tmpArray[1])};
           myarraylist.add(intArray);
        }
        reader.close();
        return myarraylist;
    }

    private static void addRelation(HashMap<Integer,HashSet> hashmapResult,
            Integer Node1,Integer Node2){
        HashSet tmpset=new HashSet();
        if (hashmapResult.containsKey(Node1)){
            tmpset=hashmapResult.get(Node1);
            tmpset.add(Node2);
        }else{
            tmpset.add(Node2);
            hashmapResult.put(Node1, tmpset);
        }
    }

    private static HashMap<Integer,HashSet> setRelation(ArrayList<Integer[]> arrayResult){
        HashMap<Integer,HashSet> hashmapResult=new HashMap<Integer,HashSet>();
        Integer tmpint[]=null;
        for (int i=0;i<arrayResult.size();i++){
            tmpint=arrayResult.get(i);
            addRelation(hashmapResult,tmpint[0],tmpint[1]);
        }
        return hashmapResult;

    private static void GetRelation(HashMap<Integer,HashSet> hashmapResult,Integer Node){
        HashSet<Integer> myset=new HashSet();
        myset=hashmapResult.get(Node);
        System.out.print("和节点"+Node+"有关的节点为:");
        Iterator<Integer> it = myset.iterator();
        Integer neighborID = 0;
        while(it.hasNext()) {
            neighborID = it.next();
            System.out.print(neighborID + ",");
        }
        System.out.println();
    }

    private static void hasRelation(HashMap<Integer,HashSet> hashmapResult,Integer Node1,Integer Node2){
        HashSet<Integer> myset=hashmapResult.get(Node1);
        if (myset.contains(Node2)) System.out.println("Yes!");
        else System.out.println("No!");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值