A custom data structure in java - java

I have a Tree structure on my swing UI. Each object in the Tree represents a element in the network. The elements in the network have alarms raised on it. Alarms (one or more) are represented in the UI with a bell image besides the network element. I use a Cache (TreeMap) to store the network elements with the name of the network element as the KEY and Alarms as Value. However, traversing the TreeMap is pretty in-efficient. The names of the network element are Strings. I would like to store the elements in a parent-child relation (like the real UI) and the STRING names fail to do that.
I would like to create a custom data-structure which would emulate my UI hierarchy. I would think that customising linked list would do the job for me. Is there any tried and tested data-structure which I can use? Any other opinions highly appreciated.

Use domain objects for your data structure ("model"), with classes like Site, Host, Application, NetworkLink etc. Then you can have methods like clearAlarms() and properties like getState() and getName().

Related

Which Java node-based data structure to use?

I have to create a star shape (like a star topology in networking) with nodes in java for homework and I'm not sure which data structure to use. There should be a central node with periheral nodes pointing to it that are not linked directly to each other but are all linked through the central node. I am to achieve this using three classes apart from the class with the main method. I'm thinking a linked list or a stack or queue would not accomplish this since they all have a particular order which I don't know how to manipulate (but they're all I've been taught about thus far). Any suggestions or links or comments on where I could begin would be very appreciated!
I think in your case you need just to use Graph data structure, or to be more specific, if you are sure that you just need one node to be connected to all other nodes, you can use Tree.
Finally I think you need to look how to implement those data structures in JAVA, so look at this:
https://www.geeksforgeeks.org/graph-and-its-representations/
https://www.javatpoint.com/tree

What data structure should I use if I want to search an object in different ways according to different attributes?

I'm programming in java, but that's just a detail.
I have this object person class with attributes like: name, age, weight... And I need to have people stored in my application and being able to search them. Now, I can search them by name, age, weight... all that person's attributes. What's the best data structure/implementation that allows me to do this efficiently?
K-D tree is a good choice for that. It partitions multi-dimensional data (any object with multiple attributes), and enables binary search tree like O(logN) search complexity. However, this will require few modifications to main variant.
If you don't know about it yet, go read about it first. So now you know, K-D tree doesn't exactly allow that "Given name="John Doe",find the guy" kind of queries. Instead, what it allows is "given this entire John Doe guy, find who is *closest* to him".
At every level of the tree, it chooses left or right sub-tree based on corresponding dimension of that level. But for 1st kind of query, your data for all dimensions except one is null. So, to search, you create an input object anyway with special dummy data for all but that one dimension. In your search function, when you encounter those special data, you carry on search on both sub-trees. Instead of closeness, unlike K-D tree, you can check for exact match.
You are unlikely to see the effect of this data structure if you are dealing with small amount of data. Interestingly though, when you search against more than 1 attribute, like "given age=20 and name = "John", find the guy(s)", the search will be lot faster.
What's your end-goal? If you just want to see trends between different people ad-hoc, I'd recommend just using R or Python PANDAS. That way you can quickly look up, compare, and visualize groups/individuals based on different attributes.
If you want to make an application in Java with multiple searchable options, and you don't care too much about space, I'd use multiple hash tables, with each hash corresponding to a different attribute. Have the values in an array be pointers pointing to a person. https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
K is your attribute (age, sex, etc.) and V are pointers to people.

Finding object by index, HashMaps or looping through an ArrayList? Which is fastest? (Java) [duplicate]

