1

I checked the following links - Link1 Link2 Link3 but was unable to resolve & hence posting.

Need:

Partial matching of Strings (exact matching not possible in all cases) of 2 string fields & get the info from the matched row. The data is stored in a MySQL db.

The transaction data table contains the description which needs to be compared to the Company name in the Scrip-Info table. I need to get the company code info from Scrip-Info Table, by matching as many words from Transaction Table to Scrip Info Table

Code:

for (List<String> listData : transactionData) {
       List<List<String>> code = scripInfo.stream()
                .filter(p -> p.get(1).toUpperCase().contains(listData.get(1).toUpperCase()))
                .collect(Collectors.toList());
// Output to console to check if its working
code.forEach(p -> System.out.println(p.get(0)));
//Prepare Output List
List<Object> rowData = new ArrayList<>();
rowData.add(code.get(0));
rowData.add(listData.get(2));
rowData.add(listData.get(3));
.........

}

Although I use 'Contains' in the above code it gets the data only if it is an exact match. So from the below example data I have provided only CANARA BANK is found as it is identical in both tables

Note: Data is stored in MySQL db & extracted.

Scrip-Info Table

  • [BHARTIARTL, Bharti Airtel Limited]
  • [BHEL, Bharat Heavy Electricals Limited]
  • [CANBK, Canara Bank]
  • [HINDUNILVR, Hindustan Unilever Limited]
  • [MARUTI, Maruti Suzuki India Limited]
  • [TATAPOWER, Tata Power Company Limited]
  • [TATASTEEL, Tata Steel Limited]
  • [TECHM, Tech Mahindra Limited]

Transaction Data Table

  • [10144, CANARA BANK, B, 100]
  • [10278, BHARTI AIRTEL LTD, B, 50]
  • [10278, BHARTI AIRTEL LTD, B, 20]
  • [10278, HIND.UNILEVER LTD., B, 12]
  • [10278, HIND.UNILEVER LTD., B, 32]
  • [10278, MARUTI SUZUKI INDIA LTD., S, 26]
  • [10278, MARUTI SUZUKI INDIA LTD., S, 26]
  • [10278, TECHM FUT 28AUG 14, S, 125]
  • [10278, TECHM FUT 28AUG 14, B, 125]
  • [11585, TATA STEEL LTD., B, 50]
  • [11585, TATA POWER CO. LTD., B, 100]

Required Output:

  • [CANBK, B, 100]
  • [BHARTIARTL, B, 50]
  • [BHARTIARTL, B, 20]
  • [BHARTIARTL, B, 80]
  • [HINDUNILVR, B, 12]
  • [HINDUNILVR, B, 32]
  • [HINDUNILVR, B, 52]
  • [MARUTI, S, 26]
  • [MARUTI, B, 26]
  • [TECHM, S, 125]
  • [TECHM, B, 125]
  • [TATASTEEL, B, 50]
  • [TATAPOWER, B, 100]

Output with present Code:

  • [CANBK, B, 100]
iCoder
  • 1,406
  • 6
  • 16
  • 35
  • what is `p.get(1)` doing? why do you think `contains` should do what you desire as an ouput? – Naman Oct 14 '17 at 08:41
  • 1
    p.get(1) gets the Company Name information from Scrip-Info table, converted to Upper Case & compared. My understanding of contains is that it will try to match as many words from LtoR, I guess am way off target in my understanding. – iCoder Oct 14 '17 at 08:46
  • Plese read the java doc for [`contains`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence-). You might want to implement a matching algorithm for your use case of binding the shorthand name in the full name or keep a mapping of such names in another data structure which can be looked up in your current loop. – Naman Oct 14 '17 at 08:48

1 Answers1

1

You can play around with the filtering part. Here is an example,

    List<TransactionData> filteredData =
            transactionData.stream().filter(t ->
            {
                List<ScripInfo> filteredScrip =
                        scripInfo.stream().filter(s -> {
                            String[] tranTokens =
                                    t.getName().toUpperCase().split(
                                            " |\\.");
                            String[] scripTokens =
                                    s.getName().toUpperCase().split(" ");
                            String scripSysmbol =
                                    s.getSymbol().toUpperCase();
                            if (tranTokens[0].contains(scripTokens[0])) {
                                return true;
                            } else if (scripSysmbol.contains(
                                    tranTokens[0])) {
                                return true;
                            }
                            return false;
                        }).collect(Collectors.toList());

                return filteredScrip.size() > 0 ? true : false;

            }).collect(Collectors.toList());

Classes ScripInfo and TransactionData are simple POJOs in my example,

@Data
@AllArgsConstructor
public class ScripInfo {
    private String symbol;

    private String name;
}

@Data
@AllArgsConstructor
public class TransactionData {
    private String name;

    private String letter;

    private int number;
}
Indra Basak
  • 7,124
  • 1
  • 26
  • 45