0

次のような (メーカー、色、タイプ) などのデータを表示/提示するにはどうすればよいですか。

List<Car> list = new ArrayList<>();          
list.add(new Car("ford", "red", "focus"));    
list.add(new Car("opel", "red", "astra"));    
list.add(new Car("opel", "blue", "astra"));    
list.add(new Car("toyota", "red", "camry"));    
list.add(new Car("honda", "blue", "civic"));    
list.add(new Car("BMW", "green", "3"));

次のようなJSFページで:

[red]
ford focus
opel astra
toyota camry

[blue]
opel astra
honda civic

[green]
BMW 3

もっと詳しい説明が必要です。データベースから来る色がわかりません。それらは不明であり、ユーザー入力に基づいており、定義されていません。これはほんの一例です。

アップデート

class Car {

private String name;
private String color;
private String model;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}
}

私の見解:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    Hello from Facelets
    <br/>
    <br/>
    <br/>
    <ui:repeat value="#{carBean.groups}" var="entry">
        <h:outputText value ="#{entry}"/>

        <br/>
        <ui:repeat value="asdasdas" var="second">
            #{second} <br/>
            <br/>
        </ui:repeat>


    </ui:repeat>

    <ui:repeat value="#{carBean.carsGroupedByColor.entrySet().toArray()}" var="entry">
        <div>
            Color: #{entry.key}
            <br />
            <ui:repeat value="#{entry.value}" var="car">
                Car name: #{car}
                <br />
            </ui:repeat>
        </div>
    </ui:repeat>



</h:body>
</html>

私の豆:

@Named
@ViewScoped
public class CarBean implements Serializable {

private Map<String, List<Car>> carsGroupedByColor;
private List<String> groups;
private List<Car> temp;
private List<Car> carsByColor;

@PostConstruct
public void init() {

    List<Car> list = new ArrayList<>();
    Car car = new Car();
    car.setColor("red");
    car.setModel("camry");
    car.setName("redcar1");
    list.add(car);
    Car car1 = new Car();
    car1.setColor("bue");
    car1.setModel("golf");
    car1.setName("bluecar1");
    list.add(car1);
    Car car2 = new Car();
    car2.setColor("red");
    car2.setModel("astra");
    car2.setName("redcar2");
    list.add(car2);

    System.out.println(list);


    groups = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        //         System.out.println(list.get(i).getName());
        //         System.out.println(list.get(i).getColor());
        //        System.out.println(list.get(i).getModel());
        groups.add(list.get(i).getColor());

    }

    Set<String> set = new HashSet<>(groups);
    groups.clear();
    groups.addAll(set);
    carsGroupedByColor = new LinkedHashMap<>();

    Car carx;

    for (int i = 0; i < groups.size(); i++) {
        temp = new ArrayList<>();
        for (int j = 0; j < list.size(); j++) {

            if (groups.get(i).equals(list.get(j).getColor())) {
                carx = new Car();
                carx.setColor(list.get(j).getColor());
                carx.setName(list.get(j).getName());
                carx.setModel(list.get(j).getModel());

                System.out.println("Color is " + groups.get(i) + "=" + list.get(j).getColor());
                System.out.println(list.get(j).getName());
                temp.add(carx);
            }
        }
        System.out.println(temp);
        carsGroupedByColor.put(groups.get(i), temp);
        System.out.println("carsGroupedByColorcars is " + carsGroupedByColor.get(groups.get(i)));

        System.out.println("temp is clear: " + temp);

    }

    for (int i = 0; i < groups.size(); i++) {

        System.out.println(groups.get(i) + "   " + carsGroupedByColor.get(groups.get(i)));



    }
}

public Map<String, List<Car>> getCarsGroupedByColor() {
    return carsGroupedByColor;
}

public void setCarsGroupedByColor(Map<String, List<Car>> carsGroupedByColor) {
    this.carsGroupedByColor = carsGroupedByColor;
}

public List<String> getGroups() {
    return groups;
}

public void setGroups(List<String> groups) {
    this.groups = groups;
}
}
4

2 に答える 2

3

a を持ち、Map<String, List<Car>>main を反復してList<Car> allCars、それに応じて車をマップに分散させることができます (私Car#colorは aを想定してStringいます。それ以外の場合は、正しいタイプをマップ キーとして使用します)。

Java コード:

@ManagedBean
@ViewScoped
public class CarBean {

    private Map<String, List<Car>> carsGroupedByColor;

    @PostConstruct
    public void init() {
        List<Car> allCars = ...; //this can be replaced to retrieve the list the way you want/need
        Map<String, List<Car>> carsGroupedByColor = new LinkedHashMap<String, List<Car>>();
        for (Car car : allCars) {
            List<Car> carsByColor = carsGroupedByColor.get(car.getColor());
            if (carsByColor == null) {
                carsByColor = new ArrayList<Car>();
            }
            carsByColor.add(car);
        }
    }

    public Map<String, List<Car>> getCarsGroupedByColor() {
        return carsGroupedByColor;
    }
}

Facelets コード:

<ui:repeat value="#{carBean.carsGroupedByColor}" var="entry">
    <div>
        Color: #{entry.key}
        <br />
        <ui:repeat value="#{entry.value}" var="car">
            Car name: #{car.name}
            <br />
        </ui:repeat>
    </div>
</ui:repeat>
于 2013-09-20T23:18:12.727 に答える
0
HashMap<String, List> cars = new HashMap<String, List>();

List<Car> red = new ArrayList<>();
List<Car> blue = new ArrayList<>();
List<Car> green = new ArrayList<>();          
red .add(new Car("ford", "red", "focus"));    
red .add(new Car("opel", "red", "astra"));    
blue.add(new Car("opel", "blue", "astra"));    
red .add(new Car("toyota", "red", "camry"));    
blue.add(new Car("honda", "blue", "civic"));    
green.add(new Car("BMW", "green", "3"));

cars.put("red",red);
cars.put("blue",blue);
cars.put("green",green);

//now it's easy to print them by color ;)
System.out.println("[red]");
for(Car car:(List)cars.get("red"))
    System.out.println(car.getColor());
//etc
于 2013-09-20T22:53:01.540 に答える