2014-10-12 91 views
1
val descrList = cursorReal.interfaceInfo.interfaces.map { 
    case values => (values.ifIndex , values.ifName , values.ifType) 
    } 

val ipAddressList = cursorReal.interfaceIpAndIndex.filter(x=> (!x.ifIpAddress.equalsIgnoreCase("0"))).map { 
    case values => (values.ifIndex,values.ifIpAddress) 
    } 

例如,排序兩個列表通過他們的第一個元素,壓縮他們在斯卡拉

val descrList = 
    List((12,"VoIP-Null0",1), (8,"FastEthernet6",6), (19,"Vlan11",53), 
     (4,"FastEthernet2",6), (15,"Vlan1",53), (11,"GigabitEthernet0",6), 
     (9,"FastEthernet7",6), (22,"Vlan20",53), (13,"Wlan-GigabitEthernet0",6), 
     (16,"Async1",1), (5,"FastEthernet3",6), (10,"FastEthernet8",6), 
     (21,"Vlan12",53), (6,"FastEthernet4",6), (1,"wlan-ap0",24), 
     (17,"Virtual-Template1",131), (14,"Null0",1), (20,"Vlan10",53), 
     (2,"FastEthernet0",6), (18,"NVI0",1), (7,"FastEthernet5",6), 
     (29,"Virtual-Access7",131), (3,"FastEthernet1",6), (28,"Virtual-Access6",131)) 

val ipAddressList = List((21,"192.168.12.1"), (19,"192.168.11.1"), 
         (11,"104.36.252.115"), (20,"192.168.10.1"), 
         (22,"192.168.20.1")) 

在這兩個列表中第一個元素是指數,我必須明智合併這兩個列表索引。這意味着 (21,"192.168.12.1")此ipAddress應與(21,"Vlan12",53)合併並形成如下(21,"Vlan12",53,"192.168.12.1")的新列表。

回答

1
scala> descrList map {case (index, v1, v2) => 
    (index, v1, v2, ipAddressList.toMap.getOrElse(index, "empty"))} 
res0: List[(Int, String, Int, String)] = List(
(12,VoIP-Null0,1,empty), (8,FastEthernet6,6,empty), (19,Vlan11,53,192.168.11.1), 
(4,FastEthernet2,6,empty), (15,Vlan1,53,empty), (11,GigabitEthernet0,6,104.36.252.115), 
(9,FastEthernet7,6,empty), (22,Vlan20,53,192.168.20.1), (13,Wlan-GigabitEthernet0,6,empty), 
(16,Async1,1,empty), (5,FastEthernet3,6,empty), (10,FastEthernet8,6,empty), 
(21,Vlan12,53,192.168.12.1), (6,FastEthernet4,6,empty), (1,wlan-ap0,24,empty), (17,Virtual- 
Template1,131,empty), (14,Null0,1,empty), (20,Vlan10,53,192.168.10.1), (2,FastEthernet0,6,empty), 
(18,NVI0,1,empty), (7,FastEthernet5,6,empty), (29,Virtual-Access7,131,empty), 
(3,FastEthernet1,6,empty), (28,Virtual-Access6,131,empty)) 
0

首先,我建議你生成一個Map而不是List。一個Map性質有一個索引器,在你的情況下,這將是ifIndex值。

一旦你有地圖中的地方,可以使用這樣的(樣品從該其他SO Best way to merge two maps and sum the values of same key?

從雷克斯克爾: MAP1 ++ map2.map {情況下(K,V)=>ķ - >(v + map1.getOrElse(k,0))}

或者像這樣從Matthew Farwell: (map1.keySet ++ map2.keySet).map(i =>(i,map1.getOrElse i,0)+ map2.getOrElse(i,0))}。toMap

如果你不能以任何理由使用Maps,那麼查看一下你現有的項目庫,如果你有Scalaz,那麼喲你有一些可用的工具。

Scalaz:https://github.com/scalaz/scalaz

如果你有油滑的,你也有一些很好的工具來直接使用。

油滑:http://slick.typesafe.com/docs/

0

首先考慮轉換decrListMap,這樣,

val a = (for ((k,v1,v2) <- descrList) yield k -> (v1,v2)).toMap 

然後,我們可以看一下鍵ipAddressList和結塊元素融入到新的記錄,如下,

for ((k,ip) <- ipAddressList ; v = a.getOrElse(k,("none","none"))) yield (k,v._1,v._2,ip) 

因此,對於ipAddressList

res: List((21,Vlan12,53,192.168.12.1), (19,Vlan11,53,192.168.11.1), 
      (11,GigabitEthernet0,6,104.36.252.115), (20,Vlan10,53,192.168.10.1), 
      (22,Vlan20,53,192.168.20.1)) 
0

鑑於數據:

val descrList = 
    List((12, "VoIP-Null0", 1), (8, "FastEthernet6", 6), (19, "Vlan11", 53), 
    (4, "FastEthernet2", 6), (15, "Vlan1", 53), (11, "GigabitEthernet0", 6), 
    (9, "FastEthernet7", 6), (22, "Vlan20", 53), (13, "Wlan-GigabitEthernet0", 6), 
    (16, "Async1", 1), (5, "FastEthernet3", 6), (10, "FastEthernet8", 6), 
    (21, "Vlan12", 53), (6, "FastEthernet4", 6), (1, "wlan-ap0", 24), 
    (17, "Virtual-Template1", 131), (14, "Null0", 1), (20, "Vlan10", 53), 
    (2, "FastEthernet0", 6), (18, "NVI0", 1), (7, "FastEthernet5", 6), 
    (29, "Virtual-Access7", 131), (3, "FastEthernet1", 6), (28, "Virtual-Access6", 131)) 

val ipAddressList = List((21, "192.168.12.1"), (19, "192.168.11.1"), 
    (11, "104.36.252.115"), (20, "192.168.10.1"), 
    (22, "192.168.20.1")) 

合併和排序:

val addrMap = ipAddressList.toMap 

val output = descrList 
    .filter(x => addrMap.contains(x._1)) 
    .map(x => x match { case (i, a, b) => (i, a, b, addrMap(i)) }) 
    .sortBy(_._1) 

output foreach println 

輸出:

(11,GigabitEthernet0,6,104.36.252.115) 
(19,Vlan11,53,192.168.11.1) 
(20,Vlan10,53,192.168.10.1) 
(21,Vlan12,53,192.168.12.1) 
(22,Vlan20,53,192.168.20.1) 
相關問題