开发者

Java SortedMap implemented with List of values

I want to have a Sorted map as follows:

srcAddr, dstAddr, srcPort, dstPort, protocol as keys

and a List of values as

packetLength, timeArrival for each key.

Is it possible to implement them in separate classes? I am confused if it will work this way.

Update:

I am getting an error indicating im not overriding abstract method compareTo(). Can you help me with it?

package myclassifier;
import java.io.Serializable;
import java.util.*;

public class Flows implements Serializable, Comparable {

    String srcAddr, dstAddr, srcPort, dstPort, protocol;

    public int compareTo(Flows other) {
        int res = this.srcAddr.compareTo(other.srcAddr);
        
        if (res != 0) {
            return res;
        }
        
        res = this.dstAddr.compareTo(other.dstAddr);
        if (res != 0) {
            return res;
        }
        
        res = this.srcPort.compareTo(other.srcPort);
        if (res != 0) {
            return res;
        }
        
        res = this.dstPort.compareTo(other.dstPort);
        if (res != 0) {
            return res;
        }
        
        return this.protocol.compareTo(other.protocol);

      
    }

    @Override
    public int hashCode() {

        final int prime = 31;
        int result = 1;
        result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
        result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
        result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
        result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode());
        return result;

    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;

        if (getClass() != obj.getClass())
            return false;

        Flows other = (Flows) obj;
        
        if (dstAddr == null) {
            if (other.dstAddr != null)
                return false;
        } else if (!dstAddr.equals(other.dstAddr))
            return false;

        if (dstPort == null) {
            if (other.dstPort != null)
                return false;
        } else if (!dstPort.equals(other.dstPort))开发者_StackOverflow
            return false;

        if (srcAddr == null) {
            if (other.srcAddr != null)
                return false;
        } else if (!srcAddr.equals(other.srcAddr))
            return false;

        if (srcPort == null) {
            if (other.srcPort != null)
                return false;
        } else if (!srcPort.equals(other.srcPort))
            return false;

        return true;
    }

}


You can write a, say a 'MyKey' class with srcAddr, dstAddr, srcPort, dstPort and protocol as member variables of it. You have to carefully override the equals and hashCode method of this class. Also this class has to implement the Comparable interface to indicate how your ordering will be determined based on member fields.

You can implement a class MyValue to have packetLength, timeArrival etc as members. This will be the value you want to store in your map.

Use TreeMap to store MyValue against MyKey.


you can implement different classes one for the key

public class Packet implements Serializable, Comparable {

String srcAddr, dstAddr, srcPort, dstPort;


public int compareTo(Object arg0) {
    // your sorting logic here
    return ...;
}


@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode());
    result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
    result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode());
    result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode());
    return result;
}


@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Packet other = (Packet) obj;
    if (dstAddr == null) {
        if (other.dstAddr != null)
            return false;
    } else if (!dstAddr.equals(other.dstAddr))
        return false;
    if (dstPort == null) {
        if (other.dstPort != null)
            return false;
    } else if (!dstPort.equals(other.dstPort))
        return false;
    if (srcAddr == null) {
        if (other.srcAddr != null)
            return false;
    } else if (!srcAddr.equals(other.srcAddr))
        return false;
    if (srcPort == null) {
        if (other.srcPort != null)
            return false;
    } else if (!srcPort.equals(other.srcPort))
        return false;
    return true;
}

}

And one for the data

public class Payload {

Integer packetLength;
Date timeArrival; 

}

then when you put payload with a certain key in a sorted map it will be placed in order according to the compareTo method


You want sort your map based on its keys or values? Do you want the keys to be sorted alphabetically or in the order you've specified above? Overall it shouldn't be too difficult:

SorterMap<String, List> mySortedMap = new TreeMap<String, List>(myComparator);
mySortedMap.put("srcAddr", Arrays.asList(new Object[] {packetLengthValue, arrivalTimeValue}))

Where you should implement myComparator to sort the keys according to your requirements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