2012-11-14 122 views
0

將自定義對象的HashMaps添加到ArrayList時遇到問題。在這裏,你是HashMaps這樣的定義:如何將自定義對象的HashMaps添加到ArrayList中

static Map<Long, Supermarket> supermarkets = new HashMap<Long, Supermarket>(); 
static Map<Long, Product> products = new HashMap<Long, Product>(); 

這是ArrayList的定義:

static ArrayList<HashMap> supemarketsAndProducts = new ArrayList<HashMap>(); 

我也用同樣的結果列表嘗試。 產品是創建產品對象的類。 超市是創建超市對象的類。

問題是添加元素到ArrayList。我想在這條路上:

supemarketsAndProducts.add(supermarkets); 
supemarketsAndProducts.add(products); 

我需要做那些包含HashMap(超市和產品),因爲我需要這個類返回既包含HashMap結構的結構。我究竟做錯了什麼?任何解決方案/解決方法來返回我需要的hashmaps?

感謝和問候。

編輯︰在這裏你是完整的代碼(但它太長了:它是一個自定義的SAX解析器,從另一個類調用,變量名稱是西班牙語)。

package parsers; 

import java.io.*; 
import java.util.*; 
import java.math.BigDecimal; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.XMLReader; 
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 
import org.xml.sax.helpers.*; 
import org.xml.sax.SAXParseException; 
import org.xml.sax.ErrorHandler; 
import org.xml.sax.InputSource; 

public class LectorXML extends DefaultHandler { 

    static Map<Long, Supermercado> supermercados = new HashMap<Long, Supermercado>(); 
    static Map<Long, Producto> productos = new HashMap<Long, Producto>(); 
    static List<Map> supermercadosYproductos = new ArrayList<Map>(); 
    static Long numeroSupermercado = 0L; 
    static Long numeroProducto = 0L; 

    private final XMLReader xr; 
    boolean bfname = false; 
    boolean blname = false; 
    boolean bnname = false; 
    boolean bsalary = false; 
    boolean bs_id = false; 
    boolean bnombre = false; 
    boolean bdireccion = false; 
    boolean bpoblacion = false; 
    boolean bprovincia = false; 
    boolean blongitud = false; 
    boolean blatitud = false; 
    boolean bfavorito = false; 
    boolean btelefono = false; 
    boolean bp_id = false; 
    boolean bcodigobarras = false; 
    boolean bnombrep = false; 
    boolean bfabricante = false; 
    boolean bcategoria_1 = false; 
    boolean bcategoria_2 = false; 
    boolean bcategoria_3 = false; 
    boolean bprecio_1 = false; 
    boolean bfecha_1 = false; 
    boolean bprecio_2 = false; 
    boolean bfecha_2 = false; 
    boolean bsupermercado = false; 
    boolean bdireccion_supermercado = false; 
    boolean bpoblacion_supermercado = false; 
    boolean bcomprar = false; 

    public LectorXML() throws SAXException { 
    xr = XMLReaderFactory.createXMLReader(); 
    xr.setContentHandler(this); 
    xr.setErrorHandler(this); 
    } 

    public void leer(final String archivoXML) 
      throws FileNotFoundException, IOException, SAXException { 
    FileReader fr = new FileReader(archivoXML); 
    xr.parse(new InputSource(fr)); 
    } 

// @Override 
// public void startDocument() { 
// System.out.println("Comienzo del Documento XML"); 
// } 
// @Override 
// public void endDocument() { 
// System.out.println("Final del Documento XML"); 
// } 
    @Override 
    public void startElement(String uri, String name, 
      String qName, Attributes attributes) throws SAXException { 
// System.out.println("tElemento: " + name); 
//   System.out.println("Start Element :" + qName); 

    for (int i = 0; i < attributes.getLength(); i++) { 
//   System.out.println("qName: ------ " + qName + " ---------"); 
//   System.out.println("ttAtributo: " 
//     + attributes.getLocalName(i) + " = " + attributes.getValue(i)); 
     if (attributes.getValue(i).equalsIgnoreCase("s_id")) { 
     bs_id = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("nombre")) { 
//    System.out.println("Nombre del supermercado: " + attributes.getValue(i)); 
     bnombre = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("direccion")) { 
//    System.out.println("Dirección del supermercado: " + attributes.getValue(i)); 
     bdireccion = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("poblacion")) { 
//    System.out.println("Población del supermercado: " + attributes.getValue(i)); 
     bpoblacion = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("provincia")) { 
//    System.out.println("Provincia del supermercado: " + attributes.getValue(i)); 
     bprovincia = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("longitud")) { 
//    System.out.println("Longitud del supermercado: " + attributes.getValue(i)); 
     blongitud = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("latitud")) { 
//    System.out.println("Latitud del supermercado: " + attributes.getValue(i)); 
     blatitud = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("favorito")) { 
//    System.out.println("¿Supermercado favorito?: " + attributes.getValue(i)); 
     bfavorito = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("telefono")) { 
//    System.out.println("Teléfono del supermercado: " + attributes.getValue(i)); 
     btelefono = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("p_id")) { 
//    System.out.println("Id del producto: " + attributes.getValue(i)); 
     bp_id = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("codigobarras")) { 
//    System.out.println("Código de barras del producto: " + attributes.getValue(i)); 
     bcodigobarras = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("nombrep")) { 
//    System.out.println("Nombre del producto: " + attributes.getValue(i)); 
     bnombrep = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("fabricante")) { 
//    System.out.println("Fabricante del producto: " + attributes.getValue(i)); 
     bfabricante = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("categoria_1")) { 
//    System.out.println("Categoría del producto: " + attributes.getValue(i)); 
     bcategoria_1 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("categoria_2")) { 
//    System.out.println("Categoría 2 del producto: " + attributes.getValue(i)); 
     bcategoria_2 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("categoria_3")) { 
//    System.out.println("Categoría 3 del producto: " + attributes.getValue(i)); 
     bcategoria_3 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("precio_1")) { 
//    System.out.println("Precio del producto: " + attributes.getValue(i)); 
     bprecio_1 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("fecha_1")) { 
//    System.out.println("Fecha del producto: " + attributes.getValue(i)); 
     bfecha_1 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("precio_2")) { 
//    System.out.println("Precio 2 del producto: " + attributes.getValue(i)); 
     bprecio_2 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("fecha_2")) { 
//    System.out.println("Fecha 2 del producto: " + attributes.getValue(i)); 
     bfecha_2 = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("supermercado")) { 
//    System.out.println("Nombre del supermercado del producto: " + attributes.getValue(i)); 
     bsupermercado = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("direccion_supermercado")) { 
//    System.out.println("Dirección del supermercado del producto: " + attributes.getValue(i)); 
     bdireccion_supermercado = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("poblacion_supermercado")) { 
//    System.out.println(" Población del supermercado del producto: " + attributes.getValue(i)); 
     bpoblacion_supermercado = true; 
     } 
     if (attributes.getValue(i).equalsIgnoreCase("comprar")) { 
//    System.out.println(" Comprar el producto? " + attributes.getValue(i)); 
     bfavorito = true; 
     } 
    } 
    } 

// @Override 
// public void endElement(String uri, String name, 
//   String qName) throws SAXException { 
// System.out.println("tFin Elemento: " + name); 
// } 
    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 
// System.out.println(new String(ch, start, length)); 

    Supermercado supermercado = new Supermercado(); 
    Producto producto = new Producto(); 

    if (bs_id) { 
     String idSupermercado = new String(ch, start, length); 
     System.out.println("Id del supermercado: " + idSupermercado); 
     bs_id = false; 
     supermercado.supermercadoId = Long.parseLong(idSupermercado); 
    } 
    if (bnombre) { 
     String nombreSupermercado = new String(ch, start, length); 
     System.out.println("Nombre del supermercado: " + nombreSupermercado); 
     bnombre = false; 
     supermercado.nombre = nombreSupermercado; 
    } 
    if (bdireccion) { 
     String direccionSupermercado = new String(ch, start, length); 
     System.out.println("Dirección del supermercado: " + direccionSupermercado); 
     bdireccion = false; 
     supermercado.direccion = direccionSupermercado; 
    } 
    if (bpoblacion) { 
     String poblacionSupermercado = new String(ch, start, length); 
     System.out.println("Población del supermercado: " + poblacionSupermercado); 
     bpoblacion = false; 
     if (poblacionSupermercado.equalsIgnoreCase("")) 
     supermercado.poblacion = null; 
     else supermercado.poblacion = poblacionSupermercado; 
    } 
    if (bprovincia) { 
     String provinciaSupermercado = new String(ch, start, length); 
     System.out.println("Provincia del supermercado: " + provinciaSupermercado); 
     bprovincia = false; 
     supermercado.provincia = provinciaSupermercado; 
    } 
    if (blongitud) { 
     String longitudSupermercado = new String(ch, start, length); 
     System.out.println("Longitud del supermercado: " + longitudSupermercado); 
     blongitud = false; 
     if (longitudSupermercado.equalsIgnoreCase("0")) 
     supermercado.longitud = null; 
     else supermercado.longitud = Double.parseDouble(longitudSupermercado); 
    } 
    if (blatitud) { 
     String latitudSupermercado = new String(ch, start, length); 
     System.out.println("Latitud del supermercado: " + latitudSupermercado); 
     blatitud = false; 
     if (latitudSupermercado.equalsIgnoreCase("0")) 
     supermercado.latitud = null; 
     else supermercado.latitud = Double.parseDouble(latitudSupermercado); 
    } 
    if (bfavorito) { 
     String supermercadoFavorito = new String(ch, start, length); 
     System.out.println("Supermercado favorito? " + supermercadoFavorito); 
     bfavorito = false; 
     if (supermercadoFavorito.equals("0")) { 
     supermercado.favorito = false; 
     } else if (supermercadoFavorito.equals("1")) { 
     supermercado.favorito = true; 
     } 
    } 
    if (btelefono) { 
     String telefonoSupermercado = new String(ch, start, length); 
     System.out.println("Teléfono del supermercado: " + telefonoSupermercado); 
     btelefono = false; 
     if (telefonoSupermercado.equalsIgnoreCase("0")) 
     supermercado.telefono = null; 
     else supermercado.telefono = telefonoSupermercado; 
    } 
    if (bp_id) { 
     String idProducto = new String(ch, start, length); 
     System.out.println("Id del producto: " + idProducto); 
     bp_id = false; 
     producto.productoId = Long.parseLong(idProducto); 
    } 
    if (bcodigobarras) { 
     String codigoBarras = new String(ch, start, length); 
     System.out.println("Código de barras del producto: " + codigoBarras); 
     bcodigobarras = false; 
     if (codigoBarras.equalsIgnoreCase("0")) 
     producto.codigobarras = null; 
     else producto.codigobarras = codigoBarras; 
    } 
    if (bnombrep) { 
     String nombreProducto = new String(ch, start, length); 
     System.out.println("Nombre del producto: " + nombreProducto); 
     bnombrep = false; 
     producto.nombre = nombreProducto; 
    } 
    if (bfabricante) { 
     String fabricanteProducto = new String(ch, start, length); 
     System.out.println("Fabricante del producto: " + fabricanteProducto); 
     bfabricante = false; 
     if (fabricanteProducto.equalsIgnoreCase("")) 
     producto.fabricante = null; 
     else producto.fabricante = fabricanteProducto; 
    } 
    if (bcategoria_1) { 
     String categoria1 = new String(ch, start, length); 
     System.out.println("Categoría del producto: " + categoria1); 
     bcategoria_1 = false; 
     producto.categoria_1 = categoria1; 
    } 
    if (bcategoria_2) { 
     String categoria2 = new String(ch, start, length); 
     System.out.println("Categoría 2 del producto: " + categoria2); 
     bcategoria_2 = false; 
     if (categoria2.equalsIgnoreCase("")) 
     producto.categoria_2 = null; 
     else producto.categoria_2 = categoria2; 
    } 
    if (bcategoria_3) { 
     String categoria3 = new String(ch, start, length); 
     System.out.println("Categoría 3 del producto: " + categoria3); 
     bcategoria_3 = false; 
     if (categoria3.equalsIgnoreCase("")) 
     producto.categoria_3 = null; 
     else producto.categoria_3 = categoria3; 
    } 
    if (bprecio_1) { 
     String precio1 = new String(ch, start, length); 
     System.out.println("Precio del producto: " + precio1); 
     bprecio_1 = false; 
     if (precio1.equalsIgnoreCase("0")) 
     producto.precio_1 = null; 
     else producto.precio_1 = Double.parseDouble(precio1); 
    } 
    if (bfecha_1) { 
     String fecha1 = new String(ch, start, length); 
     System.out.println("Fecha del producto: " + fecha1); 
     bfecha_1 = false; 
     if (fecha1.equalsIgnoreCase("0")) 
     producto.fecha_1 = null; 
     else producto.fecha_1 = ConvertirFecha.conversion(fecha1); 
    } 
    if (bprecio_2) { 
     String precio2 = new String(ch, start, length); 
     System.out.println("Precio 2 del producto: " + precio2); 
     bprecio_2 = false; 
     if (precio2.equalsIgnoreCase("0")) 
     producto.precio_2 = null; 
     else producto.precio_2 = Double.parseDouble(precio2); 
    } 
    if (bfecha_2) { 
     String fecha2 = new String(ch, start, length); 
     System.out.println("Fecha 2 del producto: " + fecha2); 
     bfecha_2 = false; 
     if (fecha2.equalsIgnoreCase("0")) 
     producto.fecha_2 = null; 
     else producto.fecha_2 = ConvertirFecha.conversion(fecha2); 
    } 
    if (bsupermercado) { 
     String nombreSupermercadoProducto = new String(ch, start, length); 
     System.out.println("Nombre del supermercado del producto: " + nombreSupermercadoProducto); 
     bsupermercado = false; 
     producto.supermercado = nombreSupermercadoProducto; 
    } 
    if (bdireccion_supermercado) { 
     String direccionSupermercadoProducto = new String(ch, start, length); 
     System.out.println("Dirección del supermercado del producto: " + direccionSupermercadoProducto); 
     bdireccion_supermercado = false; 
     producto.direccion_supermercado = direccionSupermercadoProducto; 
    } 
    if (bpoblacion_supermercado) { 
     String poblacionSupermercadoProducto = new String(ch, start, length); 
     System.out.println("Población del supermercado del producto: " + poblacionSupermercadoProducto); 
     bpoblacion_supermercado = false; 
     if (poblacionSupermercadoProducto.equalsIgnoreCase("")) 
     producto.poblacion_supermercado = null; 
     else producto.poblacion_supermercado = poblacionSupermercadoProducto; 
    } 
    if (bcomprar) { 
     String comprarProducto = new String(ch, start, length); 
     System.out.println(" Comprar el producto? " + comprarProducto); 
     bcomprar = false; 
     if (comprarProducto.equals("0")) { 
     producto.comprar = false; 
     } else if (comprarProducto.equals("1")) { 
     producto.comprar = true; 
     } 
    } 
    if (supermercado.supermercadoId != null) 
     supermercados.put(numeroSupermercado, supermercado); 
    if (producto.productoId != null) 
     productos.put(numeroProducto, producto); 
    } 
    supermercadosYproductos.add(supermercados); 
    supermercadosYproductos.add(productos); 
} 
+0

