简体   繁体   中英

Java Pattern Matching of Tuples

I have a vector where each object is a tuple containing of types for ex: <-1,2,3,45.67>. Now i had large set of these tuples, how can i find all the patterns in these tuples, i dont know their start and end points and how many such patterns.
Need to find all different patterns and how many times they occur?

EX:

<1,2,2,68.8752808539275><-1,1,2,68.8752808539275><-1,-2,2,112.60225083387081> <-2,0,2,158.8752808539275> <1,2,2,68.8752808539275><-1,1,2,68.8752808539275><-1,-2,2,112.60225083387081>

Now I need match first three tuples with last tuples as they are identical.
How can I do it in Java?

My input is just a vector of objects, where each object is has the above fields of a class, and i dont give search pattern, it should recognize all different patterns in the vector..

I would do the following:

  • First, use a regular expression to geht the actual String, representing ONE tupel. (<[^>]+>)
  • Create a Tuppel class, that takes that in a constructor, implement the equals and hashcode method as required, and add all results to a set while iterating.

Class:

Class MyTupel{
  private int a;
  private int b;
  private int c;
  private double d;

  public MyTupel(String tupel){
    //Regex to match a,b,c,d over here: ^<(\d+),(\d+),(\d+),([^>]+)>$
  }

  public boolean equals(MyTupel another){
    return (a == another.getA() && b == another.getB() && c = another.getC() && d == another.getD())
  }

  @Override
  public int hashCode(){
     return a+b+c+ (int)Math.floor(d);
  }

  //getter and setter
}

and finally u can use it like (verbal):

  • Create List of MyTuppel (or Set)
  • foreach Match in String "<1,2,2,68.8752808539275>...."
  • Create Tuppel-Instance ( Tuppel t = new Tuppel(match) )
  • if not list contains tuppel: Add tuppel.

consider bellowed class

class Tuple<A,B,C,D>{

  public Tuple(A a,B b,C c, D d){
 ...
  }

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((a == null) ? 0 : a.hashCode());
        result = prime * result + ((b == null) ? 0 : b.hashCode());
        result = prime * result + ((c == null) ? 0 : c.hashCode());
        result = prime * result + ((d == null) ? 0 : d.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;
        Tuple other = (Tuple) obj;
        if (a == null) {
            if (other.a != null)
                return false;
        } else if (!a.equals(other.a))
            return false;
        if (b == null) {
            if (other.b != null)
                return false;
        } else if (!b.equals(other.b))
            return false;
        if (c == null) {
            if (other.c != null)
                return false;
        } else if (!c.equals(other.c))
            return false;
        if (d == null) {
            if (other.d != null)
                return false;
        } else if (!d.equals(other.d))
            return false;
        return true;
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM