Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!

Stephan van Hulst

Saloon Keeper
+ Follow
since Sep 20, 2010
Enschede, The Netherlands
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stephan van Hulst

Here's a red-black tree:

Imagine that the integer values of the nodes are the keys of the map. When you ask TreeMap to retrieve the entry for key '7', it looks if the key is less than or greater than the key of the root node. Since it's less, it then moves to the left child of that node and compares the keys again. '7' is greater than '2', so it moves to the right child of that node and compares the keys again. It then finds that that node has the same key, so it returns that node.

Changing the way that keys compare to each other does not change the current structure of the tree. If the Comparator you've passed in suddenly changes the order of its keys so that '7' is considered greater than '11', the search will try to find the entry to the right of the root node. It will report that the entry does not exist, even though you added it earlier.

This means that changing the way that keys compare to each other (because the average rating of a movie changed) possibly makes the map lose entries. Not only does it lose entries, but it may not longer add new entries correctly, because the tree is not in a valid state. It's complete behavior becomes undefined.

Allowing a Comparator to change the way it orders elements is the same to a TreeMap as allowing a key to return different hash codes is to a HashMap.
1 hour ago
Sure, but don't interact with the database directly, but instead use JPA (of which Hibernate is one implementation).

Also, note that JSP is not a substitute for JavaScript. If you want to dynamically display new messages as they come in (whether it's through WebSockets or by explicitly polling for them yourself), you will have no choice but to use JavaScript.
7 hours ago
Don't look at JSP to avoid front-end work. You will still be writing HTML templates and style it with stuff like CSS. JSP is just there so you don't have to combine application logic and presentation in the same code.

Having said that, it's good to use JSP and become familiar with it. You will get a lot of necessary experience with web page templates.
8 hours ago
It sounds like you just want to cache information retrieved from the external source. Again, I don't know whether you're using a web application framework (I really recommend it), but as an example, if you're using Spring you would make the component method that retrieves the values from the external service @Cacheable. They will then automatically be cached somewhere appropriate in the application scope, and you don't have to hack around with the servlet context or global variables.
8 hours ago
Well, seeing as you will probably want to notify clients of messages that other clients have sent, you will at least want to use something like Websockets or Comet. Seeing as a chat application probably doesn't need to deal with a high volume of information, either of these two will be fine.

Google around a bit to find out what server side frameworks allow you to easily use either of these two technologies. Are you already familiar with a certain web application container (such as Spring in Java, or Laravel in PHP, or ... etc.)?
9 hours ago
Use a JSON parser, not String operations.
10 hours ago
Alright, so does it need to be a desktop application or do users chat in their browser, or what?
11 hours ago
You can make a chat application with a whole bunch of different technologies. You can't settle on any before you've established your application's functional requirements. It's kinda odd that you already picked a database, even though you can probably build a chat application without using a database at all.

What do you want the applicator to do? How would the user interact with it?
14 hours ago
Well, unless Objects.hash() does weird things with the array, like passing it around to other method calls, escape analysis can determine that the object can be allocated on the stack.

Check out this question on StackOverflow:

Piet Souris wrote:Lastly but one: if the ratings map gets updated, it is then simply a matter of creating a new instance of the FilmComparator, or you could give that class a method like 'setNewMap'.

You will still not be able to use it in a TreeMap, because it will break the invariants that TreeMap has regarding its internal red-black tree.
23 hours ago
That's absolutely a valid solution, but you're pretty much just using your map as a list of key-value pairs, with the only difference being that the list will be sorted while it's being filled, instead of afterwards.
1 day ago
If you're using Swing, instead of setting the font on each component, you can set defaults on the UIManager before Swing starts running:

Here are all the keys I could find:

  • Button.font
  • CheckBox.font
  • CheckBoxMenuItem.font
  • ColorChooser.font
  • ComboBox.font
  • DesktopIcon.font
  • EditorPane.font
  • FormattedTextField.font
  • Label.font
  • List.font
  • Menu.font
  • MenuBar.font
  • MenuItem.font
  • OptionPane.font
  • Panel.font
  • PasswordField.font
  • PopupMenu.font
  • ProgressBar.font
  • RadioButton.font
  • RadioButtonMenuItem.font
  • ScrollPane.font
  • Slider.font
  • Spinner.font
  • TabbedPane.font
  • Table.font
  • TableHeader.font
  • TextArea.font
  • TextField.font
  • TextPane.font
  • TitledBorder.font
  • ToggleButton.font
  • ToolBar.font
  • ToolTip.font
  • Tree.font
  • Viewport.font
  • 1 day ago
    Why? Because it's more convenient? Bad idea. In fact, I find it very suspicious that you have extra data in your servlet context in the first place.

    What kind of objects are these, and why don't you retrieve them from a repository rather than the servlet context?

    If these really are objects that must remain in memory and be globally accessible (they MUST be immutable), register them with your IoC container as singletons. In Spring, you can do that with the @Component (on a class) or @Bean (on a method) annotations.
    1 day ago

    1 day ago

    Campbell Ritchie wrote:I was thinking the idea of the Map passed to the constructor was simply to provide access to the Rating lists.

    Yes, and this is a bad idea, because the only situation in which sorting anything at all will be at least as performant as sorting a list of key-value pairs will be if the map is hash-based, meaning you have a hard requirement on HashMap or LinkedHashMap.

    Presumably it would be all right to take all the films out of a hash map and use those averages to sort it into a tree map (or similar)?

    If you keep a TreeMap sorted based on the values of another map the behavior will be undefined unless that second map remains unchanged. The strength of TreeMap is that you keep it sorted even after you add new entries. If you can't add new entries because you can't change the content of the second map, then what's the point of using a TreeMap?

    But what is going to happen if you take the Films out of the Map and concurrently scores are added to a film which later alter the sorting?

    It doesn't even matter if you do it concurrently. Once you alter the values of the map, the content of your TreeMap will be completely undefined if it uses a comparator based on values of another map.
    1 day ago