Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
Why does this code use an oversized array instead of a Map?

Here's JBoss JSTL implementation for the EscapeXML tag

public class EscapeXML {

    private static final String[] ESCAPES;

    static {
        int size = '>' + 1; // '>' is the largest escaped value
        ESCAPES = new String[size];
        ESCAPES['<'] = "&lt;";
        ESCAPES['>'] = "&gt;";
        ESCAPES['&'] = "&amp;";
        ESCAPES['\''] = "&#039;";
        ESCAPES['"'] = "&#034;";
    }
  //omitted
}

Why is ESCAPES a 61 elements array? What are the implication of using a Map<Character,String> instead?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think the main reason is performance. Each map query needs to get the hashcode, and then calculate the position of the array in the map, and the array can be obtained directly. The following is a simple test, querying 10,000 times separately, the array is about 10 times faster than the map.

array query result: cost time= 184041
map query result: cost time= 1677042
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author jahe
 * @date 2022/1/9
 * @note
 */
public class ArrayMapTest {
    private char[] chars = {'<', '>', '&', '\'', '"'};
    private char[] charsForQuery = new char[10000];
    @Before
    public void init(){
        Random random = new Random(5);
        random.nextInt(5);
        for (int i = 0; i < charsForQuery.length; i++) {
            charsForQuery[i] = chars[random.nextInt(5)];
        }
        System.out.println(Arrays.toString(charsForQuery));
    }
    @Test
    public void test() {
        int size = '>' + 1;
        String[] ESCAPES = new String[size];
        ESCAPES['<'] = "&lt;";
        ESCAPES['>'] = "&gt;";
        ESCAPES['&'] = "&amp;";
        ESCAPES['\''] = "&#039;";
        ESCAPES['"'] = "&#034;";
        long start = System.nanoTime();
        doTestForArray(ESCAPES);
        long end = System.nanoTime();
        System.out.println("array query result: cost time= " + (end - start));

        Map<Character, String> map = new HashMap<>();
        map.put('<', "&lt;");
        map.put('>', "&gt;");
        map.put('&', "&amp;");
        map.put('\'', "&#039;");
        map.put('"', "&#034;");
        start = System.nanoTime();
        doTestForMap(map);
        end = System.nanoTime();
        System.out.println("map query result: cost time= " + (end - start));

    }
    private void doTestForArray(String[] ESCAPES){
        for (char c : charsForQuery) {
            String str = ESCAPES[c];
        }
    }
    private void doTestForMap(Map<Character, String> map){
        for (char c : charsForQuery) {
            String s = map.get(c);
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!