Working with data from table - java

I am just wondering if I could get some advice on implementing an algorithm for creating a JFreeChart. Basically, I am extracting some data from a JTable which contains information about patients. There are age categories for all patients such as 20-26, 26-30, 31-35, 35-40, 50-55, 55-60 etc. There are about 30 of them and every patient belongs to their corresponding age category. There is a button on top of the JTable which opens a frame containing the age distribution graph. What I am thinking of doing:
Create an integer variable for every category
Loop through the age details for all patients in the JTable
Compare variables with the JTable data and increment by 1 if there is a match (lots of if statements to go in the loop)
Store the categories names and the amount of people registered under every category in a HashMap
Pass the map to the ChartFrame
I suppose this might be a relatively good way of doing this but I was wondering if somebody could possibly suggest a way of avoiding having to create a variable for every category and then having to do all those comparisons using about 30 if statements.
EDIT: I do not have the patient's exact age - only the category they belong to.

I've assumed you've got your own class AgeRange which stores a range of ages. Then what you can do is store the age ranges in a TreeMap<Integer,AgeRange>, where the key is the first number of the range and the value is the range itself.
When you need to find which age range contains a particular age, use
theMap.lowerEntry(age + 1)
to find it.
Check the TreeMap Javadoc at http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

Use the SimpleHistogramDataset class in JFreeChart. Add one SimpleHistogramBin for each of your age ranges, and then call the addObservation() method for each person's age. Once you are done, you have a working dataset - it implements the IntervalXYDataset interface so you can, for example, pass it to the createXYBarChart() method to create a histogram.

Related

how to choose or write my own java data structure allowing multi attributes search

I have a pretty large list containing many instances of one class, this class has many attributes(member variables). My problem is to find a feasible data structure to store these instances that allow searches based on multiple attributes like database search(i.e. A Student class, each student has age, date of birth, grade and GPA.find all 2nd year students whose ages are between 20 and 23). The Map seems not applicable as it only allow single key and if I create multi attribute index for searching, the big O is still not decreased. I also considered using trees like AVL tree, and I don't think it would work.
I'd be grateful if someone could give me some hints.
I think what you are looking for is an Inverted Index (using attribute name + value as keys) or possibly one Inverted Index per attribute. A search would build the intersection of all results found for each attribute.
You could do this:
Build an AVL tree with objects sorted by the most recurrent attribute (just one, e.g. "id" or "name").
Then create a search function that instead of taking a value, takes a Java lambda expression F (so your seacrh condition must be something like F(myObj) == true instead of myObj.deFaultAttribute == searchParameter)
For the example you gave, F could be something like ((myObj) -> myObj.year==2 && myObj.age>=20 && myObj.age<=23)
I hope it helps.

A two-dimensional array with different datatypes

I have a situation of creating approximately 8 tables, each with a different number of columns. Each column represents a SUBJECT / COURSE chosen by each student.
On table 1, I want to show the courses applicable for semester 1 and on table 2 show courses for semester 2 and so on.
Somewhat like this SemesterCourses<Integer,<ACourse>> with ACourse being the course object. I have one row for each course for a semester. I want to read all the courses for a given semester and load it in the ARRAY/MAP/OBSERVABLELIST ???? and then pass it on to the next function which would create columns for each COURSE in this collection.
I need these arrays for each semester in memory. Otherwise, I can read from database and create the column and forget the course info.
Is there any way that I can create this type of LIST? I tried Array<ACourse>[8][] because the number of semesters is 8 and is constant. But I get syntax errors.

how can i generate a item code value with auto increment integer plus an identification integer

i'm creating a Itemcode for my inventory system i want the number system of integer values like this using java
for example this
for group 1 the code would be 001 -
0010001,
0010002
for group 2 the code would be 002-
0020003,
0020004
for group 3 the code would be 003-
0030005,
0030006
the items are encoded individually so when i add a new entry it will detect which group it belongs to and generate it desired item code the first 3 digits will be the corresponding Value identification in which group it belongs to the the next 4 digit code will just be the increment value..and would be stored as one integer using MySQL database
You need to decide:
Are the item codes to be represented as: one integer, a pair of integers (group & item), a string ... or something else.
Is the numbering scheme per the first example or the second one. (You seem to have chosen one scheme now ...)
How you are going to populate the items and codes. Do you read the codes? Do you generate them all in one go while loading items from a file. Do you create items and item ids one at a time (e.g. interactively).
How is this information going to be "stored"? In memory only? In a flat file? In a database? (MySQL ... ?)
These decisions will largely dictate how you implement the item id "generation".
Basically, your problem here is that >>you<< need to figure out what the requirements are. Once you have done that, the set of possible solutions will reduce to a manageable size, and you can then either work it out for yourself or ask a sensible question.

