Oracle Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
while ((line = br.readLine()) != null) {
Matcher m = Pattern.compile("(/*[\\w]+)").matcher(line);
if (line.startsWith("[") && m.find()) {
// read tags
if (line.contains("/")) {
// end tag
if (line.contains(list.get(list.size() - 1))) {
list.remove(list.size() - 1);
} else {
// Got an element with /, but it is not current
break;
}
} else {
//start tag
list.add(m.group(0));
}
} else {
// split data
String[] keyVal = line.split("=");
System.out.println(list.toString().replace("[", "").replace("]", "").replace(", ", ".") + "." + keyVal[0] + " Output " + keyVal[1]);
}
}
Were you required to construct an Employee java object here which I think needs reflection knowledge. If that is not the case I would put everything in a map starting with the main map called employee. Then start putting everything as key value until I see an input like [address] for which I will create another map and save this as a nested map inside the main map and so on.
On retrieval, if I get employee.name - I will turn it into employee.get("name") and check if it instance of String, if it is - just output it, If not - it will be instance of Map and I can in that case continue until I find the String I am looking for.
I agree we can take help from the Composite pattern.
String fileName = "configuration.conf";
Pattern KEY_MATCH = Pattern.compile("\\[.*\\]");
BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
String line;
String prefix = "";
while ((line = br.readLine()) != null) {
if(KEY_MATCH.matcher(line).matches()) {
String newPrefix = line.replaceAll("(\\[)(.+?)(\\])", "$2");
if(prefix.length() == 0 )
prefix = newPrefix;
else
prefix += "." + newPrefix;
} else {
String[] keyPair = line.split("=");
System.out.println(prefix + "." + keyPair[0] + " Output " + keyPair[1]);
}
}
1. var prefix = "";
2. var readline <-- read new line from file and repeat from step 3 to ....
3. if readline contain pattern [] then
3.1 var readline= readline without [ and ]
3.2 var prefix = prefix+(prefix.size==0 ? "" : ".")+readline;
4 else if readline contain pattern [/] then set prefix = substr(prefix,0,last index of "." - 1)
5 else
5.1 str[] = split readline on "="
5.2 var key = prefix + "." + str[0]
5.3 var value = str[1]
5.4 stroe key value pair into hashmap so that value can be retrived from hashmap on given key like employee.name
public class Node
{ private HashMap<String, Node> Child = new HashMap<String, Node>(4);
private HashMap<String, String> Values = new HashMap<String, String>(4);
private String name;
private Node parentNode;
public String getName()
{ return name;}
public void setName(String name)
{name = name;}
public Node getParentNode()
{return parentNode;}
public void setParentNode(Node parentNode)
{ this.parentNode = parentNode;}
public HashMap<String, Node> getChild()
{return Child;}
public void setChild(HashMap<String, Node> child)
{ Child = child;}
public HashMap<String, String> getValues()
{return Values;}
public void setValues(HashMap<String, String> values)
{Values = values; }
}
public static void main(String[] args)
{
Node root = new Node();
Node currentNode = root;
try
{
File f = new File("C:\\Users\\Documents\\oracle.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
while ((readLine = b.readLine()) != null)
{
if (Pattern.matches("\\[[^\\]]*\\]", readLine))
{
System.out.println(readLine);
if (readLine.contains("/"))
{
currentNode = currentNode.getParentNode();
}
else
{
Node temp = new Node();
temp.setName(readLine);
temp.setParentNode(currentNode);
currentNode.getChild().put(readLine, temp);
currentNode = temp;
}
}
else
{
String[] out = readLine.split("=");
currentNode.getValues().put(out[0], out[1]);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
After researching the above problem can be solved via Composite Design Pattern Easily..
- rameshelworthy July 08, 2015