Skip to main content

Posts

Showing posts with the label list

Is there any Relation between Iterator.hasNext and for-each loop

I was using JProfiler for profiling of my application, as it is a huge application so I am very aware of its performance and efficiency. It was taking too long so I replace all Iterator.hasNext with for-each but when I am seeing in the JProfilers CPU view it is showing me Iterator.hasNext method called where I am using for-each . Why does so? Is there any relation between these two? Here is the example code : List<Map<String, Object>> mapList = jdbcTemplate .queryForList(MAP.SELECT_ALL); for (Map<String, Object> map : mapList) { list.add(fillPreferenceMaster(preferenceMasterMap)); }

Java Variable References when using Lists

Just a thought question here. In C++, I could do the following: vector<vector<string> > data; // add data into data //.. data[0].push_back( "somedata" ); And I would expect somedata to get written to the vector array because the [] notation gives me access to the object by reference. What about in Java? If I: List<List<String>> data = new ArrayList<List<String>>(); // add data into data //.. data.get(0).add( "somedata" ); Would this actually write somedata into the data object? Or would it create a new copy of the element at data(0), add somedata to that, and then that object disappears into GC sometime down the line?

Error while working with List<? extends DefaultMutableTreeNodel>

public void findClassNodesMatching(String lowerCaseSearchText, List<? extends DefaultMutableTreeNode> foundNodes) { findClassNodesMatching(lowerCaseSearchText, (DefaultMutableTreeNode) getRoot(), foundNodes); } private void findClassNodesMatching(String lowerCaseSearchText, DefaultMutableTreeNode node, List<? extends DefaultMutableTreeNode> foundNodes) { String nodeLabel = node.toString().toLowerCase(); if (nodeLabel.indexOf(lowerCaseSearchText) >= 0) { foundNodes.add(node); } } Why does this code give an error The method add(capture#2-of ? extends DefaultMutableTreeNode) in the type List is not applicable for the arguments (DefaultMutableTreeNode) The error is at the line of foundNodes.add(node);