Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将结果集转换为另一个具有不同结构的结果集?_Java - Fatal编程技术网

Java 将结果集转换为另一个具有不同结构的结果集?

Java 将结果集转换为另一个具有不同结构的结果集?,java,Java,我有两个类,如图所示: public class Route { public int node; public int link; public int weight; } 节点列表中的数据如下所示: node link weight ---------------------- 1 A 0 1 B 10 2 C 5 2 D 8 2 E

我有两个类,如图所示:

public class Route {
    public int node;
    public int link;
    public int weight;
}
节点列表中的数据如下所示:

node    link    weight
----------------------
1       A       0
1       B       10
2       C       5
2       D       8
2       E       12
第二类是:

public class Segment{
    public int node;
    public String linkFrom, linkTo;
    public int weight;
}
是否有一个优雅的解决方案将上面的列表转换为一个新的段列表,该列表将如下所示

node    linkfrom    linkto    weight
------------------------------------
1       A           B         10
2       C           E         25

通过按节点分组,将节点列表转换为段列表。权重是节点中这些链接的权重之和,而
linkFrom
是节点中的第一个链接,
linkTo
是组更改之前节点中的最后一个链接。

假设对于特定节点,对于不同的链接,每个链接将具有不同的节点。您可以尝试为route类添加一个函数,如
transform
和一个自定义比较器(或
compareTo
函数,并使其实现~comparable`iterface)以基于权重比较路由,我会使用自定义比较器,因为与权重进行比较似乎不是路由的自然比较

代码应该是这样的-

static class WeightComparator implements Comparator<Route>
 {
     public int compare(Route o1 , Route o2) {
         if(o1.weight > o2.weight) {
             return 1;
         }
         else if(o1.weight < o2.weight) {
             return -1;
         }
         return 1;
     }

public class Segment{
    public int node;
    public String linkFrom, linkTo;
    public int weight;

    public void transform(int node, List<Route> routes) {
        List<Route> routesWithNode = new ArrayList<Route>();
        for(Route route : routes) {
            if(route.node == node) {
                routesWithNode.add(route);
            }
        }
        Collections.sort(routesWithNode , new WeightComparator());
        this.node = node;
        this.linkFrom = routesWithNode.get(0).link;
        this.linkTo = routeWithNode.get(routesWithNode.length - 1).link;
        int tweight = 0;
        for(Route r: routeWithNode) {
            tweight = tweight + r.weight;
        }
        this.weight = tweight;
    }
}

请注意上面不是java代码,它是伪代码,您必须编写完整的代码

添加一个新类,让我们调用is Convert

public class Convert{
  public Route firstRoute;
  public Route lastRoute;
  public Segment segment;

  public Convert(){
   this.segment = new Segment();
 }
}
然后创建所有路线的地图,以标识所有路线列表中的第一个成员

public HashMap<Integer,Route> map = new HashMap<>();
public HashMap map=new HashMap();
创建转换列表以存储转换后的列表数据

public List<Convert> convertedList = new ArrayList<>();
public List convertedList=new ArrayList();
然后迭代

for(int i=0;i<routes.size();i++){
    Route route = routes.get(i);
    Convert convert = new Convert();
    if(map.get(route.node)!=null){
        convert.lastRoute = route;
        convert.segment.linkTo=String.valueOf(route.link);
    }else{
       map.put(route.node);
       convert.firstRoute = route;
       convert.segment.node=route.node;
       convert.segment.linkFrom=String.valueOf(route.link);
    }
    convert.segment.weight+=route.weight;
     convertedList.add(convert);
}


for(Convert con:convertedList)[
//Print in the required format
}

for(inti=0;i我创建了下面的代码。它非常原始

public void convert() {
        // List to hole segments
        ArrayList<Segment> segments = new ArrayList<Segment>();
        // List holding routes
        ArrayList<Route> routes = createRoutes();
        // Node id
        int startRouteNode = routes.get(0).node;
        // Start Link
        String startLink = null;
        // Link in previous node traversed
        String previousLink = null;
        // Weight
        int weight = 0;
        for (Route route : routes) {
            // If current route is on the same node as previous one
            if (startRouteNode == route.node) {
                if (startLink == null)
                    startLink = route.link;

                previousLink = route.link;
                weight += route.weight;
            } else {
                // If current route is not on previous node
                segments.add(new Segment(startRouteNode, startLink,
                        previousLink, weight));
                startRouteNode = route.node;
                startLink = previousLink;
                weight = route.weight;
            }
        }
        // Add last segment in segment list
        segments.add(new Segment(startRouteNode, startLink, previousLink,
                weight));
        System.out.println(segments);
    }

    ArrayList<Route> createRoutes() {
        ArrayList<Route> arraylist = new ArrayList<Route>();
        arraylist.add(new Route(1, "A", 0));
        arraylist.add(new Route(1, "B", 10));
        arraylist.add(new Route(2, "C", 5));
        arraylist.add(new Route(2, "D", 8));
        arraylist.add(new Route(2, "E", 12));

        return arraylist;
    }
public void convert(){
//列出孔线段
ArrayList段=新的ArrayList();
//列出等候路线
ArrayList routes=createRoutes();
//节点id
int startRouteNode=routes.get(0.node);
//起始链接
String=null;
//已遍历上一个节点中的链接
字符串previousLink=null;
//重量
整数权重=0;
用于(路线:路线){
//如果当前路由与前一个路由位于同一节点上
if(startRouteNode==route.node){
if(startLink==null)
链接=route.link;
previousLink=route.link;
重量+=路线重量;
}否则{
//如果当前路由不在上一个节点上
段。添加(新段(星形槽榫、星形槽榫、,
上一个链接,权重);
startRouteNode=route.node;
startink=先前链接;
重量=路线重量;
}
}
//在段列表中添加最后一段
段。添加(新段(startRouteNode、startink、previousLink、,
重量);
系统输出打印项次(段);
}
ArrayList createRoutes(){
ArrayList ArrayList=新的ArrayList();
添加(新路由(1,“A”,0));
添加(新路线(1,“B”,10));
添加(新路线(2,“C”,5));
添加(新路线(2,“D”,8));
新增(新路线(2,E,12);;
返回数组列表;
}

你能描述一下计算吗?你从哪里得到从B到E的链接?你如何在int link中存储“A”?此外,你确定你的第二个条目应该像
2BE25
还是像
2CE25
?应该是2BE25,因为从B到C之间的权重是5,但节点e将从1更改为2。某个特定节点的权重是否唯一?否。并且节点1的多个链接也可能在节点2之后再次出现,因此节点在列表中也不是唯一的。
public void convert() {
        // List to hole segments
        ArrayList<Segment> segments = new ArrayList<Segment>();
        // List holding routes
        ArrayList<Route> routes = createRoutes();
        // Node id
        int startRouteNode = routes.get(0).node;
        // Start Link
        String startLink = null;
        // Link in previous node traversed
        String previousLink = null;
        // Weight
        int weight = 0;
        for (Route route : routes) {
            // If current route is on the same node as previous one
            if (startRouteNode == route.node) {
                if (startLink == null)
                    startLink = route.link;

                previousLink = route.link;
                weight += route.weight;
            } else {
                // If current route is not on previous node
                segments.add(new Segment(startRouteNode, startLink,
                        previousLink, weight));
                startRouteNode = route.node;
                startLink = previousLink;
                weight = route.weight;
            }
        }
        // Add last segment in segment list
        segments.add(new Segment(startRouteNode, startLink, previousLink,
                weight));
        System.out.println(segments);
    }

    ArrayList<Route> createRoutes() {
        ArrayList<Route> arraylist = new ArrayList<Route>();
        arraylist.add(new Route(1, "A", 0));
        arraylist.add(new Route(1, "B", 10));
        arraylist.add(new Route(2, "C", 5));
        arraylist.add(new Route(2, "D", 8));
        arraylist.add(new Route(2, "E", 12));

        return arraylist;
    }