I use the following code to try MVStore off heap store:
OffHeapStore offHeap = new OffHeapStore();
MVStore s = new MVStore.Builder().fileName("c:\\temp\\h2.cache").fileStore(offHeap).open();
int count = 100;
Map<Integer, String> map1 = s.openMap("u1");
for (int i = 0; i < count; i++) {
map1.put(i, "Hello " + i);
}
s.commit();
int size1 = map1.size();
s.close();
System.out.println("=====");
MVStore s2 = new MVStore.Builder().fileStore(offHeap).open();
map1 = s2.openMap("u1");
for (int i = 0; i < size1; i++) {
System.out.println("M1>"+i+","+map1.get(i));
}
s2.close();
The code seems to work. After the code executes, the file "c:\temp\h2.cache" is not created. Why?
OffHeapStore is located in the memory of the process, but outside of Java heap. When you use custom .fileStore(something), .fileName(something) is silently ignored.
If you want to store data on the disk, you need to remove initialization of OffHeapStore, both .fileStore(offHeap) calls and add missing .fileName("c:\\temp\\h2.cache") call to the second MVStore.Builder().
Related
I would like to create more then one image with a loop. Yet, it doesn't seem to work this way. The problem is that an image can't have a number as a name. Is there a way to do this?
for(int i = 0; i < getObjects().size(); i++) {
Image i = new Image(getObjects().get(i).getImagePath()));
}
Try making an ArrayList of type Image and you can store all your images there
ArrayList<Image> imageArrayList = new ArrayList<Image>();
for(int i = 0; i < getObjects().size(); i++) {
imageArrayList.add(new Image(getObjects().get(i).getImagePath());
}
If you are just starting with java don't want this to be as confusing when returning to the code maybe use something like this
ArrayList<Image> imageArrayList = new ArrayList<>();//Creates the list that will store images
for(int i = 0; i < getObjects().size(); i++) {
Image image = new Image(getObjects().get(i).getImagePath());//Create the image you want to store
imageArrayList.add(image);//Add that image to the arraylist
}
(1) Can anyone tell me how do I declare array org[] and rev[] where I'm storing my two strings the original and reverse string?
(2) When I'm trying to store the characters of original String in org[] using charAt() they are throwing me an error.
Can anyone help me out on reversing two strings without making use of reverse() instead making use of for loop ?
String a = "12345";
char [] original= a.toCharArray();
char[] reverse = new char[a.length()];
int j =0;
for(int i=reverse.length-1; i>=0; i--) {
reverse[j] = original[i];
j++;
}
System.out.println(new String(reverse));
Here's a quick method you can use (must go inside your class):
public static void getCharacters(String input, char[] forward, char[] reverse)
{
for(int i = 0; i < input.length(); i++)
{
forward[i] = input.charAt(i);
reverse[i] = input.charAt(input.length() - 1 - i);
}
}
This assumes that forward and reverse were already allocated, and were initialized to the length of the string input. Example usage from inside another method:
String input = "Hello, world!";
char[] forward = new char[input.length()];
char[] reverse = new char[input.length()];
getCharacters(input, forward, reverse);
// forward and reverse now contain the characters from input
I have csv file with 5,00,000 records in it. Fields in csv file are as follows
No, Name, Address
Now i want to compare name and address from each record with name and address of all remaining records.
I was doing it in following way
List<String> lines = new ArrayList<>();
BufferedReader firstbufferedReader = new BufferedReader(new FileReader(newFile(pathname)));
while ((line = firstbufferedReader.readLine()) != null) {
lines.add(line);
}
firstbufferedReader.close();
for (int i = 0; i < lines.size(); i++)
{
csvReader = new CSVReader(new StringReader(lines.get(i)));
csvReader = null;
for (int j = i + 1; j < lines.size(); j++)
{
csvReader = new CSVReader(new StringReader(lines.get(j)));
csvReader = null;
application.linesToCompare(lines.get(i),lines.get(j));
}
}
linesToCompare Function will extract name and address from respective parameters and do comaprison. If i found records to be 80% matching(based on name and address) i am marking them as duplicates.
But my this approach is taking too much time to process that csv file.
I want a faster approach may be some kind of map reduce or anything.
Thanks in advance
It is taking a long time because it looks like you are reading the file a huge amount of times.
You first read the file into the lines List, then for every entry you read it again, then inside that you read it again!. Instead of doing this, read the file once into your lines array and then use that to compare the entries against each other.
Something like this might work for you:
List<String> lines = new ArrayList<>();
BufferedReader firstbufferedReader = new BufferedReader(new FileReader(newFile(pathname)));
while ((line = firstbufferedReader.readLine()) != null) {
lines.add(line);
}
firstbufferedReader.close();
for (int i = 0; i < lines.size(); i++)
{
for (int j = i + 1; j < lines.size(); j++)
{
application.linesToCompare(lines.get(i),lines.get(j));
}
}
.proto file structure
message repetedMSG
{
required string data = 1;
}
message mainMSG
{
required repetedMSG_id = 1;
repeated repetedMSG rptMSG = 2;
}
I have one mainMSG and in it too many (suppose 10) repetedMSG are present.
Now i want to delete any particular repetedMSG (suppose 5th repetedMSG )from mainMSG. For this i tried 3 ways but none of them worked.
for (int j = 0; j<mainMSG->repetedMSG_size(); j++){
repetedMSG reptMsg = mainMsg->mutable_repetedMSG(j);
if (QString::fromStdString(reptMsg->data).compare("deleteMe") == 0){
*First tried way:-* reptMsg->Clear();
*Second tried Way:-* delete reptMsg;
*Third tried way:-* reptMsg->clear_formula_name();
break;
}
}
I get run-time error when i serialize the mainMSG for writing to a file i.e. when execute this line
mainMSG.SerializeToOstream (std::fstream output("C:/A/test1", std::ios::out | std::ios::trunc | std::ios::binary)) here i get run-time error
You can use RepeatedPtrField::DeleteSubrange() for this. However, be careful about using this in a loop -- people commonly write code like this which is O(n^2):
// BAD CODE! O(n^2)!
for (int i = 0; i < message.foo_size(); i++) {
if (should_filter(message.foo(i))) {
message.mutable_foo()->DeleteSubrange(i, 1);
--i;
}
}
Instead, if you plan to remove multiple elements, do something like this:
// Move all filtered elements to the end of the list.
int keep = 0; // number to keep
for (int i = 0; i < message.foo_size(); i++) {
if (should_filter(message.foo(i))) {
// Skip.
} else {
if (keep < i) {
message.mutable_foo()->SwapElements(i, keep)
}
++keep;
}
}
// Remove the filtered elements.
message.mutable_foo()->DeleteSubrange(keep, message.foo_size() - keep);
I have an ini file with information like:
slExpsave_0 = 0.23;
slExpsave_1 = 0.40;
slExpsave_2 = -0.85;
...
I need to use them in a loop;
...
for(var i=0; i<3; i++)
{
slExpArray[i].value = slExpsave_[i];
}
But it doesn't work.
What is the reason for this?
I'm guessing this is Javascript, so you should use an array if you want to access them within a loop:
slExpsave_ = [0.23, 0.40, -0.85];
Does this help?
Let me explain my problem more clearly.
As I did it manually, It worked as I wanted:
slExpsave_0 = 0.23;
slExpsave_1 = 0.40;
slExpsave_2 = -0.85;
slExpArray[0].value = slExpsave_0; // output is 0.23;
slExpArray[1].value = slExpsave_1; // output is 0.40;
slExpArray[2].value = slExpsave_2; // output is -0.85;
when I tried to do it automatically by put in the for loop:
the first one I tried:
for(var i=0; i<3; i++)
{
slExpArray[i].value = "slExpsave_" + i;
}
// output is slExpsave_0, slExpsave_1, slExpsave_2
the second was:
for(var i=0; i<3; i++)
{
slExpArray[i].value = slExpsave_[i];
}
// doesn't work.