I've building a tree pagination in JSF1.2 and Richfaces 3.3.2, because I have a lot of tree nodes (something like 80k), and it's slow..
So, as first attempt, I create a HashMap with the page and the list of nodes of the page.
But, the performance isn't good enough...
So I was wondering if is something faster than a HashMap, maybe a List of Lists or something.
Someone have some experience with this? What can I do?
Thanks in advance.
EDIT.
The big problem is that I have to validate permissions of users in the childnodes of the tree. I knew that this is the big problem: this validation is slow, because I have to go inside the nodes, I don't have a good way to know if the user have permission in a 10th level node without iterate all of them. Plus to this, the same three has used in more places...
The basic reason for why I was doing this pagination, is that the client side will be much slow, because of the structure generated by richfaces, a lot of tr's and td's, the browser just going crazy with this.
So, unfortunatelly, I have to load all the nodes, and paginate just client side, and I need to know what of them is faster to iterate...
Sorry my bad english.
A hash map is the fastest data structure if you want to get all nodes for a page. The list of nodes can be fetched in constant time (O(1)) while with lists the time is O(n) (n=number of pages, faster on sorted lists but never getting near O(1))
What operations on your datastructure are too slow. That's what you have to analyse before you start optimization.
It's probably more due to the fact that JSF is a performance pig than a data structure choice. The one attempt I've seen to create a JSF app could be timed with a sundial.
You're making a mistake by guessing about solutions without more knowledge about the root cause. I'd recommend that you profile your app to see where the time is being spent.
The data structure to use always depends on how you need to store the data and how you need to access it. HashMap<K, V> is supposed to have constant time complexity in accessing the value, provided the key. When you call get(key), the hashCode() for key is computed and it's used to retrieve the related value. Unless you've got different keys that have the same hashcode (in which case you may have been doing something wrong, as while is not mandatory different objects should have different hash codes, at least in the majority of cases), this is usually fast.
Searching an element in a plain list requires scanning of the list, which will (almost) always be slower than computing an hashcode.
If you need to associate values with keys, a Map is the way. And HashMap should be fast enough.
I don't know too much about JSF, but I think - if the data structure and access pattern is the one that a Map is designed for - the problem is not the HashMap itself.
I would solve this with a javascript/ajax calls method that fetches childnodes.

Data structure for continuous additions and cheap deletions

I am reading this blog post about making animations with Gnuplot and Cairo -terminal which algo's plan is simply
to save png-images to working directory, and
to save latest the video to working directory.
I would like to have something more such that the user can also browse the images real time when the images are being converted:
Data-parallelism model - data structure regularly arranged in an array
to give the user some list in some interface which the user can browse by arrow buttons
in this interface, new images are being added to the end of the list
the user can also remove bad images from the stream in real time
which may work well in Data parallelism model of Parallel programming i.e. a data set regularly structured in an array.
The operations (additions, deletions) can operate on this data, but independently on distinct processes.
Let's assume that there is no need for efficient searches for simplicity in Version 1.
However, if you come with a model which can do that also, I am happy to consider it - let's call it Version 2.
I think a list is not a good data structure here because of the wanted opportunity for deletions and continuous easy addition to the end of the data structure.
The data structure stack is not going to work either because of deletions.
I think some sort of tree data structure can work because of rather cheap deletions and cheap search there.
However, a simple array in the Data-parallelism model can be sufficient.
Languages
I think Java is a good option here because of parallelism.
However, any language and pseudocode are good too.
Frontend
I have an intuition that requirements for such a system in the frontend should be qT as a terminal emulator.
What is a better data structure for cheap deletions and continuous additions to the end?
Java LinkedList seems to be the thing you could use for version 1. you can use its single param add() to append to the list in constant time. if by "real-time" you mean when the image is in user's display and thus pointed to somehow, can delete them in constant time as well.
optimum use of memory and no re-instantiation as you'd have with an Arraylist.
any doubly linked list implemented on objects (as opposed to an array) would do.
your second version isn't clear enough.

LinkedList vs ArrayList - asking point of view for implementation ideas

i want to use a list which will store objects of some type (lets say for simplicity - books) so i can show them in a listview object.
im kinda new to this, so i ask for the help of more advanced and experienced users about the following debates -
which one to use? linkedlist is something im familiar with. however, how do i make the app maintain the list? should i save the details of each object in a XML? if i do that, isnt it just better to use Arraylist? (please exclude in your answer things related to proccessing time).
if not via xml - how do i 'store' a list for later use even when the app is shut down and later on activated?
Thanks!
ArrayLists are good to use when you want random access via an indexed lookup. They're just as well suited for iterating through as LinkedLists.
OTOH, LinkedList doesn't need to be resized, it only runs out of room when you run out of memory to hold more nodes. If you have lots of data growth, or you're doing lots of sequential add/removes, then LinkedLists will win out in performance.
Sometimes you need both random access and growth, in those cases you need to make a judgment call on which criteria you want to be more performant.
In your current use case, I'd probably choose an ArrayList, you'll likely know how big the list should be, it won't be changing in size that often, and if you want to display this thing in a GUI, you're likely to need to do indexed lookups.
As far as storing the list, XML is as good a means as any, CSV files (or plain line-delimited text files), YAML, JSON and even class serialization are some alternatives, choose what's easiest and most convenient for you.
You must storage your data into SQLite. Android provides a very easy way. Look at this tutorial: http://www.vogella.de/articles/AndroidSQLite/article.html
I would prefer ArrayList over LinkedList because it has methods to manipulate the size of the array that is used internally to store the list
If i am going to use it as a stack, queue, or double-ended queue then I would use a LinkedList

Categories

Resources