Optimize Memory Java String Array I was trying to solve one programming challenge :

First input is a list of connections which is made of pair of IPv4
address.  Each IP has its own state which starts with false, each
connection also has state which is made up by logical AND of state of
its IPs
Second input is a toggle IPs list , these toggles the state of IP
address ( false to true or true to false). For each toggle action ,
calculate the impact on connections which is defined as how many
connections state changed due to change in one IP address.

I solved the above challenge by using below Java program, but I'm not sure if there could be any scope of improvement for memory consumption and run time.
Please pardon my programming knowledge, I would appreciate any positive feedback to cover corener cases
public class NumberOfDevices {

    public static void main(String[] args) {
        String [][]connections ={{&quot;192.167.0.0&quot;,&quot;192.167.0.1&quot;},{&quot;192.167.0.2&quot;,&quot;192.167.0.0&quot;},{&quot;192.167.0.0&quot;,&quot;192.167.0.3&quot;}};
        String []toggleIps= {&quot;192.167.0.1&quot;,&quot;192.167.0.0&quot;,&quot;192.167.0.2&quot;,&quot;192.167.0.0&quot;, &quot;0.0.0.0&quot;};
        int[] result = numberOfDevices(connections, toggleIps);
        
        for(int i:result) {
            System.out.print(i + &quot; &quot;);
        }
    }

    public static int[] numberOfDevices(String[][] connections, String [] toggleIps) {
        int count =0;
        int [] result = new int[toggleIps.length];
        if(connections.length&gt;0) { // already a given contaraint that the length of toggle IPs is &gt;=1


            Map&lt;String,Ipv4Address&gt; ip = new HashMap&lt;&gt;();
            for(int i=0; i&lt;connections.length; i++) {
                Ipv4Address ip1, ip2;
                
                if(!ip.containsKey(connections[i][0])) {
                    ip.put(connections[i][0], new Ipv4Address(connections[i][0]));                  
                }
                if(!ip.containsKey(connections[i][1])) {
                    ip.put(connections[i][1], new Ipv4Address(connections[i][1]));                  
                }
                
                ip1=ip.get(connections[i][0]);
                ip2=ip.get(connections[i][1]);
                
                ip1.addConnection(ip2);
                ip2.addConnection(ip1);

            }
            
            for(int j=0; j&lt;toggleIps.length; j++) {
                if(!ip.containsKey(toggleIps[j])) {
                    result[j]=0;
                }
                else {
                    Ipv4Address ipv4 = ip.get(toggleIps[j]);
                    ipv4.setState(!(ipv4.getState()));
                    for(Ipv4Address conn:ipv4.getConnections()) {                       
                        if(conn.getState()) {
                            result[j]++;
                        }                       
                    }
                }
            }
        }
        
        
        return result; 
    }

}

// class representing the IPv4 address , it has IP address, current state and the connections where its present
class Ipv4Address{
    String ipAddress;
    boolean state;
    List&lt;Ipv4Address&gt; connections;
    

    public Ipv4Address(String ipAddress) {
        this.ipAddress = ipAddress;
        this.state=false;
        connections= new ArrayList&lt;&gt;();
    }

    public boolean getState() {
        return this.state;
    }
    public void setState(boolean state) {
        this.state=state;
    }

    public void addConnection(Ipv4Address ipaddress) {
        this.connections.add(ipaddress);
    }
    public List&lt;Ipv4Address&gt; getConnections(){
        return this.connections;
    }

}
Welcome to Stack Review, it seems me there is no much space for improvement, just some little tips:
class Ipv4Address {
    String ipAddress;
    boolean state;
    List&lt;Ipv4Address&gt; connections;
   
    //setters and getters
}

Probably you included your Ipv4Address class inside the NumberOfDevices class file but for a cleaner code better create a Ipv4Address class file separated. If you have defined setters and getters, you can encapsulate fields into the class using the private access modifier like below:
public class Ipv4Address {
    private String ipAddress;
    private boolean state;
    private List&lt;Ipv4Address&gt; connections;

    public Ipv4Address(String ipAddress) {
        this.ipAddress = ipAddress;
        this.state = false;
        this.connections = new ArrayList&lt;&gt;();
    }
    
    //getters and setters
}

No much to say about NumberOfDevices; in its numberOfDevices method you have the following code :
public static int[] numberOfDevices(String[][] connections, String [] toggleIps) {
    int count =0;
    int [] result = new int[toggleIps.length];
    if(connections.length&gt;0) { // already a given contaraint that the length of toggle IPs is &gt;=1
    
    
       Map&lt;String,Ipv4Address&gt; ip = new HashMap&lt;&gt;();
       for(int i=0; i&lt;connections.length; i++) {
          Ipv4Address ip1, ip2;
                    
          if(!ip.containsKey(connections[i][0])) {
            ip.put(connections[i][0], new Ipv4Address(connections[i][0]));                  
          }
          if(!ip.containsKey(connections[i][1])) {
            ip.put(connections[i][1], new Ipv4Address(connections[i][1]));                  
          }
                    
          ip1=ip.get(connections[i][0]);
          ip2=ip.get(connections[i][1]);
                    
          ip1.addConnection(ip2);
          ip2.addConnection(ip1);
    
     }

      //other lines
}

You can rewrite this part of your method using a foreach construct like below:
public static int[] numberOfDevices(String[][] connections, String[] toggleIps) {
    int[] result = new int[toggleIps.length];

    if (connections.length == 0) { return result; }

    Map&lt;String, Ipv4Address&gt; map = new HashMap&lt;&gt;();

    for (String[] connection : connections) {

        for (String c : connection) {

            if (!map.containsKey(c)) {
                map.put(c, new Ipv4Address(c));
            }
        }

        Ipv4Address ip1 = map.get(connection[0]);
        Ipv4Address ip2 = map.get(connection[1]);
        ip1.addConnection(ip2);
        ip2.addConnection(ip1);
   }

   //other lines
}