从列表中的对象的属性创建列表

时间:2015-12-02 10:06:20

标签: java list

我希望我的标题更简洁,但我不太清楚如何描述我想要做的事情。我有一个列表,其中包含我所制作的一些类的对象。每个对象都有许多属性,可由一些getter和setter访问。我想为每个属性创建一个列表,获取每个对象的属性并将它们放入各自的列表中。

我目前正在使用以下代码执行此操作:

ArrayList<Integer> counts = new ArrayList<Integer>();
ArrayList<String> colors = new ArrayList<String>();
ArrayList<String> shapes = new ArrayList<String>();
ArrayList<String> fills = new ArrayList<String>();

for (Card card: cards) {
    counts.add(card.getCount());
    colors.add(card.getColor());
    shapes.add(card.getShape());
    fills.add(card.getFill());
}

但我想知道是否有更短,更好的方法来做到这一点?或者我是否已经“适当”了?谢谢!

2 个答案:

答案 0 :(得分:1)

你的方式看起来很好。如果使用Java 8 Streams,则可以用较少的代码行编写它,但这需要在cards列表上进行4次迭代,而不是当前的单次迭代。

例如,要创建计数列表,您可以写:

List<Integer> counts = cards.stream().map(Card::getCount).collect(Collectors.toList());

答案 1 :(得分:1)

如果您正在开发set game,这里有一个检查和完成卡片组的解决方案:

<强> Card.java:

import java.util.Locale;

public final class Card {
    public static void main(String[] args) {
        Card a = new Card(Count.ONE, Color.RED, Fill.STRIPED, Shape.DIAMOND);
        Card b = new Card(Count.TWO, Color.RED, Fill.SOLID, Shape.DIAMOND);
        Card c = completeSet(a, b);
        System.out.println(c);
    }

    public int count;
    public int color;
    public int fill;
    public int shape;

    public Card() {

    }

    public Card(Count count, Color color, Fill fill, Shape shape) {
        setCount(count);
        setColor(color);
        setFill(fill);
        setShape(shape);
    }

    // getters and setters to operate with the attribute enumerations
    public Count getCount() {
        return Count.values()[count];
    }

    public Color getColor() {
        return Color.values()[color];
    }

    public Shape getShape() {
        return Shape.values()[shape];
    }

    public Fill getFill() {
        return Fill.values()[fill];
    }

    public void setCount(Count count) {
        this.count = count.ordinal();
    }

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

    public void setShape(Shape shape) {
        this.shape = shape.ordinal();
    }

    public void setFill(Fill fill) {
        this.fill = fill.ordinal();
    }

    public static int completeAttribute(int a, int b) {
        if (a == b) {
            // attribute for each same
            return a;
        } else {
            // attribute for each different
            int c;
            for (c = 0; c < 3; c++) {
                if (c != a && c != b) {
                    break;
                }
            }
            return c;
        }
    }

    public static Card completeSet(Card a, Card b) {
        // determine missing card to make a set
        Card result = new Card();
        result.count = completeAttribute(a.count, b.count);
        result.color = completeAttribute(a.color, b.color);
        result.shape = completeAttribute(a.shape, b.shape);
        result.fill = completeAttribute(a.fill, b.fill);
        return result;
    }

    public static boolean isSet(Card a, Card b, Card c) {
        // check if it is a set by completing it
        return completeSet(a, b).equals(c);
    }

    @Override
    public boolean equals(Object that) {
        // currently unused
        if (this == that) {
            return true;
        }
        return that != null && that instanceof Card && this.equals((Card) that);
    }

    public boolean equals(Card that) {
        if (this == that) {
            return true;
        }
        return that != null
                && this.color == that.color
                && this.count == that.count
                && this.fill == that.fill
                && this.shape == that.shape;

    }

    @Override
    public int hashCode() {
        // currently unused
        int result = count;
        result = 31 * result + color;
        result = 31 * result + shape;
        result = 31 * result + fill;
        return result;
    }

    @Override
    public String toString() {
        // pretty print attributes
        String result = getCount() + " " + getColor() + " " + getFill() + " " + getShape();
        result = result.toLowerCase(Locale.ROOT);
        if(getCount() != Count.ONE) {
            result += "s";
        }
        return result;
    }

    // enumerations for pretty names
    public enum Count {
        ONE,
        TWO,
        THREE
    }

    public enum Color {
        RED,
        GREEN,
        VIOLET
    }

    public enum Shape {
        ELLIPSE,
        DIAMOND,
        TWIRL
    }

    public enum Fill {
        OPEN,
        STRIPED,
        SOLID
    }
}

输出: three red open diamonds