Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an object as a parameter and modify it within the method

Let's say I got a Map<String, String> and I wanna remove all the entries that value contains foo. What is the best way to do it, in terms of optimization/memory/etc.? The four syso below are printing the same result, that is to say {n2=bar}.

public static void main(String[] args) {

    Map<String, String> in = new HashMap<String, String>();
    in.put("n1", "foo");
    in.put("n2", "bar");
    in.put("n3", "foobar");

    // 1- create a new object with the returned Map
    Map<String, String> in1 = new HashMap<String, String>(in);
    Map<String, String> out1 = methodThatReturns(in1);
    System.out.println(out1);

    // 2- overwrite the initial Map with the returned one 
    Map<String, String> in2 = new HashMap<String, String>(in);
    in2 = methodThatReturns(in2);
    System.out.println(in2);

    // 3- use the clear/putAll methods
    Map<String, String> in3 = new HashMap<String, String>(in);
    methodThatClearsAndReadds(in3);
    System.out.println(in3);

    // 4- use an iterator to remove elements
    Map<String, String> in4 = new HashMap<String, String>(in);
    methodThatRemoves(in4);
    System.out.println(in4);

}

public static Map<String, String> methodThatReturns(Map<String, String> in) {
    Map<String, String> out = new HashMap<String, String>();
    for(Entry<String, String> entry : in.entrySet()) {
        if(!entry.getValue().contains("foo")) {
            out.put(entry.getKey(), entry.getValue());
        }
    }
    return out;
}

public static void methodThatClearsAndReadds(Map<String, String> in) {
    Map<String, String> out = new HashMap<String, String>();
    for(Entry<String, String> entry : in.entrySet()) {
        if(!entry.getValue().contains("foo")) {
            out.put(entry.getKey(), entry.getValue());
        }
    }
    in.clear();
    in.putAll(out);
}

public static void methodThatRemoves(Map<String, String> in) {
    for(Iterator<Entry<String, String>> it = in.entrySet().iterator(); it.hasNext();) {
        if(it.next().getValue().contains("foo")) {
            it.remove();
        }
    }
}
like image 322
sp00m Avatar asked Jun 12 '12 10:06

sp00m


People also ask

When an object is passed as a parameter into a method?

To allow a method to modify a argument, you must pass in an object. Objects in Java are also passed by value, however, the value of an object is a reference. So, the effect is that the object is passed in by reference. When passing an argument by reference, the method gets a reference to the object.

How do you pass a parameter to a method?

Parameters and Arguments Information can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

Can we pass an object in method argument?

We can pass the data to the methods in form of arguments and an object is an instance of a class that is created dynamically. The basic data types can be passed as arguments to the C# methods in the same way the object can also be passed as an argument to a method.

Can we pass parameters in method?

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.


2 Answers

The best way is methodThatRemoves because:

  1. In terms of memory consumption: it doesn't create a new map so doesn't add memory overhead.
  2. In terms of CPU use: iterator has O(1) complexity for calling next or removing the current element.
like image 132
tibtof Avatar answered Oct 20 '22 01:10

tibtof


The most efficient way methodThatRemoves, because it

  • Uses almost no memory
  • Creates no objects except the (lightweight) iterator
  • Is extremely fast (doesn't use any map lookups)

I would not make a copy first though, unless you have an unmodifiable map or you need to preserve the original.

like image 27
Bohemian Avatar answered Oct 19 '22 23:10

Bohemian