Java Program to check the string word contains with map keys

Java Program to check the string word contains with map keys:

 

Sometimes we need to check the each and every word of a particular string is contains with a map or list or set values for some of our business needs. Below program will help you to achieve the similar functionality.

Input String is having some words, if the same words matches with the map keys then we are taking and keeping in separate arraylist. This list will have only the contains string words.

Program:

package in.javadomain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class WordSplit {

Map<String, ArrayList<String>> mainMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> cateForm = new ArrayList<String>();

public static void main(String[] args) {
String str = "Upto 60% + Min 35% Cashback Lavie Bags Mobiles";
Map<String, String> mapgMap = new HashMap<String, String>();
mapgMap.put("Mobiles", "Mobiles & Tablets");
mapgMap.put("Lavie", "Lavie coupons");
mapgMap.put("Bags", "Walelts & Bags");
mapgMap.put("Laptops", "Desktops & Laptops");
WordSplit ws = new WordSplit();
System.out.println(ws.cateList(mapgMap, str));
}

private ArrayList<String> cateList(Map<String, String> mapgMap, String str) {
ArrayList<String> cateLst = new ArrayList<String>();
for (Entry<String, String> mapingStr : mapgMap.entrySet()) {
if (str.contains(mapingStr.getKey().toString())) {
cateLst.add(mapingStr.getKey());
}
}
return cateLst;
}
}

Output:

[Mobiles, Lavie, Bags]

 

588 total views, 2 views today

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.