Sure, in some cases, absolutely – although it is sometimes more interesting than that. Also, the goal is not for it to barely work, but for it to be helpful and idiomatic in Processing.
So, for example, check out anagrams – Java and Processing
// Java 1.5+
import java.net.*;
import java.io.*;
import java.util.*;
public class WordsOfEqChars {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.puzzlers.org/pub/wordlists/unixdict.txt");
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader reader = new BufferedReader(isr);
Map<String, Collection<String>> anagrams = new HashMap<String, Collection<String>>();
String word;
int count = 0;
while ((word = reader.readLine()) != null) {
char[] chars = word.toCharArray();
Arrays.sort(chars);
String key = new String(chars);
if (!anagrams.containsKey(key))
anagrams.put(key, new ArrayList<String>());
anagrams.get(key).add(word);
count = Math.max(count, anagrams.get(key).size());
}
reader.close();
for (Collection<String> ana : anagrams.values())
if (ana.size() >= count)
System.out.println(ana);
}
}
// Processing
import java.util.Map;
void setup() {
String[] words = loadStrings("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt");
topAnagrams(words);
}
void topAnagrams (String[] words){
HashMap<String, StringList> anagrams = new HashMap<String, StringList>();
int maxcount = 0;
for (String word : words) {
char[] chars = word.toCharArray();
chars = sort(chars);
String key = new String(chars);
if (!anagrams.containsKey(key)) {
anagrams.put(key, new StringList());
}
anagrams.get(key).append(word);
maxcount = max(maxcount, anagrams.get(key).size());
}
for (StringList ana : anagrams.values()) {
if (ana.size() >= maxcount) {
println(ana);
}
}
}
Same algorithm – the Processing answer is based on the Java answer. But lots of differences:
Processing |
Java |
import java.util.Map; |
import java.net.*; import java.io.*; import java.util.*; |
loadStrings() |
BufferedReader(InputStreamReader(URL.openStream())) |
StringList |
Collection<String> and ArrayList<String> |
void setup() |
public class foo { public static void main() } |
max() |
Math.max() |
println() |
System.out.println() |
You could copy-paste the Java example and tweak a few things and the BufferedReader and Map<String, Collection<>> would work – but even though it worked, it wouldn’t show you a Processing way to solve the problem with Processing built-ins and data structures.
Or compare Bulls & Cows games:
Java uses Scanner(System.in)
with a do...while
loop to catch keyboard input and process it every 4 characters. That whole setup doesn’t make any sense in Processing – key events are processed with the key handlers, and Processing is already running a loop – draw. You don’t want an user input “while” inside draw! A Processing solution could just print to the console, but for status text (like numeric input, or scores) it makes more sense to display game feedback on the sketch surface…