To save time on calculations, I am making a program that will use formula to calculate a value based on the data that the user inputs. The program will prompt the user for five double values: A, B, and C, D, and E. It will then multiply A by B and then find the corresponding value on a conversion table. It will do the same for C and D and plug in the corresponding values along with E in a formula to give the user the answer. My question is: How would I include the table of values I mentioned above into my program so that I can easily find the corresponding values? I'm thinking of hardcoding these values into hashmaps but that would take quite awhile. Is there a file format that stores similar types of data that would be optimal to the situation?
Store the values in CSV. Load the values into an custom object/class with a field for each column. Start by looping over the entire set of objects to find the correct value/range each time. If that does not perform well optimize by doing things like having multiple lists of references to the objects where each list is sorted by a different column-- use those sorted lists to quickly find the correct object.
I say "range" here, because I am assuming you are sometimes looking for doubles. If the result of your calculation tells you to look for 1.999999 you may actually have to look for that +/- some tolerance. For this same reason you wouldn't want to use doubles as the keys for a map.
Related
I'm completely new to programming and to java in particular and I am trying to determine which data structure to use for a specific situation. Since I'm not familiar with Data Structures in general, I have no idea what structure does what and what the limitations are with each.
So I have a CSV file with a bunch of items on it, lets say Characters and matching Numbers. So my list looks like this:
A,1,B,2,B,3,C,4,D,5,E,6,E,7,E,8,E,9,F,10......etc.
I need to be able to read this in, and then:
1)display just the letters or just the numbers sorted alphabetically or numerically
2)search to see if an element is contained in either list.
3)search to see if an element pair (for example A - 1 or B-10) is contained in the matching list.
Think of it as an excel spreadsheet with two columns. I need to be able to sort by either column while maintaining the relationship and I need to be able to do an IF column A = some variable AND the corresponding column B contains some other variable, then do such and such.
I need to also be able to insert a pair into the original list at any location. So insert A into list 1 and insert 10 into list 2 but make sure they retain the relationship A-10.
I hope this makes sense and thank you for any help! I am working on purchasing a Data Structures in Java book to work through and trying to sign up for the class at our local college but its only offered every spring...
You could use two sorted Maps such as TreeMap.
One would map Characters to numbers (Map<Character,Number> or something similar). The other would perform the reverse mapping (Map<Number, Character>)
Let's look at your requirements:
1)display just the letters or just the numbers sorted alphabetically
or numerically
Just iterate over one of the maps. The iteration will be ordered.
2)search to see if an element is contained in either list.
Just check the corresponding map. Looking for a number? Check the Map whose keys are numbers.
3)search to see if an element pair (for example A - 1 or B-10) is
contained in the matching list.
Just get() the value for A from the Character map, and check whether that value is 10. If so, then A-10 exists. If there's no value, or the value is not 10, then A-10 doesn't exist.
When adding or removing elements you'd need to take care to modify both maps to keep them in sync.
I have an assignment on Data structure, the prof wants us to use different kinds of DS in the project, but I don't know what he means with (A three-dimensional data structure that holds items in a positional relationship to each other. Each cell in the data structure can hold multiple items.)
I tried Arraylists of objects, queues with objects!
any idea what kind of DS I can try to save my time?
thanks
If you are allowed to use Guava, then I would consider a Multimap of MyObj indexed by XyzCoord, where XyzCoord is a custom object to hold three positional numbers, and MyObj is the custom object you wish to store one or more of at various coordinates.
Avoiding Guava, you can use a standard Map of List<MyObj>. It could also be indexed by List<Integer> which are of length 3.
The fact is that there are many, many ways to do this. Your question may be a bit too broad as a result. Have a look at the collection classes some more and try to ask specific questions about each one if you don't know how they are used.
The simplest spatial data structure is a 3D array. In java you can create one with as follows:
Object[][][] my3DArray = new Object[10][10][10];
Here you can store 10*10*10=1000 Object in spatial relation to each other. Unfortunately, there are only 10 possible coordinates in each dimension.
If you want something more efficient, look for quadtrees/octrees, kd-trees (as mentioned by #BeyelerStudios in the comments), R-Trees, LSH (locality sensitive hashing) or even space-filling curves (Z-curve, Hilbert curve, ...). These are just the main families, there are many versions of DSs of each type.
EDIT to answer comment.
Except the 3D array approach, all solutions above are quite space efficient. The most space efficient one may be the PH-Tree (a type of quadtree, developed by myself) in some cases it may require less memory than a plain array of coordinates. Unfortunately it is comparatively complex to understand and implement.
If you want to use a 1D sorting scheme, such as array or list, try using a space filling curve. The Z-curve may be easiest.
Using spacefilling curves you can calculate a 'key' (z-key or Morton-number) for each point in space and then sort these in the array or list. In such an ordered list/array, direct neighbors are also likely (but not guaranteed) to be close in 3D space. Inversely, points that are close in 3D space tend (but not guaranteed) to be close in the list/array.
For integer coordinates, the z-key (also called MortonNumber) can be calculated by interleaving the bits of the coordinates. You can also do this for floating points values, but you need to be careful with negative values, otherwise you may get a rift between positive and negative values.
I need a structure to translate different enumerators (Integers) from one API to another one. The APIs are GL, DX9 and DX10.
The integers are not contiguous, that is we don't have all the values from 0 to 232
Example:
Given [GL, DX9, DX10]
one entry could be:
[COMPRESSED_RGB_S3TC_DXT1_EXT, D3DFMT_DXT1, DXGI_FORMAT_BC1_UNORM]
That is:
[33776, 827611204, 71]
I should be able to get every API enum from any other one. This means, for example, I have COMPRESSED_RGB_S3TC_DXT1_EXT and I want the DX10 equivalent or I have D3DFMT_DXT1 and I want the GL equivalent and so on..
I saw some option, like attaching different subkeys here or concatenating all of them in Strings like here, but they don't look very practical/elegant to me, is there a better option?
I have a source of strings (let us say, a text file) and many strings repeat multiple times. I need to get the top X most common strings in the order of decreasing number of occurrences.
The idea that came to mind first was to create a sortable Bag (something like org.apache.commons.collections.bag.TreeBag) and supply a comparator that will sort the entries in the order I need. However, I cannot figure out what is the type of objects I need to compare. It should be some kind of an internal map that combines my object (String) and the number of occurrences, generated internally by TreeBag. Is this possible?
Or would I be better off by simply using a hashmap and sort it by value as described in, for example, Java sort HashMap by value
Why don't you put the strings in a map. Map of string to number of times they appear in text.
In step 2, traverse the items in the map and keep on adding them to a minimum heap of size X. Always extract min first if the heap is full before inserting.
Takes nlogx time.
Otherwise after step 1 sort the items by number of occurrences and take first x items. A tree map would come in helpful here :) (I'd add a link to the javadocs, but I'm in a tablet )
Takes nlogn time.
With Guava's TreeMultiset, just use Multisets.copyHighestCountFirst.
Scenario: You have been supplied with an ascii text file containing one day’s worth of catch records.
Each line in the file contains one "colon separated" catch record with three fields:
CONTESTANTS_NAME:FISH_TYPE:FISH_WEIGHT
for example
PETER:TUNNY:13.3
which indicates that a competitor called PETER caught a TUNNY weighing 13.3 kg. Note
that PETER may have caught more than one fish on the day.
How would you solve this problem using java's built-in classes Tokenizer and HashMap?
Your design should provide the following analysis:
The total weight of each type of fish caught on the day.
The total weight of fish caught by each competitor.
The top three competitors ranked by total catch weight.
Reason I'm posting this is that at first glance I sort of panicked knowing that any map contains just a key-value pair and had no idea how to solve this since it has three fields. What I did is have two HashMaps, first one had keys with CONTESTANT-NAME and second one keys were FISH_NAME and was able to provide the required analysis: this required a number of loops and I'm not sure if that's a good way of programming. If somebody has a better approach, please let me know. I just need the logic.
You may want to check out table class, like Guava Table (think of it as of 2-dimensional map). Then you may use CONTESTANT_NAME as a first key, FISH_NAME as a second, and a weight as a stored value.
Guava Table even pretends to do well with sparse tables, so I strongly suggest you to give it a try.
you can do a get/update/put combo on the hashmap
Double contestantTotal = contestantMap.get(contestant);
if(contestantTotal ==null)contestantTotal = Double.getValue(0);//if it wasn't already in the map the returned value will be null
contestantTotal += weight;
contestantMap.put(contestant,contestantTotal );//put overwrites the previous values
Double fishTypeTotal = fishTypeMap.get(fishType);
if(fishTypeTotal ==null)fishTypeTotal = Double.getValue(0);
fishTypeTotal += weight;
fishTypeMap.put(fishType,fishTypeTotal);
this requires just 3 loops one the input loop and 2 output loops