• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

filters

 
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How can we Write a program, using stream to filter list of employee where age is greater than 22 and  sort on first name.

it has to be done  showcasing effectively final feature of java 8.

I just tried like below :-



but i am not sure whether it showcase java 8 features or not also not sure how could i sort it based on first name?


Thanks
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,any updates by experts please?
 
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Show us what you have got so far.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
already showed what i was able to do sofar in above example but how to compile that code and how to filter based on first name where i am stuck currently.

 
Sheriff
Posts: 21922
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Ken wrote:also not sure how could i sort it based on first name?


Start by checking out the methods available on Stream. Some of them must mention how to sort the results.
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Ken wrote:already showed . . .

Sorry. I missed that

Are you supposed to create a List<String>? You aren't doing badly, but you should take Rob's hint.
You mightn't need forEach() to print the names. You might only need System.out.println(myLiat);
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe collect into a sorted collection:


But that's a set. If you need it to be a list you could just add

 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but where to fit in my lengthy code could you please fit it in my code so that it could start giving desired results soon?


Thanks..
 
Svullo Svullosson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .collect(TreeSet::new) is to replace the .collect(...) you've already got at line 12 of your code.

If you instead stick with your current list and use Collections.sort(...),  that would go right after the .collect(...) but before printing, i.e. at line 13.

As has been suggested. you could also look into Stream.sorted(). For performance in a real life setting I would assume that collecting into a TreeSet would be best, followed by Collections.sort(...) and then Stream.sorted(), but that's just a guess.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So according to you do you mean below should give me desired output?




Thanks
 
Svullo Svullosson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was a bit sloppy writing that initial reply, it should be
 
Saloon Keeper
Posts: 3900
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not quite according to the specs that OP mentioned in his opening post. We should have a List of Employees, filtered and sorted. The use of a TreeSet is also fine, as far as I am concerned. But what we have now is just a TreeSet of names, since Employees are being mapped to their names.

What we need is a Comparator that compares Employees on the basis of their names. So, using a TreeSet:



 
Sheriff
Posts: 7602
521
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Ken wrote:So according to you do you mean below should give me desired output?


I'd just try to run and see if that gives the results you are expecting.

You need to build code in small pieces and try to make a sense of it. Quite weird you asking this actually.

Your class is named `MaximumUsingStreamMain`, and I'm wondering what it has to do with the functionality it is trying to achieve? They don't match to me.

 
Svullo Svullosson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:This is not quite according to the specs that OP mentioned in his opening post.



I started out replying with the same code you just posted, then I realized that he had, in his code, mapped employees to names so I assumed that was his actual intention and changed my reply. Hence the "edit:" note on my post.
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly don't edit posts like that after they have been replied to; I have undone the edit.
 
Svullo Svullosson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Kindly don't edit posts like that after they have been replied to; I have undone the edit.


Who says I edited it after anyone replied to it?
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The timings on the board software.
 
Svullo Svullosson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, my memory failed me since I typically never edit. The reason I did this time is it was motivated, inconsequential and documented:He asked me to put it into his code, at which point I realized the mismatch between his question and code and adapted mine and noted why. So I really don't see the problem. But that's me and I wont disturb you further.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:This is not quite according to the specs that OP mentioned in his opening post. We should have a List of Employees, filtered and sorted. The use of a TreeSet is also fine, as far as I am concerned. But what we have now is just a TreeSet of names, since Employees are being mapped to their names.

What we need is a Comparator that compares Employees on the basis of their names. So, using a TreeSet:





where should i fit it in my code so that desired functionality be well achieved?
 
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried, and what was the result?
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i tried alreeady shared in very first post of this thread ...
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope  that doesn't mean you have gone two days without doing anything about this problem.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am still confused where to fit Piet Souris's updated code in my code so that i could achieve desired results??
 
Piet Souris
Saloon Keeper
Posts: 3900
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But since this is a friendly place for everyone, I will answer Johns question, and then asking a question to John.

If you look at the code you have in your opening post, you see that you first create the List, and then do the filtering (and not yet sorting). Now, with my code snippet it is just the same: first create a List<Employee>, and when you have that List, you can invoke my code whenever you want: right after where you created the List, or later, if you need the filtering and sorting.

