java <String> tag with objects? [duplicate] - java

This question already has answers here:
What does <T> (angle brackets) mean in Java?
(6 answers)
Closed 8 years ago.
So, while looking through others code ive been seeing things like such as in the following code:
List myList = new ArrayList<String>(Arrays.asList(s.split(" ")));
What exactly does this do? I haven't been able to find any documentation, in part by the reason that I don't really know what it is called. And if possible an explanation of what exactly they do?

That particular code generates a list that contains the entries resulted from splitting the s string at each space.
The < String> defines the generic type for the List, and the advantage (among other ones) is that you can call myList.get(index) and not have to cast it to a String.

This is a generic. You are instantiating an ArrayList that stores String objects.

Related

Most efficient way to check if an array contains a value in Java? [duplicate]

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 6 years ago.
I have a similar logic for a method in a Java Class (not the real code, this is simplified for example purposes).
private Boolean method(Boolean booleanValue, SomeObject object) {
return booleanValue ? Arrays.asList(object.getStringsArray()).contains("string") : false;
}
A collaborator who assigned himself to check the PR gave the following comment:
This is inefficient. It is creating a new data structure only to iterate it and check if there is a certain string.
The getStringsArray() method returns a String[], so will using a for-loop be better than Arrays.asList()?
Which way is more efficient to achieve this?
Your co-worker is incorrect when they assert that your method is creating a new data structure.
If you look at the API for Arrays.asList(), it says that it
Returns a fixed-size list backed by the specified array.
There is no reason to write your own code to iterate over the array when you can just wrap a List around it, and use its built-in methods.

What is the difference between multiple implementations of ArrayList in the (Java8) source code [duplicate]

This question already has answers here:
Arrays.asList() doubt?
(8 answers)
Closed 9 years ago.
I was trying to understand Streams in Java8 and intermittently I stumbled upon an interesting thing in the source code of Java8: ArrayList seems to be implemented twice:
The obvious one: java.util.ArrayList
The non-obvious one: java.util.Arrays.ArrayList, which is a private class.
One odd difference is that the normal version is way bigger, and implements List<E>, whereas Arrays.ArrayList does not do so (directly).
Why is it defined twice? And why with the same name?
Actually its there ever since Arrays.asList() introduced. Array's ArrayList is view of the underlying array. If the Array gets changed the ArrayList will get effected and viceversa.
The main benefit, No additional space required because it wont copy the array to a new object (ArrayList), also no additional time to copy the elements.

Equivalent to "in" for Java [duplicate]

This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 9 years ago.
In Python you can do something like
if 7 in list
return True
Is there anything in java like this? To go "if x in array" without having to do a for loop or several lines of code?
Thanks
Java arrays don't have such properties, but you can either use a collection (preferable a Set, because the lookup methods are the most efficient) or wrap your array with Arrays.asList()
return Arrays.asList(arr).contains(7)
You can use
Arrays.asList(yourArray) - convert array to list
and then
.contains(7) - find value at list
Some other solutions:
http://javarevisited.blogspot.cz/2012/11/4-ways-to-search-object-in-java-array-example.html
The ArrayList class provides method contains(Object).
You need to call Collection contains method to check for existense:
list.contains(7)
If you are already using Commons Lang, there is ArrayUtils#contains.
Convert your array to list using Arrays#asList() and
There is contains() method in List
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object)

Sorting an ArrayList by the value of a field in the objects it stores [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting ArrayList of Objects by Object attribute
Basically I have an ArrayList that stores objects, each of those objects has a field that stores an integer. I want to store the objects in my ArrayList by acending order of that integer.
Is there an easy way to do this, I've looked around the Java API doc but can't find anything that looks suitable, sorry if this is trivial but it sounds like it should be simple enough if I can just find the right documentation.
Thanks
Create your custom comparator class implementing java.util.Comparator and use java.util.Collections.sort(list, comparator).

How to make an array in Java read only? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Java unmodifiable array
Immutable array in Java
How do I make an array read only so that the elements inside it can only be read but cannot be modified,added or deleted. need to do this in JAVA. Please help. I think merely the use of final keyword wont help.Need to do something more than that at the code level. Thanks in advance!
Short answer is you can't -- final will only guarantee you that the reference to the array itself won't be changed. You can do this with a List though, as the Collections class provides a method for creating a List that cannot be modified (Collections.unmodifiableList) -- that is only if you can change your application to use List rather than array.

Categories

Resources