1

I am having one problem in java arraylist. I am good in databases :) We normally use "group by" to group rows. I want the same thing but in java for one of my project I have following format in arraylist

name1:val1
name1:val2
name1:val3
name2:val8
name2:val7
name7:val54
name7:val76
name7:val34

I want to convert this arraylist to give me following output:

-name1
  val1
  val2
  val3
-name2
  val8
  val7
-name7
    .
    .
    .
  val34

this is not a school assignment :). may be for some of Java Guru it looks like a small thing to do.

1
  • it is one entry in arraylist "name7:val54" not two :) Commented Jun 22, 2010 at 0:33

4 Answers 4

2

I like to do that kind of thing with a map.

import java.util.*;

public class DoIt {
    public static void main(String[] args) {
         List l = new ArrayList<String>();
         l.add("name1:val1");
         l.add("name1:val2");
         l.add("name1:val3");
         l.add("name1:val4");
         l.add("name2:val1");
        Map results = new HashMap<String,String>();
        for (Iterator i = l.iterator(); i.hasNext();) {
             String s = (String)i.next();
             String[] tmp = s.split(":");
             if (!results.containsKey(tmp[0])) {
                 System.out.println("-"+tmp[0]+"\n"+tmp[1]);
                results.put(tmp[0], tmp[1]);
             } else {
                 System.out.println(tmp[1]);
             }
         }
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

WOW you are awesome ! Let me try and I will get back to you :)
1

Use a Map<String, List<Integer>>. You could use the following snippets:

// declare
Map<String, List<Integer>> m = new HashMap<String, List<Integer>>();

// insert into the structure the pair 'a':2 
List<Integer> l = m.get("a");
if ( l == null ) {
  l = new ArrayList<Integer>();
  m.put("a", l);
}
l.add(2);

// iterate over the values
for (Map<String, List<Integer>>.Entry e : m) {
  System.out.println("-" + e.getKey());
  for (Integer i : m.getValue()) {
    System.out.println(" " + i);
  }
}

Comments

0

What you are looking for is called multi-map.

There is no standard multi-map in java.util, but Google collections has implemented it -> here. The project home page is here

Comments

0

Use Comparator

List<Samp> bla = new ArrayList<Samp>();

Collections.sort(bla, new Comparator<Samp>() {
  @Override
    public int compare(Samp s1, Samp s2) {
       return s1.getCategory().compareTo(s2.getCategory());
    }
});

then Create 1 List .,. compare if that list already contains the category, if not, add Category and Name, else just add Name. see code below :)

List<String> catList = new ArrayList<String>();

for(Samp s : bla){
   if (!catList.contains(s.getCategory())){
       catList.add(s.getCategory());
       System.out.println(s.getCategory() + " - ");     
       System.out.println(s.getName());
    } else {
       System.out.println(s.getName());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.