Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
kakooo16
Creator
Creator

How to create a list of items using tjava

hello ,

I'm trying to create a list of files that are opened by user .

Here is my job design

0695b00000L1YZ6AAN.png

to check if a file is open i used this code

File sourceFile = new File(((String)globalMap.get("tFileList_3_CURRENT_FILEPATH")).toString());

List<String> file_Name_Opend=new ArrayList<String>();

if(!sourceFile.renameTo(sourceFile)) {

  

    file_Name_Opend.add(sourceFile.toString()) ; 

    System.out.println("** Files opened are :"+  file_Name_Opend);

      

 }

But my list is always overwritten and got always the last file that is opened .

How to resolve this ?

Labels (2)
5 Replies
Anonymous
Not applicable

You are nearly there, but you are overlooking the fact that the tJava2 is run in its entirety for every iteration. This means that you are recreating your ArrayList for every file. This is why the data is overwritten.

 

I was round this is to use the globalMap HashMap. I have converted your code above to use this and built a demo job to test it.

My job looks like this.....

0695b00000L1Z9xAAF.png 

It's essentially the same as yours, except I have added a tJava below to iterate over the data collected to test my code.

 

The code which updates yours is below......

 

*****************************************************************************************************

java.util.ArrayList<String> file_Name_Opend = null;

 

//Check to see if an ArrayList already exists in the globalMap. If not, create it.

if(globalMap.get("myFileList")==null){

file_Name_Opend=new java.util.ArrayList<String>();

 

}else{

file_Name_Opend = ((java.util.ArrayList)globalMap.get("myFileList"));

 

}

 

 

String sourceFile = ((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"));

 

file_Name_Opend.add(sourceFile) ; 

 

globalMap.put("myFileList", file_Name_Opend);

*****************************************************************************************************

 

My code in the tJava below to test what is collected is seen below.........

 

*****************************************************************************************************

java.util.ArrayList<String> file_Name_Opend = ((java.util.ArrayList)globalMap.get("myFileList"));

 

java.util.Iterator<String> it = file_Name_Opend.iterator();

 

while(it.hasNext()){

 

System.out.println(it.next());

}

*****************************************************************************************************

billimmer
Creator III
Creator III

Yes try tjavaflex instead to do this.

Anonymous
Not applicable

The tJavaFlex won't solve the problem here. On a flow link it would, but this is an iterate link. As such the Start and End sections of the tJavaFlex will be triggered for every iteration as well. However, it is certainly the way to go in most situations.

billimmer
Creator III
Creator III

tFileList ---iterate----> tIterateToFlow -----row----> tjavaflex

 

Anonymous
Not applicable

Yes, that should work. The code would look something like below I think.....

 

Start Code

*****************************************************************************************************

java.util.ArrayList<String> file_Name_Opend = new java.util.ArrayList<String>();

*****************************************************************************************************

 

Main Code

*****************************************************************************************************

file_Name_Opend.add(row.column) ; 

*****************************************************************************************************

 

End Code - to save the ArrayList object for use later in the job

*****************************************************************************************************

globalMap.put("myFileList", file_Name_Opend); 

*****************************************************************************************************

 

Thanks for the suggestion, it helps to give as many solutions as possible 🙂