I'm missing something obvious here.
I have a glazedlists EventList<X> where X is my custom class. This list contains a bunch of values. When I update one of the values, how do I make sure the GUI updates its display for that row?
It looks like you can invoke addListEventListener to register a ListEventListener. See also the Glazed Lists Tutorial.
The FAQ mentiones two ways under the question:
How do I tell Glazed Lists that an Object in my EventList has been updated?
Either you use the get/set approach as pointed out by Jason, or you make the elements in the list observable by for instance the PropertyChangeListener and then use the ObservableElementList. I think this second approach is cleaner and it should also work with concurrent threads.
The way to do this appears to be to replace the list element with itself:
EventList<X> list = /* get reference to a list */
X x = list.get(3);
/* update x here */
list.set(3,x);
Related
I want to make a set of some type of collection (not sure which one yet) as a way of "storing duplicates" in a set. For example if I wanted to add the integer 5 with 39 additional copies I could put it into an arraylist at index 39. Thus if I were to get the size of the arraylist, I would know how many copies of 5 existed within the set.
There are a few other ways I could implement this but I have yet to decide on one. The main issue I'm having with implementing this is that I'm not sure how I can "dynamically" make arraylists (or whatever collection I may end up using) so that whenever someone were to call mySet.add(object), the object is first inserted into a unique arraylist then into the set itself.
Can anyone give me some ideas on how I could approach this?
EDIT:
Sorry I should have been more clear in my question. The point of the code that I'm writing is that we have a set-like collection that allows duplicates. And yes some of the associated methods will be re-written/will have to be re-written. Also my code should be written under the assumption that we do not know what type of object is being inserted(only one data type per set though) nor how many instances of the same object will be added nor how many different unique objects will be added.
I would rather go for using a Map like
HashMap list <Object, Integer>
where Object is the Object that you want to count and Integer is the count
You could try guava's MultiSet, I think it's what you want.
It can store the count of each object. What you need to do is just
multiSet.put(object);
And if it is put for the first time, like you said, a new list will be created, or its count will added by one.
So i have a code snippet here. I go this issue while i was discussing some code with my friend
Map<Integer , List<String>> myMap = new HashMap<Integer , List<String>>();
List<String> list = new ArrayList<String>();
myMap.put(45,list);
List<String> lst = myMap.get(45);
lst.add("String1");
lst.add("String2");
lst.add("String3");
System.out.println(myMap.get(45));
My question here is.
-> If its ok to modify the list outside the map through another reference? I am asking from OOP design point of view.
That is completely ok, IMHO
When you write
List<String> lst = myMap.get(45);
Still it is refering to the value in the map, for the key 45.
Once you get the value(reference to the list), It's up to you what you are doing with it.
If its ok to modify the list outside the map through another reference? I am asking from OOP design point of view.
It really depends on the context in which you're modifying it. If you plan on doing this a lot, with a lot of different values, then you're quickly going to find yourself with very confusing code that is difficult to debug and to follow.
BUT, in your example, you first load it from the map, then you edit it. It's completely clear that the data is coming from your Map object. Provided you make it clear with comments and documentation, especially when you're passing this reference between other methods, this isn't bad practise at all.
It is OK, provided that you take care of any potential synchronizations; e.g. if there are multiple threads that might be modifying the map and / or the list.
You might be confusing this with the case where you modify a key object. That is distinctly NOT ok if the modification breaks the hash table invariants; e.g.
if it causes either the key's hashcode to change, or
if it causes the key to give a different result when compared with some other key used in the table.
I am asking from OOP design point of view.
I'd say that OO design is neutral on this issue. You are using a Java interface (i.e. Map) that doesn't take control of the values. You are not violating encapsulation because the values are not encapsulated by the Map abstraction.
Whether this is sound design from the application perspective depends on the overall design. We can't make a judgement one way or another without understanding the context.
Every reference has a scope, it is your take(based on your requirement) whether you want the Map to be accessed through multiple reference or through a single reference.
It's OK.
After you have added numbers to the list in lines 5-7 in your code snippet, and then you get the list from the map again in line 8, the list you get from the map will have the extra numbers you just added.
That depends on what you want to do with the list and what your requirements are.
I'd say it is ok-ish but it might be better to encapsulate that in another object.
Consider the question what to do with empty lists, should they be removed or kept?
Encapsulation would allow you to ensure that empty lists are removed, since the user would then only access the wrapper, not the list directly.
Btw, with HashMap you have to change the list outside the map ;)
ArrayList is mutable. It is resizeable and keeps the same reference after modification. To have immutable list you should use following code.
List<String> list = Collections.unmodifiableList(new ArrayList<String>());
If you define list above way, than you can't modify it.
I have the following question:
if I have the following line of code:
List<Position> allPos = posDBM.getAllPos();
Position is an object
posDBM is a SQLite Database Manager class, which manages the SQLite database,
getAllPos() returns all database data.
The return type of getAllPos() is List<Position>.
If I want to initialize a List<> like this List<Position> pos = new, I have to specify the type of the List (ArrayList, LinkedList, etc.) .
So back to my question, what kind of List do I have, after I filled the list from the database?
I would guess it's an ArrayList , but I can't find any source to back this up. It's just a matter of interest...
You don't have to know; that's the point. The interface is what matters to you, not the implementation.
You can't know without looking at the source of that method. But even if you do, it's immaterial to your client. All you call are List methods.
That you will find in getAllPos() source code. List<Position> due to Polymorphism will accept all classes implementing List interface.
It you are just curious, then one way to find out is to do something like this:
List<Position> allPos = posDBM.getAllPos();
System.out.println("The class is " + allPos.getClass().getName());
Of course, you don't need to know ... because you don't need to instantiate the list implementation class yourself. The database management code deals with that.
The returned List<Position> is a generic or a Strongly Typed list. The option that you were asking is about ArrayList which specifies a list that can take up any object. This will require an overhead of Boxing and Unboxing when writing / reading using the ArrayList.
Ideally you should not worried about the actual implementation , once you have List returned from the method call , you can just iterate over it like this .
List<Position> allPos = posDBM.getAllPos();
for(Position position : allPos){
//Your code goes here
}
And if you want to initialize a new list you can do it in many ways by using different implementations of List interface , now which implementation you want to choose very much depends on your requirement.
I would suggest you to add a breakpoint and see allPos variable after posDBM.getAllPos(), the debugger should tell you the Type.
Let's say we have an array list of objects ObjArray.
What is the most efficient way for that object to locate itself within the list, and remove itself from the list?
The way I tend to use is this:
Every object in the list has an ID that corresponds to its place in the list
When object.remove() is called, the object simply simply calls ObjArray.remove(ID).
ObjArray is parsed from index ID upwards calling ObjArray.get(i).ID--. This sets all objects above the removed object to the right ID.
The other method is of course simply parsing ObjArray until a object match is found.
So, is there a better way of doing this? ArrayList is not necessary, if a HashMap or LinkedList can be used to do things better, that's just as good.
More information as requested.
Objects contain information as to where they need to be drawn on screen, and what image is to be drawn. The paint function of the main JPanel is called by a timer. The paint function loops through the list ObjArray and calls the the object's draw function (Obj.draw(Graphics g)).
Objects may be added or removed by clicking.
When an object is removed, it need to remove itself from the ObjArray list. I have stated the two methods that I can think of in the first part.
I would like to know if anyone knows of a more efficient way of doing this.
In short:
What's the most efficient way for an item to find/know its position in a list
Efficient in terms of code:
list.remove(this);
The object must be given a reference to the list of course.
Efficient in terms of performance would require a small redesign, probably involving a Map, but is beyond the scope of this question.
Usethe List's indexOf to get the ID. Drop your idea of an ID for each object._
I want to load a series of objects to an ArrayList, and return the index of where it was added so it can be used later directly from the ArrayList. It'd be akin to me loading a set of 5 frames for a graphic and calling the data for frame 3 directly from the list.
Upon looking around at a couple potential posts that may be related, I found something that was related, and made me wonder. Is there even a built in function to GET the index of a recently added object?
The link I am looking at that made me think of this was: ArrayList indexOf() returns wrong index?
Basically, the way I was looking at it was that I would do something along the lines of the following psuedocode:
private ArrayList<FrameData> mylistofframeshere = new ArrayList();
FrameData Framenumberone = new FrameData(constructorblah goes in here);
int wherediditgo = mylistofframeshere.add(Framenumberone);
Or I thought I could do something along the lines of
mylistofframeshere.getindex(Framenumberone);
My backgrounds in coding are more procedural based at this point, so I am more used to knowing what the index is, just in order to place the data to begin with. I looked around at the oracle documentation as well, with findings something similar to the above link. Any suggestions??
EDIT : I'm going to add some extra clarification in here, because I really didn't put enough effort into the example and explanation.
Another example of trying to use something like a direct index of a list would be if I had a set of tiles I wanted to use as terrain for a game or something. I would load a set of possible tiles for the area into a central ArrayList. Upon trying to place these tiles, I would just reference the pre-loaded object I have in my ArrayList in order to draw the appropriate bitmap/whatever.
I'm still a bit new to Java, so I'm willing to bet it's just something simple I'm overlooking in the mechanics of using these datatypes. I'll keep editing this until I get the explanation right.
When you add something to an ArrayList, it goes to the last available space. In other words:
List<FrameData> list = new ArrayList<FrameData>();
list.add(frame1);
FrameData frame = list.get(list.size() - 1); //frame == frame1
But I wonder why you would need to do that. If you explain more about what you are trying to achieve, there might be a different / better way to get to the same result.
There is a method like ArrayList.indexOf(object); , Try using that method to get index of the object
It all depends on what you want to use the index for. If you simply need to map each element in your list to a key so you can later retrieve the element with the key, you should perhaps consider using a HashMap. If you are more concerned with the ordering, you can use a List. As someone already answered, the index with be incremented as you add elements into the list. If you know the total number of frames you will have before hand, you can initialize an ArrayList by passing in the size as an argument to its constructor, then add each frame by manually specifying the index with list.add(0...size-1, element).
In short,
If you simply want to store and retrieve by your own key /
incremented key -> use a HashMap.
If ordering is important, use a list.
Instead of using ArrayList in this way, you can use a Map<Integer,FrameData>. you can replace Integer with anything which might fit better in your project.