Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
tnewbie
Creator II
Creator II

Iterate on a list of values

Hello All,

 

I need to count the number of occurrences of certain predefined strings (but these may increase or decrease so want to make it data driven or file driven) against every line of incoming data (data coming in a file).

 

To explain further:

For each of this incoming lines of data i have to check the count of certain strings like 

"ABC", "XYZ" ,"AAABC","UCAD1","DDEZY"

 

my incoming data is something like this:

California,ABC,123,Oct23Purview,ABCD,ABC,A,B,XYZ,UCAD1,ABC,DD,UCAD2,UCAD1  ---> line 1
Houston,XYZ,XYZ,Jan21Conclusion,ABC,AB,XYZ,XYZD,ABC,IDE123,IDE870,ABD,UCAD1 --> line 2

 

Output i am looking for is:

line 1 --ABC =3, XYZ = 1, AAABC=0,UCAD1=1, DDEZY=0

line 2 --ABC=2,XYZ=3 ,AAABC=0 ,UCAD1=1, DDEZY=0

Any thoughts please?

 

Labels (2)
5 Replies
Anonymous
Not applicable

Use Collections.frequency

String [] strs = new String[] {"ABC", "XYZ" ,"AAABC","UCAD1","DDEZY"};

ArrayList<String> ll = new ArrayList<>();

for (String s : strs){
	ll.add(s + "=" + Collections.frequency(Arrays.asList(row1.data.split(",")),s));
}

System.out.println(String.join(",", ll)); //ABC=3,XYZ=1,AAABC=0,UCAD1=2,DDEZY=0
//ABC=2,XYZ=3,AAABC=0,UCAD1=1,DDEZY=0
tnewbie
Creator II
Creator II
Author

Hello evansdar, can you elaborate a bit, as to how you envision the solution

tnewbie
Creator II
Creator II
Author

Hello All,

 

Can you throw some light on this simple scenario...I am sure people working in Talend would have come across this situation, please spend a minute to read.

Anonymous
Not applicable

Not sure what you're expecting here. My code does what you're asking, what more do you need?

tnewbie
Creator II
Creator II
Author

Hello evansdar,

 

Thanks for the time spent...I am an ETL person and i look for data driven approaches, as such i am not a Java person, so your proposed solution looks little constrained to me because the way you tried to push the strings into an array {"ABC", "XYZ" ,"AAABC","UCAD1","DDEZY"}; will confine the solution if I have to add / remove strings at a later point in time....also being a non-java person i didn't know how to include your code (probably a tjavaflex would have served the purpose, but i am not sure) .

 

I did move forward with a work around where i had all the strings as separate text files and used a tFileList which can dynamically source these values and play around with a context variable...though i could achieve the result, i am not personally convinced with the approach so was looking around to see if some other brains might have a better solution.

 

Thanks again

 


my_solution.png