user238384
user238384

Reputation: 2466

java arraylist format

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.

Upvotes: 1

Views: 11709

Answers (4)

Clint Barbosa
Clint Barbosa

Reputation: 71

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());
    }
}

Upvotes: 0

Jubal
Jubal

Reputation: 8717

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]);
             }
         }
     }
}

Upvotes: 2

Ricardo Marimon
Ricardo Marimon

Reputation: 10687

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);
  }
}

Upvotes: 1

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

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

Upvotes: 0

Related Questions