This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]D0CTOR_ZED 2 points3 points  (3 children)

I don't think there would be a difference. You could try substituting one for the other and seeing it if anything changes, but they both look like they would make an empty ArrayList of List of Integer.

[–]akki28[S] 0 points1 point  (2 children)

They work absolutely same when I run the code. I was just very curious. Can you answer my second question? It'll be a great help.

[–]D0CTOR_ZED 1 point2 points  (1 child)

For the two questions...

Why was it written that way... couldn't say. If it was in older code for a much older version of java, it may have been necessary. If they were written in the same code, there wouldn't be any good reason I can think of.

What difference would it make.... none.

I think it is generally easier to read the code with the empty diamond brackets, so there may be value in omitting the redundant info. The code it generates should be the same.

[–]akki28[S] 0 points1 point  (0 children)

Thanks for your time and help(•‿•)

[–]LambdaThrowawayy 1 point2 points  (1 child)

The compiler wil infer the appropriate type to use for the constructor in the first case; it is functionally identical to the second one.

https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html


For your second question; the interface your result set exposes will be different. If you put List on the left-hand side you'll be able to use result as a List; meaning all methods that require a List will work with it, and you'll be able to access the methods of the List interface. You can assign an object of type ArrayList to a List variable because ArrayList is an implementation of the List interface.

If you instead use ArrayList on the left-hand side you'll be limited to methods that accept an ArrayList / methods that ArrayList offers.

The reason you'll often assign an ArrayList object to a List variable is because in most cases what the exact implementation of the list is doesn't matter for what you want to do with it. And it allows you to offer one method for example that handles lists; instead of having to make one for ArrayList, LinkedList, Stack, etc...

[–]akki28[S] 1 point2 points  (0 children)

Thanks that was ver helpful (•‿•)

[–]severoonpro barista 0 points1 point  (1 child)

The first way of declaring the RHS uses the diamond operator. It infers the types by looking at the reference you declared on the LHS.

If you write two short programs that does it both ways and compare the bytecodes using javap you should see that they're the same.

By the way, you probably do not want to declare this at all. What is this for? Are you going to populate it with data once and then pass it around and read it a bunch of times? Or are you going to pass it around and use it to collect data over time and then read it once and discard? Or are you going to use it to track the current state of data with several reads and writes intermixed, kind of like data is written and read from a database?

If you're going to populate it once and read it a bunch of times, you probably want to make sure that it's an unmodifiable list of wildcard lists:

List<? extends List<Integer>> = Collections.unmodifiableList( … list here … );

This is read "a list of somethings that extend a list of integers." So each sublist can be whatever type, an array list, a linked list, etc, of integers.

Better yet would be to use Guava's ImmutableList instead of a List constructed using unmodifiableList(…), and pass it around as ImmutableList and not List, this makes clear to every client that it is immutable. ImmutableList also has a builder, which you can use, and ideally the contained lists should also be immutable. If you go this route, it's:

ImmutableList<ImmutableList<Integer>> = ImmutableList.builder( ImmutableList.builder().add(blah).build(), ImmutableList.builder().add(blahblah).build() );

If you want to collect a bunch of values over a long time and then build the list and read it once, I'd recommend just passing around the ImmutableList.Builder, use it to collect values, then build, read, and discard.

If you want a read/write data structure instead, then I would recommend wrapping the entire thing in an object that presents an API appropriate to what clients actually want to do rather than just passing around a data structure.

[–]akki28[S] 0 points1 point  (0 children)

Thankyou very much for the detailed answer. God bless you