dynamic array list usage to iterate object Arraylist and obtain results base don object name

I have an object collection created in this way. What I need to do is to get a collection arranged by the name.

The object is

public class Object {
    private Integer id;
    private String name;
    private int value;

    public Object(Integer id, String name, int value) {
        this.id = id;
        this.name = name;
        this.value = value;
    }
        //getters,setters
}
private static List<Object> populateObjList1(){
    List<Object> objList = new ArrayList<Object> ();
    Object obj1 = new Object(1, "aa", 4);
    Object obj2 = new Object(2, "bb", 3);
    Object obj3 = new Object(3, "cc", 7);
    Object obj4 = new Object(4, "dd", 6);
    Object obj5 = new Object(1, "aa", 2);
    Object obj6 = new Object(2, "cc", 1);
    Object obj7 = new Object(3, "ee", 5);
    Object obj8 = new Object(4, "ff", 7);
    Object obj9 = new Object(1, "bb", 3);
    Object obj10 = new Object(2, "cc", 4);
    Object obj11 = new Object(3, "dd", 7);
    Object obj12 = new Object(4, "ff", 1);
    objList.add(obj1);
    objList.add(obj2);
    objList.add(obj3);
    objList.add(obj4);
    objList.add(obj5);
    objList.add(obj6);
    objList.add(obj7);
    objList.add(obj8);
    objList.add(obj9);
    objList.add(obj10);
    objList.add(obj11);
    objList.add(obj12);
    return objList;
} 

my code impl was –

public static void main (String args[]){
    List<Object> day1 = populateObjList1();
    List<String> abj = new ArrayList<String>();
    System.out.println(""+abj.size());
    for (Object object : day1) {
        if(!abj.contains(object.getName())){
            abj.add(object.getName());
        }else{
            System.out.println("available");
        }
    }
    for (String string : abj) {
        System.out.println("__ "+string);
    }   
}

    *** The expected dataset should be *****
    **********************
    |Name | ** | ** | ** |
    **********************
    aa      4     2    - 
    **********************
    bb      3     -    3
    **********************
    cc      7     1    4
    **********************
    dd      6     -    7
    **********************
    ee      -     5    -
    **********************
    ff      -     7    1
    **********************

Answer

You have to change your design. I’d like suggest to use Outer-Inner class and TreeSet.

class Foo {
 private String name;
 private TreeSet<Item> items;

 public Foo(String name) {
    this.name = name;
    items = new TreeSet<Item>();
 }
 public String getName() { return name; }
 public void setName(String name) {this.name = name;}
 public TreeSet<Item> getItems() {return items; }
 public void addItem(Integer id, Integer value) {
    items.add(new Item(id, value));
 }

 public class Item implements Comparable {
    @Override
    public int compareTo(Object arg0) {
        Item i = (Item) arg0;
        if (id == i.id)
            return 0;
        else if (id > i.id)
            return 1;
        else
            return -1;
    }

    private Integer id;
    private Integer value;

    public Item(Integer id, Integer value) {
        this.id = id;
        this.value = value;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

}

And create objects of Foo and Item,

List<Foo> list = new ArrayList<Foo>();
list.add(new Foo("aa"));
list.add(new Foo("bb"));
list.add(new Foo("cc"));
list.add(new Foo("dd"));

list.get(0).addItem(1, 4);
list.get(0).addItem(2, 2);
list.get(0).addItem(3, 3);
list.get(1).addItem(3, 1);
list.get(1).addItem(1, 1);
list.get(1).addItem(2, 8);
list.get(2).addItem(3, 3);
list.get(3).addItem(2, 10);
list.get(0).addItem(5, 10);

for (Foo foo : list) {
 System.out.print(foo.getName());
 Foo.Item[] ar = foo.getItems().toArray(new Foo.Item[0]);
 for (int i = 1, j = 0; i <= 8; i++) {
   if (j >= ar.length)
    System.out.print(" - ");
   else if (ar[j].getId() == i) {
    System.out.print(" " + ar[j].getValue());
    j++;
   } else
        System.out.print(" - ");
  }
  System.out.println();
}

Leave a Reply

Your email address will not be published. Required fields are marked *