Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:44:58+00:00 2026-05-30T01:44:58+00:00

I have a problem were i need to aggregate some vectors in order to

  • 0

I have a problem were i need to aggregate some vectors in order to find some statistics. For example i have vectors of doubles and i need to sum them. My vectors look like this:

      1,0,3,4,5
      2,3,4,5,6
      3,4,5,5,6

My key-value pairs so far are (String,String). But every time i need to add these vectors, i first have to convert them to double arrays, add them up and finally convert the aggregate vector into string. I think it would be a lot faster if i just could have key-value pairs in the form (String,double array). There would be no need to convert them back and forth. My problem is that i cant find a way to have double arrays as value. Is there any easy way rather than create a new custom type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:44:59+00:00Added an answer on May 30, 2026 at 1:44 am

    You mean something like this?

    Map<String, List<Double>> arrays = new HashMap<String, List<Double>>();
    
    double[] array;
    arrays.put("ArrayKey", Arrays.asList(array));
    

    then you could call your map method:

    map(String key, String arrayKey) {
        List<Double> value = arrays.get(arrayKey);
    }
    

    Also you can serialize your double array, and then deserialize it back:

    package test;
    
    import org.apache.commons.codec.binary.Base64InputStream;
    import org.apache.commons.codec.binary.Base64OutputStream;
    
    import java.io.*;
    import java.util.Arrays;
    
    public class Test {
    
        public static void main(String[] args) throws IOException, ClassNotFoundException {
            double[] array = {0.0, 1.1, 2.2, 3.3};
            String stringValue = serialize(array);
            map("Key", stringValue);
        }
    
        public static void map(String key, String value) throws ClassNotFoundException, IOException {
            double[] array = deserialize(value);
            System.out.println("Key=" + key + "; Value=" + Arrays.toString(array));
        }
    
        public static String serialize(double[] array) throws IOException {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            Base64OutputStream base64OutputStream = new Base64OutputStream(byteArrayOutputStream);
            ObjectOutputStream oos = new ObjectOutputStream(base64OutputStream);
            oos.writeObject(array);
            oos.flush();
            oos.close();
            return byteArrayOutputStream.toString();
        }
    
        public static double[] deserialize(String stringArray) throws IOException, ClassNotFoundException {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(stringArray.getBytes());
            Base64InputStream base64InputStream = new Base64InputStream(byteArrayInputStream);
            ObjectInputStream iis = new ObjectInputStream(base64InputStream);
            return (double[]) iis.readObject();
        }
    }
    

    OUTPUT:

    Key=Key; Value=[0.0, 1.1, 2.2, 3.3]
    

    Mapping is faster, but serialization will be more usefull if you use nodes and clusters for that (if you need to pass your arrays into another JVM):

     private static class SpeedTest {
            private static final Map<String, List> arrays = new HashMap<String, List>();
    
            public static void test(final double[] array) throws IOException, ClassNotFoundException {
                final String str = serialize(array);
                final int amount = 10 * 1000;
    
                long timeStamp = System.currentTimeMillis();
                for (int i = 0; i < amount; i++) {
                    serialize(array);
                }
                System.out.println("Serialize: " + (System.currentTimeMillis() - timeStamp) + " ms");
    
                timeStamp = System.currentTimeMillis();
                for (int i = 0; i < amount; i++) {
                    deserialize(str);
                }
                System.out.println("Deserialize: " + (System.currentTimeMillis() - timeStamp) + " ms");
    
                arrays.clear();
                timeStamp = System.currentTimeMillis();
                // Prepaire map, that contains reference for all arrays.
                for (int i = 0; i < amount; i++) {
                    arrays.put("key_" + i, Arrays.asList(array));
                }
                // Getting array by its key in map.
                for (int i = 0; i < amount; i++) {
                    arrays.get("key_" + i).toArray();
                }
                System.out.println("Mapping: " + (System.currentTimeMillis() - timeStamp) + " ms");
            }
        }
    

    OUTPUT:

    Serialize: 298 ms
    Deserialize: 254 ms
    Mapping: 27 ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem: We need to find the next august. I other
I have a problem with reflection. I need to find the type that instantiates
I have a problem: imagine I have a plugin-based system. I need some kind
I have a problem where I need to remove all code and triggers from
I have the following problem: I need to use XSLFO to generate a 2-column
I have the next problem: I need to process only 1 request at a
I have a problem where I always need to give simple access to my
I have a problem. I need to host grid with controls in ScrollViewer to
I have a problem. I need to trace all read/write operations to the registry
i have a problem, i'm working on a gallery php script and need help,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.