What data structure to choose for an ELO rating system with 20k+ entries?

I have some Objects (currently 20 000 +) that have the following Attributes
Object
----------
String name
int rating
I want to create an ELO rating for all these Objects. This implies the following:
To adjust the ELO of 2 Objects matched against each other I need to find those Objects in the list by name.
To Display the List I need to get every Object ordered by its rating.
The whole program will be implemented in Java, but I think its independent from the programming language.
Currently I am unsure which data model to choose for this Project. A friend advised me to use a 2-4 tree to insert the Objects ordered by name so I can change the rating of each object fast.
However the Objects are printed in order of their rating rather than by name and I don't want to sort so many Objects every time I output the list.
As I am pretty new to data structures: What is the usual way to solve this problem?
Creating another tree ordered by rating?
Having a list of ratings and each rating linked to each Object currently having that rating?
Edit: ELO rating is a mapping from the set of objects to the integers. Each object only gets one rating but a rating can have multiple Objects associated with it.
Creating another tree ordered by rating? Having a list of ratings and each rating linked to each Object currently having that rating?
Well , this is one way to do so , but will take huge space also since you have 20K+ entries .
The best way i can think of now is :
Use datastructure like multimap with key=name , and value = ratings.
This way , everytime you insert a new object in multimap , it will take O(logN) time .
To find all ratings with same name use equal_range , which is also an O(logN) operation .
Hope this helps !
A HashMap with name as the key will give you O(1) performance when matching the elements, keep a TreeSet with a rating Comparator and you'll have the items in order. Although you'll need to reinsert an element if the rating changes.

ConcurrentSkipListMap sorting: Can it be done by the value's compareTo?

In a game, I'm trying to keep a list of users and have it sorted by score, so that I could query the list at any given time and return (for example) the top ten users by score. This list should be thread-safe. I envision using the userName string as a key and the value would be a User object which implements Comparable and has properties such as displayName and score. The User object would therefore have a compareTo method which would compare the score attribute to determine its position.
I'm looking at using a ConcurrentSkipListMap for this, but as best I can tell, the Map (as opposed to the Set) uses the key to sort. I'd like to have the list sorted by the score property of the User object, but still use a Map because I need to be able access any given user and modify their score attribute from a thread.
It doesn't seem that using my own Comparator for the key would solve my problem, as I doubt I'd have access to the associated value for comparison. I could use a ConcurrentSkipListSet but accessing the list to modify an individual user's score would be (I would imagine) an expensive operation (due to the need to iterate every time).
Would anyone be able to suggest how to accomplish this?
No, I don't think you can. The comparator used for ordering is the same one used for indexing. You will probably have to maintain 2 collections. One for keeping the ordering of user's scores the for referring to the users by name.
get(key) depends on the comparator (to be able to locate the key). You propose a comparator that would depend on get(key) (to access the mapped value of a key an compare based on that). That necessarily leads to infinite recursion and stack overflow (on the bright side, you are posting at the right website!!)
Michael is right, you can't have your cake and eat it too ;)
I think you have 3 choices:
Use a Map so that updates to a user's score are quick, and you pay the price when sorting to find the highest scores.
Use a SortedSet that sorts by score so that finding the highest scores is fast, but you must pay the price when updating user's scores
Maintain two data structures, so that you can have the best of 1 and 2. For example, you have your real data in a set sorted by score, but then also maintain a mapping of username to index into the set or similar. That way you always have the sorted scores, and updating a user's score is just a lookup, not a search. The price you pay for this is now you are maintaining some duplicate information in two places, and especially considering concurrent access, it can be tricky ensuring both places are always updated in synch.
I would not make assumptions about which is faster between 1 & 2. I would try them both out with your expected usage and measure to see what is worst.
If you are really only interested in the top n scores, then there is the possibility to just maintain that list separately. So have your map of username to score for everyone, but also maintain a small set of the top scores (and their users). Every time you add/update someone's score, just check the score against the top score list, and if it's bigger than the smallest one there, just add it and bump off the lower one. This is similar to suggestion 3 above, but is less overhead and perhaps easier to maintain.

Categories