collections - Cons'ing a List in Java -


I have a java.util.list list and I want to create a new << Code> list by adding an element e to << Code> list (i.e., opposition e and list ). For example, if is the list

  [1,2,3,4]  

and e < / Code> is 5 , then the opposition (e, list) yes

  [5,1,2,3,4]   

and cons (e, list) , but list Should not be modified.

What is the easiest and / or most effective way to implement cons ? Results are not okay for the result. Use of the Google Collection Library is permitted.

What if list is a com.google.common.collect.ImmutableList ?

  public static & lt; T & gt; & Lt; T & gt; Cons (List & lt; T & gt; List, TT) {ArrayList & lt; T & gt; Results = New ArrayList & lt; T & gt; (List); Result.add (0, t); Return result; }  

In response to comments, edited: Since the question "is the simplest and / or most effective way to implement disaster," I went with "simplest". I would not be surprised to learn that there are more efficient ways to put elements first before the list is another valid approach, and initially allocating the right size may possibly improve performance. Premature adaptation is the root of all evils.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -