import java.util.*;

/*
 * Copyright (C) 2004 by Zhang Le <ejoy@users.sourceforge.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
public class StringReduction {
    public static Map<String, Integer> reduce(Map<String, Integer> m, int freq) {
        Map<String, Integer> out = new TreeMap<String, Integer>();
        // TODO : move this out of here!
        // TODO : object comparison is probably enough to avoid the ArrayList
        List<Map.Entry<String, Integer>> sort = new ArrayList<Map.Entry<String, Integer>>(m.entrySet());
        Collections.sort(sort,
                new Comparator<Map.Entry<String, Integer>>() {
                    public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
                        return a.getKey().compareTo(b.getKey());
                    }
                });
        int size = sort.size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // Avoid self comparison
                if (i == j) {
                    continue;
                }
                if ((Math.abs(sort.get(i).getValue()) - Math.abs(sort.get(j).getValue()) < freq)
                        && Tools.is_substr(sort.get(i).getKey(), sort.get(j).getKey())) {
                    if (sort.get(i).getValue() > 0) {
                        sort.get(i).setValue(-sort.get(i).getValue());
                    }
                }
            }
        }
        for (int k = 0; k < sort.size(); k++) {
            if (sort.get(k).getValue() > 0) {
                out.put(sort.get(k).getKey(), sort.get(k).getValue());
            }
        }
        return out;
    }

}

@@ -1352,141 +1352,11 @@
     
-// TODO : move this out of here!%0A        // TODO : object comparison is probably enough to avoid the ArrayList%0A        List%3CMap.Entry
+Map
 %3CStr
@@ -1368,17 +1368,16 @@
 Integer%3E
-%3E
  sort = 
@@ -1384,27 +1384,15 @@
 new 
-ArrayList%3CMap.Entry
+TreeMap
 %3CStr
@@ -1408,441 +1408,141 @@
 ger%3E
-%3E
 (m
-.entrySet());%0A        Collections.sort(sort,%0A                new Comparator%3CMap.Entry%3CString, Integer%3E%3E() %7B%0A                    public int compare(Map.Entry%3CString, Integer%3E a, Map.Entry%3CString, Integer%3E b) %7B%0A                        return a.getKey().compareTo(b.getKey());%0A                    %7D%0A                %7D);%0A        int size = sort.size();%0A        for (int i = 0; i %3C size; i++) %7B%0A            for (int j = 0; j %3C size; j++
+);%0A        for (Map.Entry%3CString, Integer%3E a  : sort.entrySet()) %7B%0A            for (Map.Entry%3CString, Integer%3E b  : sort.entrySet()
 ) %7B%0A
@@ -1606,14 +1606,19 @@
 if (
-i == j
+a.equals(b)
 ) %7B%0A
@@ -1695,27 +1695,17 @@
 ath.abs(
-sort.get(i)
+a
 .getValu
@@ -1720,27 +1720,17 @@
 ath.abs(
-sort.get(j)
+b
 .getValu
@@ -1789,41 +1789,21 @@
 str(
-sort.get(i).getKey(), sort.get(j)
+a.getKey(), b
 .get
@@ -1836,27 +1836,17 @@
     if (
-sort.get(i)
+a
 .getValu
@@ -1884,41 +1884,21 @@
     
-sort.get(i).setValue(-sort.get(i)
+a.setValue(-a
 .get
@@ -1988,39 +1988,55 @@
 or (
-int k = 0; k %3C sort.size(); k++
+Map.Entry%3CString, Integer%3E k  : sort.entrySet()
 ) %7B%0A
@@ -2051,27 +2051,17 @@
     if (
-sort.get(k)
+k
 .getValu
@@ -2099,41 +2099,21 @@
 put(
-sort.get(k).getKey(), sort.get(k)
+k.getKey(), k
 .get

rewrite to be more java-like