Mike London

Ranch Hand
+ Follow
since Jul 12, 2002
Cows and Likes
Cows
Total received
11
In last 30 days
0
Total given
0
Likes
Total received
33
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike London

OK, these replies are helpful. I appreciate it.

So, I could use JDOM and count matches,

---

I could also just process the file line by line and count the number of matches.

---

Or, I could use RegEx and do...

int count = 0;
while (matcher.find())
    count++;

I had thought counting would be simple to do with stream since the XML is only a simple text file.

Thanks for all the suggestions.

- mike
3 hours ago
Hello,

I'm trying the Java 8 way to stream a List and count a particular word in that list, but I keep getting zero.

Here's the basic logic I'm using:



Returns zero.

Suggestions?

Thanks,

-- mike
5 hours ago
I learned a lot. Been doing Java for 15+ years, but keep learning every day since there is just SO much there.

Lambas and declarative stuff is deceptively easy (as in easy for some things, not so easy for others.).

Appreciate all the great comments and input.

I added to the code the ability for a "Cartesian" listing -- each item of list 1 with every item of list 2 as an optional method.

Thanks again.

- mike
6 days ago
Gotcha. Thanks!

- mike
6 days ago
Yep that works and is less confusing.  As I mentioned, I found the code online a few hours ago and have been scratching my head (eyes bleeding) ever since...

6 days ago

Piet Souris wrote:Indeed, and I don't think that ListZip class is simple. For instance

So, in twoLists we have that A is List and B is List. However, the zip method takes a List<A> and a List<B> as parameters. Very confusing. But take note that the generic A and B from ListZip are not the same as the generic A and B from the zip method!


Edit: my reply was a reply to Junilu's post

Edit2: indeed it is the Object::toString that causes each ListZip to be transformed into a String, and the toString is used for this.



Thanks,

So maybe the zip method needs to use different parameters to keep everything sane:

6 days ago

Junilu Lacar wrote:

Mike London wrote:Maybe I misunderstand, but I can set a breakpoint on toString and it runs on the result.add()


I think you did misunderstand. The only line that would call toString() is line 21:



That was in demo code I added, but the toString() still gets called this way (when called in a REST service):

  finalList = zipLists.zip(list1, list2).stream().map(Object::toString).collect(Collectors.toList());

Perhaps it's the Object::toString() here that's doing it?

The code was initially confusing (add hours) so I missed that part.

Thanks,

-- mike
6 days ago

Piet Souris wrote:

Mike London wrote:I found the code below which appears simpler, but the question I have is why does the "result.add(...)" code call the toString() method? I thought toString() got called when you tried to System.out.println(someobject).


Well, the 'toString' method is not being called. it1 is an iterator(A) and it2 is an iterator(B) (can't use the angled brackets here, because they have a special meaning in this editor), so the calls to 'next' result in an A and a B being delivered, and from these two a new ListZip object is formed.
It is a bit simpler than mine, but mine is a tad more general, and it also deals somehow with the situation that the lists are unequal of size, This method simply stops at that point.



Maybe I misunderstand, but I can set a breakpoint on toString and it runs on the result.add()
6 days ago
I found the code below which appears simpler, but the question I have is why does the "result.add(...)" code call the toString() method? I thought toString() got called when you tried to System.out.println(someobject).

I also never realized you could just put any placeholders you wanted (like A and B) in for generic arguments.

--------

6 days ago

Liutauras Vilda wrote:But if you are happy with the result you got - all fine



In my original posting, I listed most of the things I tried. Streams.of concat, .forEach, various lambdas, and none of that worked.

The examples I saw online where people seemed to write their own zip implementation were long and convoluted to the point of ...what's the point of doing that?

Thanks

-- mike
6 days ago

Liutauras Vilda wrote:I thought you'd go with your own implementation (which is fairly simple). Didn't try Guava's this method, but I trust it does the job.



I thought you would post a code example, too.

The links you gave me pointed to several libraries that did the job. I already had "working code" with two nested list for-loops, but wanted to stream-ify it if possible.

Because the declarative model does stuff for you, I find it often difficult to interpolate all the online code examples, into working code.

Thank again.

- mike
1 week ago

Liutauras Vilda wrote:@OP

Don't forget to "think about"/handle the cases when the lists might be not equal in terms of their sizes.



Right, thanks.

My updated code, after adding the new Guava depdenncy is:

                        Stream<String> l1 = list1.stream();
                        Stream<String> l2 = list2.stream();
                        Stream <String> lineByLineConcat =  com.google.common.collect.Streams.zip(l1, l2, (s, y) -> s + " - " + y);
                        finalList = lineByLineConcat.collect(Collectors.toList());

This works!

Thanks guys!!!

--mike
1 week ago
Say I have two ArrayLists:

List 1:
-------
Item 1
Item 2

List 2:
------
Coffee
Soda

What I want to do is combine these two lists LINE BY LINE so I end up with a final list that has:

Item 1 - Coffee
Item 2 - Soda


------

Have tried Stream.of with both lists and concat methods, but that isn't right.

Also tried various forEach attempts, and ".map", but can't quite get this right.

--

Would appreciate some ideas how to do this Declaratively using Java 8.

Thanks,

- mike

1 week ago

Campbell Ritchie wrote:

Mike London wrote:. . . I didn't use the Enumeration to get Calendar.JANUARY.

Unfortunately, it isn't an enumeration (small e; an Enumeration with large E is something different), nor an enum, nor anything enumerated; it is simply ints as constants in the class. Had it been enumerated, as this is, you wouldn't have the problem of entering 2 and getting MARCH. You would have a compiler error from anything requiring that enum.



Thanks,

-- mike

On behalf of the other people who gave you more of an answer than I did:-

That's a pleasure



Thanks Campbell. Good to know. And, BTW, you've been very helpful.

- mike
2 months ago

Campbell Ritchie wrote:One reason for abandoning Calendar and Date is that the month numbers in Calendar are unintuitive.



Totally agree. I'm completely on the Java 8+ DateTime API now, but needed another test case today with this weird date math calculation from the third party application. As you can see above, to underscore your point, I made that "off-by-one" error that gave me the incorrect date since I didn't use the Enumeration to get Calendar.JANUARY.

Thanks,

-- mike
2 months ago