Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:12:35+00:00 2026-06-11T06:12:35+00:00

I have an object collection created in this way. What I need to do

  • 0

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
    **********************
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:12:36+00:00Added an answer on June 11, 2026 at 6:12 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that has a child collection: class Person { public string
I have an object Document with nested Properties (Name, Value) collection. Now I want
I have a collection companies. Every object in this collection has lastcall-param. It's a
I have the following code to get the object type of a collection: Dim
I have a number of enums and need to get them as List<string> objects
I have a collection of objects in JavaScript like this: Object collection = new
Dear all, this is going to be tough: I have created a game object
I have the following class... public class Product { public int Id { get;
I have a json object collection of geo locations that I build in the
I have a Customer object which has a collection of ContactNumbers. Is it possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.