你面臨什麼問題?請說明清楚。並請發佈'真實代碼'。 –

+0

從超市和產品中刪除泛型。所以你的列表將是通用的接受hashmaps! – madhairsilence

+0

您是否可以不返回類似'SupermarketStock'或類似物品的物品,將「超市」與某些「產品」結合? – Edd

回答

0

那麼,最後我自己解決了這個問題。 的定義是確定的(原代碼,沒有翻譯變量不浪費更多的時間哈哈):

static List<Map> supermercadosYproductos = new ArrayList<Map>(); 

的問題是,我是要來挽救產品和超市在錯誤的地方:它應該是我的public List<Map> leer(final String archivoXML)這種方法:

if (!supermercadosYproductos.isEmpty()) { 
    supermercadosYproductos.add(supermercados); 
    supermercadosYproductos.add(productos); 
    return supermercadosYproductos; 
} 
else return null; 
1

必須使用通配符來覆蓋要添加不同的地圖進入名單的事實:

static List<Map<Long, ?>> supemarketsAndProducts = new ArrayList<>(); 

當然,當你檢索從這個列表中的對象,你會不會有靜態類型。這是不可能實現的。您需要執行未經檢查的演員表,並確保您的類型安全。

+0

它不支持jdk1.6,只有1.7。 1.6的想法?謝謝。 – Alberto

+0

只需從左側複製類型參數到右側。 –

+0

它不起作用。我得到:'預計''包supemarketsAndProducts不存在'當我嘗試這樣保存'supemarketsAndProducts.add(0L,超市);'。聲明是'static List > supemarketsAndProducts = new ArrayList >();' – Alberto

相關問題