Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can someone help me or guide me with resources that would help me write this code in a simple way? We are internally storing some values which have a different representation across the company. I was wondering if there is a way in JAVA where I store this value ahead of time with its actual standard values which everyone knows.
For instance,
{
ab -> apple
bc -> ball
cd -> cat
}
In my code, I retrieve ab from the database. Instead of checking each time what ab represent, I can on the fly pass the standard value to some other service.
I'm looking to implement something pass apple instead of ab without the use of if and else statement. I know that I will have to store the standard value and point to the internal value which would be a one time thing of writing.
Something in the direction of an array
instead at index[0] -> 1
I say index[ab] -> apple
So something like: let's say I retrieved a from the database, in the code, I just simply call a convert.(a) which return apple instead..
I can't think of a way ... need some help
If I understood you correctly you want to retreive values from a database and transform these values into something you "hard code". This could be achieved be a "HashMap". This Map is able to store key-value-pairs.
This Map has basically this structure: HashMap<Key, Value>.
Where Key is anything you choose to identify a Value.
In your case it would look like this (considering you want to store String-String-pairs):
Map<String, String> map = new HashMap<String, String>();
map.put("a","apple");
map.put("b","ball");
map.put("c","cat");
This basically says e.g. "take an "a" and associate it with "apple". Those two values do not need to be String. They can be anything: Integer, Float, Object, MyClass,....
Now if you want to get the transformation for your a you simply do:
map.get("a");
This call will return "apple" since you associated apple with it earlier.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is how my text file looks
Cincinnati
Oxford
Chicago
New York
Las Vegas
Houston
Detroit
Miami
Denver
Boston
I want to populate an ArrayListCity randomly from the text file and then sort it with collections.sort. All of the cities from the text file should be in the array list, but in a different order each time the program is run.
Read the file sequentially - thats the easiest route.
Then randomly shuffle the collection.
Actually, another question. Could you use math.random() to look at the lines of the text file and if, lets say, the line 5 comes up then you remove it from the parameters you set for math.random()? It would pick from 0-9 and after the fifth line is used it would pick from 0-4,6-9
It is possible - but over complicates things. The best way to implement such a feature, is to have a 'pool' of numbers. (i.e. an arraylist of Integer objects), then you can use a Random number generator (between 0 and arrayList.size()) to get (and remove it from your arrayList too) one of these Integer objects. Then read that line. This approach needs several objects (Random, Arraylist, Integer, Reader).
At best, its overcomplicated for something so simple. Best thing to do, like I said, read each line and insert it into the arrayList. Then suffle.
Another take on this is, read each line, get the size of the arraylist and insert the new String, randomly within the arraylist. Heres some code:
arrayList.add(getRandomIndex(arrayList.size()),string);
public int getRandomIndex(int size){
return ((int)Math.random()*size)
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I know it's a simple question but really want to see if there's another way to do it instead of using ArrayList to hold all the values. And if that's necessary, what should be the design of the java class.
Say I would need 5 list of values read from 5 files. Previously I just used 5 ArrayList to store the 5 list of values.
public class Values{
ArrayList<String> o1 = new ArrayList<String>();
ArrayList<String> o2 = new ArrayList<String>();
ArrayList<String> o3 = new ArrayList<String>();
...
public void readFromFile(ArrayList<String> listName, String filePath){
/*read file contents into list*/
}
But my problem is, each may contain more than 2000 string values. Is this an appropriate way to do so? If so, what would be a better design of it?
I think you will be fine with using ArrayLists for such a task. I have processed a large dataset of Tweets (aka Twitter Streaming data) to the tune of 5 GB and 1.5 million individual tweets. It wasn't an issue.
You can always increase your heap size if you have problems. Do realize that unless you really need to create and store so many ArrayLists, you can always clear them after intermediate processing.
java -Xms2048M -Xmx4096M YourProgramName
I think this should give you an idea of how you should design your program. The idea here is to add, process, remove. For my case, I just parsed, manipulated a tweet, cleared and moved on.
Given that you really need to have that data in memory, there is nothing wrong with ArrayList to accomplish that. 5 files with 2000 strings of a length of 80 characters are 5*2000*80*2 bytes of character data + some overhead for the 10000 String objects + 5 ArrayList objects, in total you will use less than 1.7 MB of memory for that. Not a big deal.
You should change the declaration and use List instead of ArrayList, like this:
List<String> o1 = new ArrayList<String>();
In this way you can use for example a LinkedList instead of the ArrayList without changing to much of your code. But as long as you don't have any specific reason to use something else, go ahead and use the ArrayList, it is the simplest solution.
KISS.
Unless a different solution enhances testability, maintainability, clarity and simplicity of what you're trying to accomplish, go with what you have. Writing good, clean code is much more important at the outset than writing highly optimized, fast performing code. Clean code is code that's easy to optimize later anyway.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I know my question is so silly and we found a lot's of resources on internet, but as a beginner i hesitate between two declaration to fill a HashMap in Java:
I have a MySQL request looking like this :
select coordinates,x, y,z from log
i want to fill a Map or a HashMap( whatever) with the values of x,y and z for each coordinates point.
that's what i did :
List<Map<String, Object>> res = h.select("select coordinates,x, y,z from log");
i never used Maps in Java, so i don't know if it's the good way to get an organized map.
So now i hesitate between the declaration above and this one:
Map<String,Map<String, Object>>
I really don't know with one i have to use and how i can read the map after .
Thank You
in case you need this data structure afterward for getting (X,Y) coordinated by Z then the second choice would give you quick access.
Map = Map.get(key);
Otherwise you will have to loop throw the list.
If this is not the case a simple ArrayList should be fine.
In any case see more details here:
http://docs.oracle.com/javase/tutorial/collections/implementations/index.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What would be the best way to write a Java program that would simulate machine code? For example, I need to create a series of instructions such as add, subtract, increment, decrement, etc.
Let's say I'm writing the add instruction which accepts 3 parameters/registers (adding the values in the first 2 registers and storing the result in the 3rd). Is it as simple as writing a function such as:
int add(int x, int y) {
int result;
result = x + y;
return result; }
I'm also open to the possibility that I'm way off base here. Any input would be much appreciated.
If you just want to write Java code that will be more or less 1:1 with machine instructions I'd suggest you create variables for all of the registers and define methods for most of the instructions (similar to what you suggested). But this will not "restrict" what you can do the way real machine instructions do, since you can multiply the BX reg by the AX reg when the machine may not allow that.
Better would be to define a class that represents the machine state (ie, registers and RAM) and methods on the class for all of the instructions. Then you couldn't multiply BX times AX unless there were a MUL_BX_AX method. Many methods would not have parameters (because the registers are inside the "opaque" object), but some would have parms where the "real" instructions would accept an offset or whatever. (Eg, ADD_AX_IMMED(5).)
Added: There is the issue of branching, though, that would require some additional thought. Java doesn't have a GOTO equivalent that would fill the role very well, so initially (until you think of something better) you might have to use standard if/else logic, et al, testing "condition codes" in the machine state class.
The best way to simulate assembly would be to handle the raw bits and bytes and do the operations accordingly.
Sure you could do that, but the big thing is how to switch on the op-codes and do all the address-field calculations.
Typically, address fields can contain literal constants, global addresses, registers, offsets relative to registers, etc.
It depends if you're simulating a simple machine or a real one.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
The first column will have serial number, the second, third, and fourth will have x-coordinate, y-coordinate and velocity. I might have to retrieve any of the fields, given the serial number. Say I want to get the y-coordinate of serial number 7, or the velocity of serial number 10. One way is to have an inner class with x,y and velocity and map serial numbers to objects of the inner class. But it makes things look complicated and retrieving a particular field value seems complex. Any better solution?
Create a key object which implements hashCode() and equals() correctly. It appears that your serial numbers might be directly usable.
Create a data object with all fields you will need to store, and a getX() and setX() for each field x.
Create a Map<KeyObject,DataObject> and use that with your (key, data) pairs.
Be very careful to remove outdated objects or you will have a memory leak.
What about using Arrays or Map (Collections -> Map ) ?
With arrays: just create one array with the dimensions you need. In your case:
Array[number_of_itens_to_store][3]
// 3 => 0 = id, 1 = x, 2 =y, 3 = speed
With Maps: take a look at http://download.oracle.com/javase/6/docs/api/java/util/Map.html . You can use your ID as the K (key) and an array as the value.
You can take a look at Collections (http://download.oracle.com/javase/tutorial/collections/index.html) and even create your own collection, just following the tutorials.