Creating my own Lists and Maps data structures [closed] - java

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 7 years ago.
Improve this question
For practice I want to make my own lists and maps (like ArrayList, HashMap, HashSet etc.).
My goal is to have it as small and flexible as possible while still maintaining good performance. (long road...)
I have some questions:
1)
Unlike the sun, I don't have to take backwards compatibility into account.
So the first thing I wonder, is there any good reason to keep add and put?
Why not just one?
If I would name put > add would this give problems / complexity / unclearness down the road?
2)
Are there any languages known to have really good data structures? (For example, they could be really smart to avoid a concurrency exception).
3)
As last more a request then a question, if you have any tips our vision of how things could be done different then please post them.

There is no duplicated methods, Collection's have add method that returns a boolean, Map's have put method that returns type associated to Map.
There are plenty of examples of data structure, the point is, ¿what you need your data stucture do best? Avoid concurrency? sort? be fast? store securely?
The examples you need are directly in Java source code:
SOURCES
List
ArrayList
HashMap
and so on....

Related

General considerations when trying to determine what type of Java collection to use? [closed]

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 2 years ago.
Improve this question
I understand that this question is very vague, but I'm trying to figure out a thought process to find out which collection is better to use when it comes to managing data.
I guess I'm just wondering if there is anything that screams "USE THIS ONE!", such as:
Big Data sets, lots of insertion and searching for a specific value: Best Collection Type
Big Data sets, sorting and data manipulation: Best Collection Type
Big Data sets, sorted by last add: Best Collection Type
Big Data sets, delete or move an item after x about of time: Best Collection Type
etc...
Little research
Assuming you are asking for decision-support, rather than for an opinionated "best-practice", I did a little research to find guidelines, comparisons, or even decision-charts.
Articles explaining the benefits and usage of collections
Here you find each of the major Java Collections explained:
Java Collections Cheat Sheet.
Here you find a list of questions to ask in order to choose the right one, as well as best-practice for using collections and their methods:
18 Java Collections and Generics Best Practices
Articles containing flow-charts for collection decision
I also found some decision flow-charts at similar question Rule of thumb for choosing an implementation of a Java Collection?, e.g. originally from Sergiy Kovalchuk's Blog:
Or viewed from different perspectives in LogicBic's tutorial Java - Collection Interfaces and Implementations
Maybe some SO-members would like to share their most applied Collections, with practical use-cases or experience from the professional field.

Java: small key/value persister [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I need a very lightweight and persistent key/value-store in Java.
The amount of data is very very low and it should be very simple (getter and setter and all can operate on strings).
So I think of using some small NoSQL-DB or even giving some integrated collection a serializer/deserializer to the filesystem.
But I think NoSQL is a overkill and I hope a persister also exists for such a simple requirement.
Whats the best approach here? Any ideas?
You can either implement your own thing if it is a simple key-value string. (Have a look at Java's Properties class too in case it suits your requirements).
If your requirements are slightly more complex have a look at the embedded lightweight databases you can use. Maybe BerkleyDB might work for you. There are quite a number of others if you do a bit of search.
Also think about what you actually need to do with the data. Do you need to query it (so it needs to be indexed?) or do you just want to load it back all into memory? (in which case using a simple JSON or YAML text format would also suffice.)
Most Map<String,String> can be serialized. So for example look into https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
there you find Serializable. Under that point information to help yourself solve the Problem.

Create too many classes or have some schema-less data structure(like dictionary)? [closed]

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 8 years ago.
Improve this question
I'm have to use 50 different custom datatypes(/classes) which are defined in a document(xml/json), they have only fields and no methods and maybe strong validations.
My question is should i go ahead and create(/generate) 50 classes or use some generic data structure (like HashMap<String,Object>)?
Update: My fear is if i go with class geneartion, then my codebase might increased by very much
and if go with schema-less way, my data integrity might be compromised, so which one is lesser evil.
Unless it is just ridiculous, more code is more forgivable, in general. There are a few different reasons:
If you give them base classes at the right points, you can have it both ways, as your handling code can hold the base classes, and may have anchor points for extracting, validating or cleaning information stored in the different formats. Surely some of the processing can be shared.
If absolutely everything really falls to the base class, you can refactor the sub-classes out of existence without pain. On the other hand, if you start the amorphous way, gathering the special cases back into separate classes is more likely to go wrong.
Excessively large code is only bad if the extra volume does not clarify the logic for readers. I would have the classes, if they constitute units in which people think.
Also, actual functionality is more important than format or even readability. So if the risk is to data integrity vs code bloat, protect the content, not the form.

What would be your interpertation of this requested queue implementation? [closed]

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 had been reading two books on JAVA and while covering data structures, I started to do some online research with regards to "QUEUE" implementation. I have an extensive background in FLEX, however ACTIONSCRIPT isn't comparable to advance languages.
Lets say if I was on a job interview and asked to implement a Queue of Object, how should I pursue it as? I am not looking for code help here, I would like to what would you quick answer be? I have been to Java online docs and do understand there are 13 known implementing classes, and "LinkedList" is one of them.
Google search has return more results with "LinkedList" implementation code than any other.
My apologies if you find this question to be rubbish or pointless in anyway.
Oracle's Java online doc ref:
Do you know what the concept of a queue is and how it differs from a stack (closely related data structure)? If so, you should be able to think of multiple ways to implement it.
Which is best depends on the exact requirements of the task it's being used to address.
So the right response to that interview question is not to start coding but to ask them for more information about the requirements your implementation has to address. Performance? Memory size? Multitasking? Any limits on maximum queue depth, eg to guard against things like a DOS attack? What's being enqueued -- objects, primitives, other? Specific kinds thereof? Parameterized type? Are there any values which should be discarded (maybe null shouldn't be enqueued)?
Knowing the requirements, you should be able to judge which answer is appropriate. Starting coding without asking the requirements is immediately going to earn you a demerit.

Best data structure to store peculiarly structured data [closed]

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 9 years ago.
Improve this question
So, I'll be looping through a data base and there will be a bunch of campaigns. Each campaign will have some demos and some sites where certain conditions are satisfied. I want to plot some graphs for the data corresponding to all the campaigns, sites and demos. I was thinking of using java, first getting the campaign, site and demo combinations where the conditions are satisfied and then looping through all of them, running individual queries based on their values and plotting the graphs using maybe, GNU plot. My questions are -
Is there a better way to achieve this (with minimal queries).
If I do do it this way, I first have to store the information.
I was thinking of storing the campaign ids in an ArrayList of Integers, the demos for each campaign in
ArrayList<ArrayList<Integer>>
and the sites for each campaign in
ArrayList<ArrayList<Integer>>
Is there a more efficient way of storing this information?
I'd recommend creating a new class to hold your campaign data and storing references to each object within an ArrayList if you need to keep a handle to them in memory (may not be necessary).
From a purist point of view, the class should be backed by a Data Access Object (DAO) and Plain Old Java Object (POJO) to manage database access and storage in memory but if this is a simple prototype then I wouldn't worry too much. I'd also recommend a utility class to convert/write your chart data - all accessible from your Campaign class.
The Campaign class should also be able to work out whether your conditions are satisfied - and if it's worth generating those charts.

Categories