Java data structure clause

I am new to this area, so please excuse my stupid mistakes :) So, the problem I am facing: On my webpage I am showing a table. At the moment, my problem is with the three columns of the table.

    First is : Area Code
    Second is : Zone Code
    Third is: Value

The relationship between the three:

1 The area code has 6 different area codes, and all these area codes have a corresponding “Value” value. I need a data structure that gives me the flexibility to get a “value” for the area code that matches a specific area code.

I have the same zone codes for all zone codes:

   Zone codes are: 111, 222, 333, 444, 555, 666

After surfing your stackoverflow, I thought I could go with this structure:

    Map<Integer, Map<Integer, Double>> retailPrices = new HashMap<Integer, Map<Integer, Double>>();
    Map<Integer, Double> codes = new HashMap<Integer, Double>(); 

where reatailPrices will contain the area code and area code as the key and "value" as the value.

SQL, :

put(Integer, Map<Integer,Double>)   Map<Integer,Map<Integer,Double>> (Integer, Double)

:

 while(oResult.next())

      retailPrices.put((new Integer(oResult.getString("AREA"))), (codes.put(new Integer(oResult.getString("ZONE_CODE")), new Double(oResult.getString("VALUE")))));


        }

, . ?

+3
3

, , , :

public class Area
{
    private int areaId;
    private List<Zone> zones;

    // other code here
}

public class Zone
{
    private int zoneId;
    private double value;

    // other code here
}

, List <Area> . , .

Java - - . .

+4

, ,

put (Integer, Map) Map (Integer, Double)

, , retailPrices,

(new Integer(oResult.getString("AREA")))

(a Integer),

pegPlPrices.put(new Integer(...), new Double(...))

.

put Map

 V put(K key, V value)

, , , . , pegPlPrices.put(Integer, Double) Double.

, , :

retailPrices.put(Integer, Double)

:

// do the put on pegPlPrices
pegPlPrices.put(new Integer(oResult.getString("ZONE_CODE")), new Double(oResult.getString("VALUE"))
// now add pegPlPrices into your map
retailPrices.put((new Integer(oResult.getString("AREA"))), pegPlPrices));

, , , . , , Enum . 1 Map s.

Enum Zone

, Enum , :

public enum Zone
{
    private Integer zoneCode;
    private Double value;

    ZONE1( /* Integer, Double */ ),
    ZONE2( /* Integer, Double */ );

    public Integer getZone()
    {
       return zoneCode;
    }
    public Double getValue()
    {
       return value;
    }

    private Zone(Integer z, Double v)
    {
       zoneCode = z;
       value = v;
    }
}
+1

, , . , :)

AreaCode zoneCode :

public class Location {
  private Integer zoneCode;
  private Integer areaCode;

  // getters and setters here

  // With an equals and hashCode (always implement BOTH or NONE)
  // they behave well in a Map, otherwise you can have the same location
  // twice if the same (zone, area) are in more than 1 object
  // NB these implementations don't like NULL areaCodes or zoneCodes
  public boolean equals(Object other) {
    if (other == null) return false;
    if (other == this) return true;

    if (other instanceof Location) {
      Location otherLocation = (Location)other;
      return (zoneCode == otherLocation.zoneCode) && (areaCode == otherLocation.areaCode);
    }
    return false;
  }

  public int hashCode() {
    return zoneCode.hashCode() + areaCode.hashCode();
  }
}

Map<Location, Double> localizedPrices = new HashMap<Location,Double>();

, :)

+1

Source: https://habr.com/ru/post/1737068/


All Articles