1

I have a hashMap. Each "Value"is going to be a a list which will be mapped later on with my "Key"s. List is desired to look like this:

[length,time][length,time][length,time]

For example: Key{srcAddr=x, dstAddr=y, srcPort=12345, dstPort=80} value{(6523,0.001),(124,0.05), () , (), ...}

I just wonder how can I have a two-col arrayList.

package myclassifier;
import java.util.ArrayList;

public class FlowStatics {
int packetLength;
double timeArrival;
public FlowStatics(int pLength, double tArrival)
{
    this.packetLength = pLength;
    this.timeArrival = tArrival;


}

} and here is how I used it:

final ArrayList<FlowStatics> staticsArray = new ArrayList<FlowStatics>();
final HashMap<Flows, ArrayList> myHashMap = new HashMap<Flows, ArrayList>();

FlowStatics flowStatics = new FlowStatics(packetLength,timeArrival);
staticsArray.add(flowStatics);
myHashMap.put(flows, staticsArray);

and here is the part that I am reading it:

Iterator<Flows> iterator =  myHashMap.keySet().iterator();
while(iterator.hasNext()){
    Flows key = iterator.next();
    ArrayList value = myHashMap.get(key);
    System.out.println("Fows"+key+"----------"+"Statics"+ value);
Red Lion
  • 213
  • 3
  • 10
  • 18

2 Answers2

4

Well, your FlowStatics is the correct solution

List<FlowStatics> will give you the "two-column array list".

Update: as of your update, myHashMap.put(flows, flowStatics); is wrong. You are this putting an individual pair, rather than a list in the map. You should use:

staticsArray.add(flowStatics);
myHashMap.put(flows, staticsArray);
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • as I changed my hashMap, it doesnt work anymore at all. im not getting any error, but iterating over my hashMap takes too long and the output is the same as before. – Red Lion Aug 14 '10 at 12:48
  • i also did override toString method. – Red Lion Aug 14 '10 at 12:52
3

A List<E> is an abstraction for a homogeneous list of elements whose type is E. There are some restrictions (e.g. no primitives), but conceptually the type E can be defined to be whatever you want.

Suppose there's an abstraction of Pair<L,R>. Then a List<Pair<L,R>> is still a list of some E, but now that E is a Pair<L,R>. So it's still a "one-column" list, but each element in the list is a "pair", so it's sort of a "two-column" list.

Note that you don't always need a generic Pair<L,R>. Any type E that properly encapsulates all the information can be used in a List<E>.

And by the way, you can have a List<List<E>> too.

See also

Related questions


Returning values analogy

Often people ask "How can I return two values in Java?". The answer is analogous. You return one value, a new type which encapsulates both information.

So instead of:

// attempt to return two values
// DOES NOT COMPILE

return "James Bond";
return "007";

You do:

return new SecretAgent("James Bond", "007");

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • thanks for your comprehensive answer. I'd thought about pair too but as the number of statistical factors which I am considering may grow, that why i didnt use it. I am still wondering what is my problem, as I am adding object to an arraylist. – Red Lion Aug 14 '10 at 12:45