• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap example issue  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying below example

http://www.vogella.com/tutorials/JavaCollections/article.html




i am getting compilation error from eclipse with ->

I tried to separate to two different lines still getting error. How to resolve it so that i can run this example on eclips. please advise

Also in below example


what is happening in below two methods?



why map.keySet() used 2 times in above method and once in below method. I see they are only dealing with key only not value at all?

please advise

why are they passing .size() to toArray() method as below






To process every element a map you can use the forEach method, which take a lambda as parameter.



Java 8 introduces the getOrDefault() method, which allows to get the current value and if this is not found in the map to return a default value.



If you want to add a new entry automatically to a map if it is not present you can use the computeIfAbsent method to calculate a value and add it to the map.


i was not clear on above three statements. what lambda?
what is default value returned by getOrDefault() method?
computeIfAbsent () what it calculates and how it adds?


Above code not compiling asking to cast with Map and when i cast even then compilation errors. please advise

Below code giving errors at ->

please advise
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are examples using Java 8. It appears the Eclipse version you have either does not support java 8, or you are not using a java 8 compiler

What version of Eclipse have you got installed?
Have you got Java 8 installed?

See http://eclipsesource.com/blogs/2014/03/25/eclipse-support-for-java-8/
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));


I haven't looked through the full body, but one thing that did jump out at me was the above line. How many placeholders do you have verses number of arguments? Could you change the format of the placeholders? Now there might be some nuance with lambdas that I don't know about, and not having yet studied these I can't advise if that is the case, but am assuming they won't alter the syntax for printf.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loo at Map#forEach. It says it takes a BiConsumer object as its parameter.

So look at BiConsumer. It is marked @FunctionalInterface which means it has one method. So we look for its methods and we find accept(T, U) and andThen(BiConsumer), so you are going to start worrying about my arithmetic. Close examination of andThen reveals that it is “default”, so it has already been implemented so it doesn't count. That makes one method which needs to be implemented, and that is what matters. If you look up FunctionalInterface, you find that a functional interface can be implemented as a λ (or otherwise).

So, when you try to write a λ from a BiConsumer, the compiler will assume you want an unimplemented (=abstract) method, so it will assume you want accept. So what you can do is miss out the name of the class(or interface) and the name of the method, but you do have to handle its parameters.
What you do is to give the two parameters names (in this case they were called k and v and anybody who has used Maps will know why those two were chosen), and provide a body for the method.
To wrote a λ you need to provide the variables (if any, and here we know you need 2 variables), and the method body. You are allowed to write anything sensible in the method body, but here all it was is to print the two parameters using their toString method. You can confirm that %s uses toString from the Formatter documentation (scroll down about 8734583 pages). And to complete the λ all you need is the arrow token to join the two parts together:-
(k, v) -> System.out.printf("%s %s%n", k, v); QED
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can give the two parameters any permissible names for parameters as long as they are different.
The printf call is included in the method body, so it uses its normal form.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Bye wrote:
sai rama krishna wrote:map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));


...How many placeholders do you have verses number of arguments? ...


The "%n" isn't a place holder per se but a newline. It's better than "\n" because it is OS sensitive.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote: . . .
i am getting compilation error from eclipse with ->
. . .
It is no good telling us you get an error; you must provide the details. An important detail (probably the most important) is whether you are using Java8 or not.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using IBM 8.5.1 as IDE.
I went to Project---> Properties--->Compiler--->shows 1.7 as attached.

How do i upgrade (as this is office laptop i am using, i may not have freedom to upgrade). I will try at home laptop by checking java there and let you all know how it goes.
Lambda mean ->

This is something new which i never come across or used. Where i can read more and find example on it. Please advise
Compiler.png
[Thumbnail for Compiler.png]
compiler
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday, I wrote:Loo at Map#forEach. It says . . .
Only “Loo” should have read “Look”

And I pushed the submit button rather than preview, so I am editing this post with what I should have said.

Take another look at that link; at the end of method decsription it says “Since … 1.8” or similar. That tells you which the earliest version of Java® that method occurs in.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Eclipse, go to Windows -> Preferences -> Java -> Installed JREs and install a 1.8 JRE. Then you can go to Project -> Preferences and change the compiler.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latest edition of the Java Tutorials has been updated to include lambdas, Streams etc.
Go to the ”Features new in Java 8“ forum and search for book promos. Look for *Welcome at the beginning of the thread title. those threads include the announcement of the promotion. you should find three, maybe more. 1 2 3 4 5 6 7.

Well, I said there might be more than 3!

Each of those posts has a link to the book on Amazon or similar. I had the good fortune to win Urma Fusco and Mycroft and like it. Have a look at those seven books, too.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will look. There are lot of books on this
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!