how to connect and insert map keys and values into derby database? - java

I have a java program and it produces the output as follows :
termname :docname : termcount
Forexample termname is hello and docname is :2 and termcount is :4
hello:doc1:4
.....
......
I stored all the values in a map. here is the following program
public class tuple {
public static void main(String[]args) throws FileNotFoundException, UnsupportedEncodingException, SQLException, ClassNotFoundException, Exception{
File file2 = new File("D:\\logs\\tuple.txt");
PrintWriter tupled = new PrintWriter(file2, "UTF-8");
List<Map<String, Integer>> list = new ArrayList<>();
Map<String, Integer>map= new HashMap<>();;
String word;
//Iterate over documents
for (int i = 1; i <= 2; i++) {
//map = new HashMap<>();
Scanner tdsc = new Scanner(new File("D:\\logs\\AfterStem" + i + ".txt"));
//Iterate over words
while (tdsc.hasNext()) {
word = tdsc.next();
final Integer freq = map.get(word);
if (freq == null) {
map.put(word, 1);
} else {
map.put(word, map.get(word) + 1);
}
}
list.add(map);
}
// tupled.println(list);
//tupled.close();
//Print result
int documentNumber = 0;
for (Map<String, Integer> document : list) {
for (Map.Entry<String, Integer> entry : document.entrySet()) {
documentNumber++;
//System.out.println(entry.getKey() + ":doc"+documentNumber+":" + entry.getValue());
tupled.print(entry.getKey());
tupled.print(":doc:");
tupled.print(Integer.toString(documentNumber));
tupled.print(",");
tupled.println(entry.getValue());
}
//documentNumber++;
}
tupled.close();
Now I want to store this values into derby database of neatbeans.
How I would be able to do that ?

Related

How do I write to one file a K from one Map, and a V from another?

I need to write to csv all the keys from one map in one column, and all the values from a different map in the next column.
I can do either column individually with this code but when I combine, how do I explain this(?), if I have 10 keys and 10 values the keys will repeat 10 of each key.
What do I need to do to my loops?
private static void generateCourseCounts() throws IOException {
ArrayList<StudentCourse> lsc = loadStudentCourses();
Map<Integer, Integer> countStudents = new TreeMap<Integer, Integer>();
for (StudentCourse sc : lsc) {
Integer freq = countStudents.get(sc.getCourseId());
countStudents.put(sc.getCourseId(), (freq == null) ? 1 : freq + 1);
}
ArrayList<Course> lc = loadCourses();
Map<String, String> courses = new LinkedHashMap<String, String>();
for (Course c : lc) {
String freq = courses.get(c.getCourseName());
courses.put(c.getCourseName(), freq);
}
FileWriter writer = new FileWriter("CourseCounts.csv");
PrintWriter printWriter = new PrintWriter(writer);
printWriter.println("Course Name\t# Students");
for (Entry<String, String> courseKey : courses.entrySet())
for (Entry<Integer, Integer> numberKey : countStudents.entrySet()) {
printWriter.println(courseKey.getKey() + "\t" + numberKey.getValue());
}
printWriter.close();
writer.close();
}
So, as per comments below, I edited to this:
for (String courseKey : courses.keySet()) {
Integer count = countStudents.get(courseKey) ;
printWriter.println(courseKey + "\t" + count);
}
However, this writes an empty file.
Try this. It does presume that the number of map entries in each map is the same.
Otherwise, you will either get an index out of bounds exception or you won't print all the values.
int i = 0;
Integer[] counts = countStudents.values().stream().toArray(Integer[]::new);
for (String courseKey : courses.keySet()) {
printWriter.println(courseKey + "\t" + counts[i++]);
}
printWriter.close();
writer.close();
You don't need embedded cycles. You can just iterate by keys from 1st map and get values from 2nd:
for (String courseKey: courses.keySet())
String count = countStudents.get(courseKey);
// ... output courseKey and count to file
}

Consolidate result of different dates by scanning the data from file and output data of particular date

I have recently given interview with major MNC and faced a coding question. It was an inventory based question. Got a file which has inputs as:
Date, name of fruit, no of fruits sold, total fruits.
**2018-06-01,apple,15,25 ;
2018-06-02,apple,13,25 ;
2018-06-03,apple,20,25 ;
2018-06-01,mango,12,25 ;
2018-06-02,mango,19,25 ;
2018-06-03,mango,20,25 ;
2018-06-01,orange,15,25 ;
2018-06-02,mango,16,25 ;
2018-06-02,orange,14,25 ;
2018-06-03,orange,24,25 ;
2018-06-02,apple,18,25**
I have to iterate file and in the end print out the results how many fruits sold on 2018-06-02. Below is the code which i have writen
enter code here
public class Inventory {
public static void main(String[] args) throws IOException {
FileInputStream geek = new FileInputStream("/Users/preet/Desktop/test2.txt");
InputStreamReader reader = new InputStreamReader(geek, StandardCharsets.UTF_8);
BufferedReader in = new BufferedReader(reader);
List<String> lines = new ArrayList<String>();
Map<String, Map<String, List<Integer>>> map = new HashMap<String, Map<String, List<Integer>>>();
Map<String, List<Integer>> map2 = new HashMap<String, List<Integer>>();
List<Integer> apple = new ArrayList<Integer>();
List<Integer> mango = new ArrayList<Integer>();
List<Integer> orange = new ArrayList<Integer>();
String result = "";
String line = null;
String[] input = new String[10];
int i = 0;
while ((line = in.readLine()) != null) {
input[i] = line;
System.out.println("value at " + i + ":" + input[i]);
i++;
}
for (String a : input) {
String b[] = a.split(",");
if (b[1].equalsIgnoreCase("apple")) {
apple.add(Integer.parseInt(b[2]));
map2.put(b[1], apple);
map.put(b[0], map2);
} else if (b[1].equalsIgnoreCase("orange")) {
orange.add(Integer.parseInt(b[2]));
map2.put(b[1], orange);
map.put(b[0], map2);
} else if (b[1].equalsIgnoreCase("mango")) {
mango.add(Integer.parseInt(b[2]));
map2.put(b[1], mango);
map.put(b[0], map2);
}
if (map.containsKey(b[0])) {
Object value1 = map.get(b[0]);
map.put(b[0], map2);
} else
map.put(b[0], map2);
}
System.out.println("Fetching Keys and corresponding [Multiple] Values n");
List<String> results = new ArrayList<String>();
for (Map.Entry<String, Map<String, List<Integer>>> entry : map.entrySet()) {
String orange1 = null;
String apple1 = null;
String mango1 = null;
int a1 = 0;
int a2 = 0;
int a3 = 0;
Map<String, List<Integer>> values = entry.getValue();
for (Map.Entry<String, List<Integer>> entry1 : map2.entrySet()) {
String key = entry1.getKey();
if (key.equalsIgnoreCase("apple")) {
List l1 = entry1.getValue();
for (int j = 0; j < l1.size(); j++) {
a1 = (int) l1.get(j);
}
apple1 = entry.getKey() + " " + a1 + " apples";
results.add(apple1);
} else if (key.equalsIgnoreCase("mango")) {
List l1 = entry1.getValue();
for (int j = 0; j < l1.size(); j++) {
a2 = (int) l1.get(j);
}
mango1 = entry.getKey() + " " + a2 + " mangoes";
results.add(mango1);
} else if (key.equalsIgnoreCase("orange")) {
List l1 = entry1.getValue();
for (int j = 0; j < l1.size(); j++) {
a3 = (int) l1.get(j);
}
orange1 = entry.getKey() + " " + a3 + " oranges";
results.add(orange1);
}
}
System.out.println("Values = " + (values));
}
System.out.println("****" + results);
}
}
For an interview I would suggest the following as a first shot: iterate over the file. Split each line at commas using String.split(). If element 0 of the resulting array is identical to 2018-06-02, parse the integer in element 2 (the no of fruits sold) using Integer.parseInt() and add to the total count.
An obvious thing missing from such a first shot is input validation. May first check that there is at least one line in the file. Check that each line ends with a semicolon. Remove it and any space before it before splitting. Check that the length of the array is 4. Use the one-arg LocalDate.parse() for parsing the date on each line and Integer.parseInt() to parse both the number sold and the total number. Validate that the number sold is less than or equal to the total number. May also check that the name is that of a known fruit.
Another thing is data modelling. Design a fruit class with a name field and a fruit sales class with a date, a total number and a number sold, for example.

Parse String to get grouped parameters

My String looks like this
http://localhost:8080/HospitalServer/files/file?id=34&firstname=alex&lastname=ozouf&age=33&firstname=kevin&lastname=gerfild&age=27
I use this code to parse the parameters
final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
final String[] pairs = query.split("&");
for (String pair : pairs) {
final int idx = pair.indexOf("=");
final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
if (!query_pairs.containsKey(key)) {
query_pairs.put(key, new LinkedList<String>());
}
final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
query_pairs.get(key).add(value);
}
System.out.println(query_pairs);
The result is
{id=[34], firstname=[alex, kevin], lastname=[ozouf, gerfild], age=[33, 27]}
The result is not too bad but I want to group the parameters by person.
{id=[34], 1=[alex,ozouf,33 ], 2=[kevin, gerfild,27]}
I can create it from the previous result but I have the feeling that the job is done twice. What do you think I shall do ?
Here's how you can do it without using any library:
import java.util.Map;
import java.util.HashMap;
public class MyUrlParser {
private static final String SEPARATOR = ",";
public static void main(String[] args) {
final String URL = "http://localhost:8080/HospitalServer/files/file?id=34&firstname=alex&lastname=ozouf&age=33&firstname=kevin&lastname=gerfild&age=27";
MyUrlParser mup = new MyUrlParser();
try {
Map<String, String> parsed = mup.parse(URL);
System.out.println(parsed);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public Map<String, String> parse(String url) throws Exception {
Map<String, String> retMap = new HashMap<>();
int queryStringPos = url.indexOf("?");
if (-1 == queryStringPos) {
throw new Exception("Invalid URL");
}
String queryString = url.substring(queryStringPos + 1);
String[] parameters = queryString.split("&");
if (parameters.length > 0) {
retMap.put("id", parameters[0]);
int personCounter = 0;
for (int minSize = 4; minSize <= parameters.length; minSize += 3) {
StringBuilder person = new StringBuilder();
person.append(parameters[minSize-3]);
person.append(SEPARATOR);
person.append(parameters[minSize-2]);
person.append(SEPARATOR);
person.append(parameters[minSize-1]);
personCounter++;
retMap.put("person" + personCounter, person.toString());
}
}
return retMap;
}
}

HashMaps, HashSets, and FileReader and FileWriter?

This code is to find the TF-IDF of words in 40 text files in a folder called docs, whenever I use this program I keep on getting null pointer exceptions. I believe it is coming from the computeTermFrequencies method. I want it to print the top 5 TF-IDF words from each file.
Any help would be greatly appreciated! Thank you!
import java.util.*;
import java.io.*;
public class KeywordExtractor {
public static void main(String[] args) {
String dir = args[0]; // name of directory with input files
HashMap<String, Integer> dfs;
dfs = readDocumentFrequencies("freqs.txt");
for(int i = 1; i <= 40; i++){
String name = dir + "/" + i + ".txt";
HashMap<String,Integer> tfs = computeTermFrequencies(name);
HashMap<String,Double> tfidf = computeTFIDF(tfs,dfs,40);
System.out.println(i + ".txt");
printTopKeywords(tfidf,5);
System.out.println();
}
}
//method to that takes string as input and returns hashmap with amount of times
//each word appears in the file
public static HashMap<String, Integer> computeTermFrequencies(String filename) {
HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line = "";
line = normalize(line);
//for(String line = br.readLine(); line != null; line = br.readLine()){
while((line=br.readLine())!=null){
String[] words = line.split(" ");
for(int i = 0; i < words.length; i++){
String word = words[i];
if(hm2.containsKey(word)){
int x = hm2.get(word);
x++;
hm2.put(word,x);
}else{
hm2.put(word,1);
}
} //end for
}//end for
}catch(IOException e){
//error
}
return hm2;
}
//method to read frequency file created in another class, it returns a hashMap
public static HashMap<String, Integer> readDocumentFrequencies(String filename){
HashMap<String, Integer> hm = new HashMap<String, Integer>();
//try block
try{
//read file
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
//for loop to loop through and take words and put in hashmap
for(String line = br.readLine(); line != null; line = br.readLine()){
String[] a = line.split(" ");
String word = a[0];
int number = Integer.parseInt(a[1]);
//put word in hashmap with the frequency of the word
hm.put(word,number);
if(hm.get(word)==null){
System.out.println("sads");
}
}//end for
}
catch(IOException e){
//error
}
return hm;
}
public static HashMap<String, Double> computeTFIDF(HashMap<String, Integer> tfs, HashMap<String, Integer> dfs,
double nDocs) {
HashMap<String, Double> hm3 = new HashMap<String, Double>();
for(String key:tfs.keySet()){
/*if(dfs.get(key)==null){
System.out.println(key);
}*/
double idf = Math.log(nDocs/dfs.get(key));
double tf = tfs.get(key);
hm3.put(key,tf*idf);
}
return hm3;
}
/**
* This method prints the top K keywords by TF-IDF in descending order.
*/
public static void printTopKeywords(HashMap<String, Double> tfidfs, int k) {
ValueComparator vc = new ValueComparator(tfidfs);
TreeMap<String, Double> sortedMap = new TreeMap<String, Double>(vc);
sortedMap.putAll(tfidfs);
int i = 0;
for(Map.Entry<String, Double> entry: sortedMap.entrySet()){
String key = entry.getKey();
Double value = entry.getValue();
System.out.println(key + " " + value);
i++;
if (i >= k) {
break;
}
}
}
public static String normalize(String word) {
return word.replaceAll("[^a-zA-Z ']", "").toLowerCase();
}
}
/*
* This class makes printTopKeywords work. Do not modify.
*/
class ValueComparator implements Comparator<String> {
Map<String, Double> map;
public ValueComparator(Map<String, Double> base) {
this.map = base;
}
public int compare(String a, String b) {
if (map.get(a) >= map.get(b)) {
return -1;
} else {
return 1;
} // returning 0 would merge keys
}
}

outOfMemory in for loop java

I am iterating 500,000 items using for loop , after 300, 000 item i am getting Out of memory , I have also tried to split loop in 100,000 still, but that also didn't work. When I increased memory in runConfig-xms10g, its working fine. Can anyone tell me how to split and free memory or any other way to iterate large number of recods.
protected Map<Long, Map<String, String>> getExistingItems()
{
Map<Long, Map<String, String>> items = new HashMap<Long, Map<String, String>>();
for (Item item : itemMaster.getItems()) {
if (item.getExpirationDate() == null && !items.containsValue(item.getItemId())) {
Map<String, String> item_hash = new HashMap<String, String>();
if (item.getEffectiveDate() != null)
item_hash.put("effectiveDate", sdf.format(item.getEffectiveDate().getTime()));
for (ItemMasterAttributeValue attrVal : item.getItemMasterAttributeValues()) {
item_hash.put(attrVal.getId().getItemMasterAttribute().getCode(), attrVal.getValue());
}
items.put(item.getItemId(), item_hash);
}
}
after splitting below is code:
protected Map<Long, Map<String, String>> getExistingItems()
{
Map<Long, Map<String, String>> items = new HashMap<Long, Map<String, String>>();
java.util.Date date = new java.util.Date();
System.out.println("before getItems : " + new Timestamp(date.getTime()));
Collection<Item> allItems = itemMaster.getItems();
System.out.println("after getItems : " + new Timestamp(date.getTime()));
int split = (allItems.size() / 100000);
Map<Integer, Collection<Item>> splitMap = new HashMap<Integer, Collection<Item>>();
Collection<Item> tempCollection = new ArrayList<Item>();
int splitKey = 1;
int key = 1;
for(Item item : allItems)
{
tempCollection.add(item);
if(splitKey / 100000 >= key && splitKey % 100000 == 0)
{
splitMap.put(key, tempCollection);
tempCollection = new ArrayList<Item>();
key++;
}
splitKey++;
}
splitMap.put(key, tempCollection);
System.out.println("map size " + splitMap.size());
for(int i = 1; i <= split + 1; i++)
{
System.out.println("i is :" + i + " " + Calendar.getInstance().getTime());
for(Item item : splitMap.get(i))
{
if(!items.containsKey(item.getItemId()))
{
Map<String, String> item_hash = new HashMap<String, String>();
if (item.getEffectiveDate() != null)
item_hash.put("effectiveDate", sdf.format(item.getEffectiveDate().getTime()));
for (ItemMasterAttributeValue attrVal : item.getItemMasterAttributeValues()) {
item_hash.put(attrVal.getId().getItemMasterAttribute().getCode(), attrVal.getValue());
}
items.put(item.getItemId(), item_hash);
}
}
System.gc();
}

Categories