Now my question to you:
that code snippet of mine had the age of 22 built-in, just like that the sorting should take place on the basis of the name. That is not very flexible, to say the least. Now, can you implement this method:

And if you have that implemented, can you give an example of how to use this method?
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before i try to answer your question i tried to run your code snippet merging with my code snippet then i got below error:-



error:-F:\>javac MaximumUsingStreamMain.java
error: file not found: MaximumUsingStreamMain.java
Usage: javac <options> <source files>
use --help for a list of possible options

F:\>--help
'--help' is not recognized as an internal or external command,
operable program or batch file.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I did not add path in cmd prompt but after adding the path properly no i am getting these many errors:-

F:\Java Training\Java Programs>javac MaximumUsingStreamMain.java
MaximumUsingStreamMain.java:18: error: cannot find symbol
   public static List<Employee> createEmployeeList()
                      ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:8: error: cannot find symbol
       List<Employee> employeeList = createEmployeeList();
            ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:11: error: cannot find symbol
                 .map(Employee::getName)
                      ^
 symbol:   variable Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:20: error: cannot find symbol
       List<Employee> employeeList=new ArrayList<>();
            ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:22: error: cannot find symbol
       Employee e1=new Employee("John",21);
       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:22: error: cannot find symbol
       Employee e1=new Employee("John",21);
                       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:23: error: cannot find symbol
       Employee e2=new Employee("Martin",19);
       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:23: error: cannot find symbol
       Employee e2=new Employee("Martin",19);
                       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:24: error: cannot find symbol
       Employee e3=new Employee("Mary",31);
       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:24: error: cannot find symbol
       Employee e3=new Employee("Mary",31);
                       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:25: error: cannot find symbol
       Employee e4=new Employee("Stephan",18);
       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:25: error: cannot find symbol
       Employee e4=new Employee("Stephan",18);
                       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:26: error: cannot find symbol
       Employee e5=new Employee("Gary",26);
       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:26: error: cannot find symbol
       Employee e5=new Employee("Gary",26);
                       ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
 ^
 symbol:   class Comparator
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
            ^
 symbol:   class Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
                             ^
 symbol:   variable Comparator
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
                                                  ^
 symbol:   variable Employee
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:34: error: cannot find symbol
var filteredAndSorted = employees.stream()
                       ^
 symbol:   variable employees
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:36: error: cannot find symbol
   .collect(toCollection(() -> new TreeSet<>(comp)))
            ^
 symbol:   method toCollection(()->new Tr[...]comp))
 location: class MaximumUsingStreamMain
MaximumUsingStreamMain.java:36: error: cannot find symbol
   .collect(toCollection(() -> new TreeSet<>(comp)))
                                   ^
 symbol:   class TreeSet
 location: class MaximumUsingStreamMain
21 errors

F:\Java Training\Java Programs>
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you already know what that error message means.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no i don't know.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"cannot find symbol" --which symbol is the compiler looking for now?
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't it tell you what it is looking for? It says something after symbol.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually there are 21 such errors and each time it says something different after symbol:-

so i just renamed my program name as Employee.java
then compiled it then also got these many errors:-


F:\Java Training\Java Programs>javac Employee.java
Employee.java:5: error: class MaximumUsingStreamMain is public, should be declared in a file named MaximumUsingStreamMain.java
public class MaximumUsingStreamMain {
      ^
Employee.java:18: error: cannot access Employee
   public static List<Employee> createEmployeeList()
                      ^
 bad source file: .\Employee.java
   file does not contain class Employee
   Please remove or make sure it appears in the correct subdirectory of the sourcepath.
Employee.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
 ^
 symbol:   class Comparator
 location: class MaximumUsingStreamMain
Employee.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
                             ^
 symbol:   variable Comparator
 location: class MaximumUsingStreamMain
Employee.java:34: error: cannot find symbol
var filteredAndSorted = employees.stream()
                       ^
 symbol:   variable employees
 location: class MaximumUsingStreamMain
Employee.java:36: error: cannot find symbol
   .collect(toCollection(() -> new TreeSet<>(comp)))
            ^
 symbol:   method toCollection(()->new Tr[...]comp))
 location: class MaximumUsingStreamMain
