Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to iterate through a HashMap in Xtend?
How to iterate through a HashMap in Xtend? [message #792949] Tue, 07 February 2012 15:45 Go to next message
Soenke Brightside is currently offline Soenke BrightsideFriend
Messages: 41
Registered: January 2011
Member
Hi,

As the title says: How can i iterate through a Hashmap in Xtend?

Sönke
Re: How to iterate through a HashMap in Xtend? [message #792995 is a reply to message #792949] Tue, 07 February 2012 16:42 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Am 07.02.12 16:45, schrieb Soenke Escher:
> Hi,
>
> As the title says: How can i iterate through a Hashmap in Xtend?
>
> Sönke

Sönke,

for(e: map.entrySet) {
..
}

should do the trick.

Alternatively you could use

map.forEach [key, value | ..]


Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: How to iterate through a HashMap in Xtend? [message #793585 is a reply to message #792995] Wed, 08 February 2012 10:09 Go to previous messageGo to next message
Soenke Brightside is currently offline Soenke BrightsideFriend
Messages: 41
Registered: January 2011
Member
Thank you Sebastian,

i already tryed this but the problem is the e/key/value has the type Object and i cannot cast them to Map.Entry.

"Couldn't resolve reference to JvmType 'Map.Entry'."

Sönke

[Updated on: Wed, 08 February 2012 10:11]

Report message to a moderator

Re: How to iterate through a HashMap in Xtend? [message #793595 is a reply to message #793585] Wed, 08 February 2012 10:24 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Sönke,

how's your map defined / instantiated?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 08.02.12 11:09, schrieb Soenke Escher:
> Thank you Sebastian,
>
> i already tryed this but the problem is the e/key/value has the type
> Object and i cannot cast them to Map.Entry.
>
> Sönke
>
>
Re: How to iterate through a HashMap in Xtend? [message #793596 is a reply to message #793595] Wed, 08 February 2012 10:28 Go to previous messageGo to next message
Soenke Brightside is currently offline Soenke BrightsideFriend
Messages: 41
Registered: January 2011
Member
Hi,

var hmap = new HashMap<SystemComponent, List<SystemComponent>>();

Sönke
Re: How to iterate through a HashMap in Xtend? [message #793609 is a reply to message #793596] Wed, 08 February 2012 10:43 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3108
Registered: July 2009
Senior Member
Sönke,

this works for me:

import java.util.HashMap
import java.util.List

class X {
def forLoopMap() {
var hmap = new HashMap<String, List<String>>();
for(e: hmap.entrySet) {
val String key = e.key
val String firstValue = e.value.head
// to avoid unused variable warnings
if (key == firstValue)
return
}
}

def forEachMap() {
var hmap = new HashMap<String, List<String>>();
hmap.forEach [ k, v |
val String key = k
val String firstValue = v.head
// to avoid unused variable warnings
if (key == firstValue)
return
]
}
}

Which version of Xtend do you use?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 08.02.12 11:28, schrieb Soenke Escher:
> Hi,
>
> var hmap = new HashMap<SystemComponent, List<SystemComponent>>();
>
> Sönke
Re: How to iterate through a HashMap in Xtend? [message #793617 is a reply to message #793609] Wed, 08 February 2012 10:55 Go to previous messageGo to next message
Soenke Brightside is currently offline Soenke BrightsideFriend
Messages: 41
Registered: January 2011
Member
Hi,

I use Xtend2 2.2.1. Your code runs fine.

Here a code-snippet of mine:


def dispatch compile(ProjectRepository pr, IFileSystemAccess fsa) {
for(e:pr.hashMap.entrySet){
// e is type of object. cant do anything with it.
}
}

def HashMap getHashMap(ProjectRepository pr){
var hmap = new HashMap<SystemComponent, List<SystemComponent>>();
//do something
return hmap;
}

Re: How to iterate through a HashMap in Xtend? [message #793620 is a reply to message #793617] Wed, 08 February 2012 10:58 Go to previous message
Soenke Brightside is currently offline Soenke BrightsideFriend
Messages: 41
Registered: January 2011
Member
Got it. I had to change

def HashMap getHashMap(ProjectRepository pr){
to
def HashMap<SystemComponent, List<SystemComponent>> getHashMap(ProjectRepository pr){
Previous Topic:How to limit content assist suggestion to same file
Next Topic:Writing unit tests for code written in my DSL
Goto Forum:
  


Current Time: Fri Jun 05 09:39:11 GMT 2020

Powered by FUDForum. Page generated in 0.01930 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top