This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 256258 - Manual array to collection copy hint replacement unnecessarily creates temporary object
Summary: Manual array to collection copy hint replacement unnecessarily creates tempor...
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.2
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-29 14:12 UTC by tln
Modified: 2015-10-29 14:12 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description tln 2015-10-29 14:12:12 UTC
See this example code:

public class Example
{
	public static void example()
	{
		Integer[] values=new Integer[]{1, 2, 3};
		Set<Integer> valuesSet=new HashSet();
		for (Integer curValue: values)		// *
		{
			valuesSet.add(curValue);
		}
	}
}

On the line marked with the "*" NetBeans complains about a "Manual array copy to collection". If I accept the "Replace with ..." suggestion, the code reads as follows:

public class Example
{
	public static void example()
	{
		Integer[] values=new Integer[]{1, 2, 3};
		Set<Integer> valuesSet=new HashSet();
		valuesSet.addAll(Arrays.asList(values)); // *
	}
}

However this is not the best solution, especially when the code is executed many times. NetBeans should rather replace with

public class Example
{
	public static void example()
	{
		Integer[] values=new Integer[]{1, 2, 3};
		Set<Integer> valuesSet=new HashSet();
		(java.util.)Collections.addAll(valuesSet, values); // *
	}
}

This solution is not only slightly more readable, it would also have the advantage of copying straight forward and not creating any temporary objects in the middle. As such it could have noticeable benefits in environments where the code executes in a loop and/or GC activity can become an issue.
As such it's the only *real* replacement of the manual array copy that did the job without any objects in the middle in the first place.

The only small disadvantage of this solution is that it's only available from Java 1.5, but for those of your users who are still working on Java 1.4 and below you can still keep the current suggestion.