Employee.java:36: error: cannot find symbol
   .collect(toCollection(() -> new TreeSet<>(comp)))
                                   ^
 symbol:   class TreeSet
 location: class MaximumUsingStreamMain
7 errors
 
Tim Moores
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what do you deduce from that?
 
Tim Moores
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so i just renamed my program name as Employee.java
then compiled it then also got these many errors:-

F:\Java Training\Java Programs>javac Employee.java
Employee.java:5: error: class MaximumUsingStreamMain is public, should be declared in a file named MaximumUsingStreamMain.java
public class MaximumUsingStreamMain {


So this first error message tells you that renaming the file was a mistake. The other errors are due to missing import statements.
 
Liutauras Vilda
Sheriff
Posts: 7602
521
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Well done on running and debugging your code, you seem to actually started solving a problem.

Just one note. Piet’s code is based on somewhat Java’s 9+ code, which Java version you are running on?
 
Campbell Ritchie
Marshal
Posts: 68936
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:. . . renaming the file was a mistake. . . .

Sorry, I didn't notice the bit about reniming.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F:\Java Training\Java Programs>java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

F:\Java Training\Java Programs>
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also i just changed main class name to Employee along with the notepad file name and then again recompiled but it again threw some errors like these but which import statements i am missing here:-


F:\Java Training\Java Programs>javac Employee.java
Employee.java:10: error: cannot find symbol
                 .filter(e->e.getAge()>22)
                             ^
 symbol:   method getAge()
 location: variable e of type Employee
Employee.java:11: error: invalid method reference
                 .map(Employee::getName)
                      ^
 cannot find symbol
   symbol:   method getName()
   location: class Employee
Employee.java:22: error: constructor Employee in class Employee cannot be applied to given types;
       Employee e1=new Employee("John",21);
                   ^
 required: no arguments
 found:    String,int
 reason: actual and formal argument lists differ in length
Employee.java:23: error: constructor Employee in class Employee cannot be applied to given types;
       Employee e2=new Employee("Martin",19);
                   ^
 required: no arguments
 found:    String,int
 reason: actual and formal argument lists differ in length
Employee.java:24: error: constructor Employee in class Employee cannot be applied to given types;
       Employee e3=new Employee("Mary",31);
                   ^
 required: no arguments
 found:    String,int
 reason: actual and formal argument lists differ in length
Employee.java:25: error: constructor Employee in class Employee cannot be applied to given types;
       Employee e4=new Employee("Stephan",18);
                   ^
 required: no arguments
 found:    String,int
 reason: actual and formal argument lists differ in length
Employee.java:26: error: constructor Employee in class Employee cannot be applied to given types;
       Employee e5=new Employee("Gary",26);
                   ^
 required: no arguments
 found:    String,int
 reason: actual and formal argument lists differ in length
Employee.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
 ^
 symbol:   class Comparator
 location: class Employee
Employee.java:33: error: cannot find symbol
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
                             ^
 symbol:   variable Comparator
 location: class Employee
Employee.java:33: error: invalid method reference
 Comparator<Employee> comp = Comparator.comparing(Employee::getName);
                                                  ^
 cannot find symbol
   symbol:   method getName()
   location: class Employee
Employee.java:34: error: cannot find symbol
var filteredAndSorted = employees.stream()
                       ^
 symbol:   variable employees
 location: class Employee
Employee.java:36: error: cannot find symbol
   .collect(toCollection(() -> new TreeSet<>(comp)))
            ^
 symbol:   method toCollection(()->new Tr[...]comp))
 location: class Employee
Employee.java:36: error: cannot find symbol
   .collect(toCollection(() -> new TreeSet<>(comp)))
                                   ^
 symbol:   class TreeSet
 location: class Employee
13 errors

F:\Java Training\Java Programs>
 
Tim Moores
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there are multiple different errors, but the ones about missing imports tell you specifically what symbol is unknown, as was already pointed out before.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic