0

I want to change the form of a given graph. The graph is in the form userID, number of followers, follower1, follower2, ..followerN, delimiter '---', userID2, ...etc.

I have to replace the followers with place values from a second file of the form ID1 place1 ID2 place2 ....

by matching the IDs.

Thus, I want to check if the followers id each time exists in a set. Both my graph and the set where I seek for the follower ids are huge.

Is there a more efficient way than the one I give you below?

 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.DataInputStream;
 import java.io.FileInputStream;
 import java.io.FileWriter;
 import java.io.InputStreamReader;
 import java.util.*;


public class Changer {
public static void main(String[] args) {


       Set set = new HashSet();
       int[][] users=new int[61578415][];



                 try{
        FileInputStream fstream2 = new FileInputStream(args[0]);
        DataInputStream in2 = new DataInputStream(fstream2);
        BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
        String strLine2;


                 while ((strLine2 = br2.readLine()) != null)   {  
                    set.add(strLine2);
                 }

                 in2.close();
                 fstream2.close();}
                 catch (Exception e){
        System.err.println("Error: " + e.getMessage()+"!\n"+e.toString()+"!\n");
        e.printStackTrace();
        System.exit(-1);
           }


           try{

        FileInputStream fstream = new FileInputStream("InputGraph.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int flag=0;
        int pos=0;


                 FileWriter fstream3 = new FileWriter("OutputGraph.txt");
        BufferedWriter out = new BufferedWriter(fstream3);


        int currentUser=0,counter=0;
        int theNum=0;
        while ((strLine = br.readLine()) != null)   {
            if(strLine.equals("---")){
                if(counter!=pos){
                    System.out.println("Error reading graph");
                    System.out.println("For:"+currentUser);
                    System.exit(-1);
                }
                flag=0;
                pos=0;
                continue;
            }

            theNum=Integer.parseInt(strLine);

            if (flag==0){

                           out.write("---"+"\n");

                           out.write(""+theNum);
                           out.write("\n");

                           currentUser=theNum;
               flag+=1;
            }
            else if (flag==1){
                counter=theNum;
                users[currentUser]=new int [counter];
                flag+=1;
                               out.write(""+theNum+"\n");
            }
            else{
                users[currentUser][pos]=theNum; 
                ++pos;

                               Iterator it = set.iterator();
                               while (it.hasNext()) {
                                 Object element = it.next();
                                 String[] arr = (String.valueOf(element)).split(" ");
                                 if (Integer.parseInt(arr[0])==theNum)
                                    {theNum=Integer.parseInt(arr[1]);break;}
                                 }


         out.write(""+theNum);
         out.write("\n");
            }
        }
        in.close();
         out.close();
    }catch (Exception e){
        System.err.println("Error: " + e.getMessage());
    }

    System.out.println("Graph has been read");
    System.gc();
    System.gc();


    System.out.println("Finished");
  }

   }
gauche_z
  • 1
  • 3

2 Answers2

1

It would be more efficient to do the for loop over intersection inside, so that you don't split and parse so much:

Iterator it = set.iterator();
while (it.hasNext()) {
    Object element = it.next();
    String[] arr = (String.valueOf(element)).split(" ");
    int arr0 = Integer.parseInt(arr[0]);
    int arr1 = Integer.parseInt(arr[1]);
    for (int integer : intersection) {
       if (arr0 == integer) {
          out.write(integer + " " + arr1 + "\n");
       }
    }
}

But this changes the order the write will be called in.

However I suspect you might benefit from loading it in to (or just replacing with) a HashMap or SparseArray. It's just hard to tell give then info you have given.

weston
  • 54,145
  • 21
  • 145
  • 203
0

For Integer detection, you can use the comparation with instanceof , but you must use objects and not primitives, e.g:

            Integer a=Integer.parseInt("12345");
            if(a instanceof Integer){
            System.out.println("We have an integer !"); 
            }

Another way for Integer detection is to override the equals method.

Andrew
  • 1
  • 1
  • `a instanceof Integer` will always be `true` here, but that's not what they are asking to do anyway. – weston Apr 02 '14 at 14:14