display in JTable part of a collection [duplicate] - java

This question already has answers here:
implement AbstractTableModel for a Java collection
(6 answers)
Closed 8 years ago.
I have created a JTable using AbstractTableModel in which I added a collection of objects (ArrayList).
I want to be able to search through the objects and return in the same JTable only the ones that meet a conditions (for example the names starts with "St"). Theoretically, how can I do that? Do I have to make new ArrayLists for every condition, and store these searched (and returned) objects there? Is there a better/simpler way? Thanks

As shown here, you can access a Collection in your implementation of AbstractTableModel. As shown here, you can sort and filter the results without modifying the original data structure. A complete example is examined here.

here is the thing,
if you are looking for syntaxic filtering this functionality already exists in java as TableRowSorter which can be combined with row filter to set the subList and show it.

Related

Setting List<E> files in location for later use

I'm working on some kind of a game. There are default values that don't change and are stored in a list. My question is, is there a way to make a list once and add it to the app files so if I need the list all I need to do is List<E> new = mylist? or something along those lines? I could work with arrays if needed but preferably lists.

Concurrently update list [duplicate]

This question already has answers here:
Is there a concurrent List in Java's JDK?
(8 answers)
Closed 5 years ago.
I have a list of accounts that will be updated - not too frequent ~1-2 times a day.
There would be a 'contains' lookup on this data at much regular interval.
An ideal data structure would have been ConcurrentLinkedList ,which unfortunately isnt around.
Is CopyOnWriteArrayList the sole preferred option?
If you are reading mostly , why you need concurrent data structure. Instead you can use HashSet or HashMap. It will make read faster. It is updated so less frequently then u can synchronise the writing part explicitly.

When to use which Data Structure? [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 7 years ago.
Improve this question
I am studying Data Structures in a Fundamentals of Software Development course. I have encountered the following data structures:
Structs
Arrays
Lists
Queues
Hash Tables
... among others. I have a good understanding of how they work, but I'm struggling to understand when and where to use them.
I can identify the use of the Queue Data structure, as this would be helpful in printer and/or thread queuing and prioritizing.
Knowing the strengths and weaknesses of a data structure and implementing it in code are different things, and I am finding the former difficult.
What is a simple example of the use of each of the data structures listed above?
For example:
Queue: first-in, first-out → used for printer queue to queue docs
I had trouble understanding them when i first started programming and so i decided to give a heads up to start with.
I am trying to be as simple as possible. Try Oracle Docs fro further details
Struct: When ever you need Object like structure, where you can group related data, use structs. Structs are very rarely used in java though(as objects are created in their place)
Arrays: Arrays are contiguous memory. when ever you want fixed time access based on index, unlike linkedlist, arrays are very fast and so use them.
But the backlog with arrays is that you need to know the size at the time of initialization. Also arrays does not support higher level methods such as add(), remove(), clear(),contains(), indexOf() etc.
List: is an interface which can be implemented using Arrays(ArrayList)
or LinkedLists (LinkedList). They support all the higher level methods specified earlier.
Also Lists re-sizes themselves whenever it is getting out of space. You can specify the initial size which the underlying Arrays or LinkedLists will be created, but whenever the limit is reached, it created the underlying structure with a bigger size and then copies the contents of the initial one.
Queue or Stack: is an implementation technique and not really a data structure. If you want FIFO implementation, you implement Queue on either Arrays or LinkedList(yes, you can implement this technique on both these data structures)
https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues
HashMap: Hashmap is used whenever you want to store key value pairs. if you notice, you cannot use arrays or linked lists or any other mentioned data structure for this purpose. a key can be any thing from String to Object(but note that it has to be an object and cannot be a primitive) and a value can also be any object
google out each data structure for more details
It depends on what you need. If you read and learn more about these data structures you will find convenient ways for their implementation.
Maybe read this book? http://www.amazon.com/Data-Structures-Abstraction-Design-Using/dp/0470128704
All these data structures are used based on their needs in the program. Try to find advantages of one data structure to the other. That should get things more clear for you.What I say wouldn't be much clear, but i'll give it a shot
Like for example,
Structs are used to create a data type, say you want to have a data type for Book & have the names of the bookBook Structure.
Lists are easier to access both ways if you use linked lists & are better than array some times. Queues, well, you can imagine them as real life queues, First In will be First Out. So you can use them when you need to set this priority.
Like I said, looking for advantages of one over the other should get things clear for you.

Need help creating a Quiz App for Android w. HashMaps & No Database

My assignment is to create a simple Quiz app for Android. I am NOT looking for the code for this.. I am simply looking for someone to possibly lay out how to get started with this (pseudo-code ish).
The program is suppose to display one term and four definitions; one of them being the correct one. Once the user selects which one they think it is, the program will tell the user if it was correct / incorrect using a toast message.
Specs:
The terms need to be randomly selected, and only displayed once per run. However the definition needs to stay in the pool of definitions for other questions.
Definitions randomally selected (except the right one)
Program ends if it runs out of terms to display
Needs to use an ArrayList to hold collections of items, and a HashMap to hold name-value pairs.
Must use Androids logging mechanism to log error messages (via try/catch statements).
If you can help me out by guiding me in the right direction, that would be great :) Thanks!
I would (as always in object-oriented languages) start by mapping your problem to real-live objects. In your case, there would be two:
A Question-class which holds four answers, the question and offers a method to check if the given answer was correct.
An Answer-class which holds a single answer and whether it's correct or not.
As for your storage question, I wouldn't use a HashMap at all. Store the Question-instances in an ArrayList and use a Random.nextInt()-method to get a random question from your list.
In your Question-class, you would store all possible answers in another ArrayList. To randomize the order in which the answers are presented, you can use the Collections.shuffle()-method.
For the presenting part, have your Question-class return the question (string) and the four answers (strings) and put the into your widgets. To identify the answer given, you can use the Answer-instance (using the array-index is easier, but it is more error prone).
To check if the right answer was given, query your Question-classes isCorrect(Answer)-method and check if the given instance in the internal ArrayList is marked as the correct one.

How do i visualize graphs in java ? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Plot Graphs in Java
I was trying to create graphs for x and y values in my java Application. Unfortunately while using the libraries in http://www.epic.noaa.gov/java/sgt/index.html, i found that most of the classes/ methods are deprecated. Kindly help me find a way out of this. Is there any alternate library that i can use to construct a graph. I have a for loop in which i generate the x and y values. Therefore, a method where i can directly feed data and then at the end of the loop create a graph would be nice.
Try this one here, this is the link, yes this is it: http://www.jfree.org/

Categories