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
How to look for a particular object in a large list(java collection) without iterating it. Assume we have a large collection and just need to check whether a object exists or not without iterating.
Ok, Let's step out of the binary world.
Think of a chest full of Lego parts. You want a 2x2 flat black piece.
How would you find it without looking in the chest?
There is no magical to find it, you need to jump into the chest and find the piece grabbing one by one and checking if it's the one you are looking for.
There are ways to speed up the process.
You can Organise (sort your collection) by colour for example and just look in the black pile.
Or you can map (Index your pieces) so you know the position of the piece and can go and retrieve from you know where the piece is.
That is, in a very simplistic way, the same idea for databases and collection.
So, summarizing, no, you can't not just find without looking. Sorry :(
Related
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
Currently my program is filled with many ugly references that often make field or method access look like this: weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2. I want to move to shorter and faster strong references like field1.field2. But my program design is such that I will also have to go for an ArrayList element-by-element search (in a for-loop) instead of accessing a WeakHashMap by get() method.
Thus, I'd like to understand, can moving to simpler references compensate for rejecting HashMap performance wise. Herewith I presume that WeakHashMap.get() is much faster than a loop-search of ArrayList.
Can someone, please, give me a rough estimate? Or maybe there's even an appropriate comparison table like this one. I'd appreciate that.
Thank you.
Currently my program is filled with many ugly references that often make field or method access look like this:
weakReference1.get().weakReference2.get().field1.getSomeCustomObject().field2
Given that the objects involved are not Data Transfer Objects
this is a violation of the law of Demeter aka Don't talk to Strangers / Tell, don't ask!
Following this LoD principle you should move the operations working with the data in field2 to a new method in the class SomeCustomObject.
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 6 years ago.
Improve this question
In Data Structures and Algorithms in Java, the advantages of ordered arrays are stated. One of the advantages, I wish I had some kind of real example for. This is not for homework, but just self-clarification. What are some real cases for when insertion/deletion is not frequent, but searches are frequent? Anything would help even if you can point me in the direction of some github repository. Thank you.
An example would be a dictionary. After it is built, it can be looked up millions of time. Like your paper dictionary, the words in it better be sorted.
While I like leeyuiwah's answer, a more common domain which you can see in commercial context is a data base of some entity, for example the customers or employees, for which normally you create a view. That's why we index them (make the retrieval faster). Indeed, after inserting some records most of the operations will be retrieval which includes a search (based on complicated conditions or a simple identifier).
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 have a project in which I need to create a "robot" that goes around and picks up batteries in a space (100x100 or whatever). I am stuck on the best way to hold his AI instructions. I want to create a list of some kind that has all instructions the robot would ever need. Example: if the robot is in a spot it would check the surrounding spots for a battery, empty space, or a wall. I want to create a lot of robots and give each scenario a random action (go up, down, left, right, etc) Each turn the robot does something. The robots with the best outcome (highest batteries) get to the next level.
Maybe this was too many details for my question but, would an array be the best way to hold the instructions?
Looking for a push in the correct direction. I am new to Java having gone through just 1 beginner class of it so far. Not looking for the code itself (examples are nice but I want to do my own work), of course, just ideas as to where to begin with the AI instructions.
To start with you should forget about what sort of data structures you are going to need, and start from the beginning. What are the robots legal moves - write that down, and get it crystal clear on how you want the robot to run.
You should write step by step use cases for each of the options which include what the positions are, sensors, batteries etc.
Once you have written all this down, the data structure will be much simpler to identify.
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
Trying to come up with the simplest way to create binary tree, so i read alot of code online from different sources.
how is it theres so many different ways to create a working binary tree? This is what i dont understand about programming syntax/language... the logic i get.. but i believe there should be a set , uniform foundation to create the same things in the simplest way. Am I wrong?
create tree
create node
create node root
Ok,
I will try to answer your question.
Let's take a big box. You want to put inside some things. Few days after, you will have to find something and use it. Someday, maybe you will want to throw something away.
So, what is your approach, if you are sure, that you will never use any object of them? Probably you will not care where and how you put this object. It is fast and easy to do.
What, if someday, you will want to use some objects? You keep this way and you will be loosing a lot of time for searching or maybe you will try to keep some order inside and easily find an object?
Exactly the same story is with the Binary Trees.
Each of alghoritms has some properties f.e. a lot of algos keep your tree balanced. Before you choose an implementation for your code, think about how you will use it and take a proper 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 8 years ago.
Improve this question
So I've been giving a project for school and was just hoping for reassurance towards the decision making I do.
My java skills are extremely basic and need to improve drastically soon! I just seem to be getting nowhere atm :P
Anyway, back on topic.
My first task is to create an interface directory that can do the following
Keep in mind this is assessed work therefore please don't provide answers. I have enough time to complete this since I've started early!
My initial approach for going about this task is using a Linked List. I don't know what you guys think about that? I may be completely wrong but based on the topics we have covered in School. LinkedList definitely seems suitable. I can add, get and remove.
Cheers for reading guys!
You are along the right lines. The java.util.Collections package will contain most of what you need.
I would actually use an ArrayList rather than a LinkedList as it is faster for random access and sorting.
However note that it says you should be able to find people efficiently and look them up by name.
That suggests using something like a TreeMap structure, mapping name to a class containing information on each person. Store the names as "Surname, Forename" and they will be sorted correctly.
That will only allow lookups based on the complete and correct name though. If you want to search for partial names the map is less useful.