How to disable collapsible in JFace TreeViewer

Published on

Restricting all key events on Tree looks promising but you would loose navigating the tree structure and expand/collapse on tree node and all other functionality. tree.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { e.doit = false; } }); Or also if you use JTree, JTree jtree = new JTree(); jtree.setToggleClickCount(0);

How to insert a word if the string has more than one space in Java?

Published on

Quest: I have the following code, String s = ” Hello I’m a multi spaced String” In string s, there are multiple (indeterminate) spaces; but, I need to print it as %temp%Hello I’m a%temp%multi spaced String How can I do this? Try This, Use regex \s{2,} and replaceAll() method like this: s.replaceAll(“\\s{2,}”,”%temp%”); Output %temp%Hello I’m … Continue reading How to insert a word if the string has more than one space in Java?

when can I use IntStream.range effectively in Java?

Published on

Here’s an example: public class Test { public static void main(String[] args) { System.out.println(sum(LongStream.of(40,2))); // call A System.out.println(sum(LongStream.range(1,100_000_000))); //call B } public static long sum(LongStream in) { return in.sum(); } } So, let’s look at what sum() does: it counts the sum of an arbitrary stream of numbers. We call it in two different ways: … Continue reading when can I use IntStream.range effectively in Java?

How to find nearest locations around a route in MOngoDB Spring Data

Published on

The route information from the maps googleapi is broken down into steps that have a start location and end location with latitude/longitude coordinates. Computing the distance of the points in your model to all the start/end locations in the route would give you a measure of how far the point is from the route. The … Continue reading How to find nearest locations around a route in MOngoDB Spring Data

How to perform a bulk update of documents in MongoDB with Java

Published on

How to perform a bulk update of documents in MongoDB 3.2 with MongoDB Java Driver 3.2? Using the example in the manual on the new bulkWrite() API, consider the following test collection which contains the following documents: { “_id” : 1, “char” : “Brisbane”, “class” : “monk”, “lvl” : 4 }, { “_id” : 2, … Continue reading How to perform a bulk update of documents in MongoDB with Java

what is the difference between implicit and explicit ArrayList size declarations?

Published on

The difference between the following declarations: List list1 = new ArrayList(); List list2 = new ArrayList(10); Here is the source code for you for first example public ArrayList() { this(10);} So there is no difference. Since the initial capacity is 10, no matter you pass 10 or not, it gets initialised with capacity 10. Can … Continue reading what is the difference between implicit and explicit ArrayList size declarations?

How to Remove a character from an ArrayList of characters

Published on

A char is promoted to an int, which takes precedence over autoboxing, so remove(int) is called instead of remove(Object) you may have intuitively expect. You can force the “right” method to be called by boxing the argument yourself: chars.remove(Character.valueOf(‘a’));

converting from Object to Subclass in Java

Published on

One of the ways how it could be done it to declare a superclass or interface Target and use it to keep targets array, the full code sample with abstract class: ArrayList<NPC> npcs = new ArrayList<NPC>(); ArrayList<Item> items = new ArrayList<Item>(); ArrayList<EnviromentalObject> enviromental_objects = new ArrayList<EnviromentalObject>(); ArrayList<Target> targets; public Target check_for_target(String target_name) { targets.addAll(npcs); targets.addAll(items); … Continue reading converting from Object to Subclass in Java