Generic pair class

Almost. I’d write it like this: public class Pair<F, S> { private F first; //first member of pair private S second; //second member of pair public Pair(F first, S second) { this.first = first; this.second = second; } public void setFirst(F first) { this.first = first; } public void setSecond(S second) { this.second = second; … Read more

Group by field name in Java

There’s probably a library that can do this more simply, but it’s not too hard to do it manually: List<Person> allPeople; // your list of all people Map<String, List<Person>> map = new HashMap<String, List<Person>>(); for (Person person : allPeople) { String key = person.getName(); if (map.get(key) == null) { map.put(key, new ArrayList<Person>()); } map.get(key).add(person); } … Read more

Java Swing Timer

This simple program works for me: import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String [] args) throws Exception{ ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { //…Perform a task… System.out.println(“Reading SMTP Info.”); } }; Timer timer = new Timer(100 ,taskPerformer); timer.setRepeats(false); timer.start(); Thread.sleep(5000); } }

How to read strings from a Scanner in a Java console application?

Scanner scanner = new Scanner(System.in); int employeeId, supervisorId; String name; System.out.println(“Enter employee ID:”); employeeId = scanner.nextInt(); scanner.nextLine(); //This is needed to pick up the new line System.out.println(“Enter employee name:”); name = scanner.nextLine(); System.out.println(“Enter supervisor ID:”); supervisorId = scanner.nextInt(); Calling nextInt() was a problem as it didn’t pick up the new line (when you hit enter). … Read more

java.util.Timer: Is it deprecated?

As others have mentioned, no it is not deprecated but I personally always use ScheduledExecutorService instead as it offers a richer API and more flexibility: ScheduledExecutorService allows you to specify the number of threads whereas Timer always uses a single thread. ScheduledExecutorService can be constructed with a ThreadFactory allowing control over thread aspects other than … Read more

Eclipse classpath entries only used for tests

You could separate all your tests into another project and add the main project as a dependency (Project->Properties->Java Build Path->Projects->Add…) Update: To avoid changing the original project structure, your test projects can use linked locations. Create the test project as normal, you now need to create a linked resource to bring in the src/test/java folder. … Read more

Replace HTML codes with equivalent characters in Java [duplicate]

Also, is there any way to optimize this regex? Yes, don’t use regex for this task, use Apache StringEscapeUtils from Apache commons lang: import org.apache.commons.lang.StringEscapeUtils; … String withCharacters = StringEscapeUtils.unescapeHtml(yourString); JavaDoc says: Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities. For … Read more

Creating an XML document using namespaces in Java

There are a number of ways of doing this. Just a couple of examples: Using XOM import nu.xom.Document; import nu.xom.Element; public class XomTest { public static void main(String[] args) { XomTest xomTest = new XomTest(); xomTest.testXmlDocumentWithNamespaces(); } private void testXmlDocumentWithNamespaces() { Element root = new Element(“my:example”, “urn:example.namespace”); Document document = new Document(root); Element element = … Read more

Java generics and array initialization

It’s because you can’t create, but you can use them: public class GenericsTest { //statement 1 public ArrayList<Integer>[] lists; public GenericsTest() { //statement 2 lists = new ArrayList[4]; //statement 3 lists[0].add(new Integer(0)); //statement 4 lists[0].add(new String(“”)); } } Statement 3 is possible, statement 4 will lead to a compiler error.