简体   繁体   中英

Why the difference with ListIterator vs Stream

We have a Dictionary txt file we converted to a String[ ] simpleArray
We seldom work with array's so this one was beyond our comfort zone
It has 466552 items in this format 40 abacterial
When we checking for a misspelled word it is blazing fast
When we go back to the list and check for the correct spelling of a word we were using ListIterator
We found the results odd based on the search term
So we tried to use Java 8 Streams with filters and lambda

Here are the results using the ListIterator with the search term "some"

somebodies someday somegate someonell someone's Somerdale somersaulting
somerseted Somersetshire Somersville somesthesis somet sometime someway
somewhatly somewhen somewheres somewhiles somic


Here are the results using the ListIterator with the search term "someo" and "someon"


someone'll somepart


Here are the results using the Steams with filters and lambda the search term is "someon"

someone someonell someone'll someones someone's


The question is have we constructed the ListIterator code correctly?
While the code runs it dose not produce reliable results

We would appreciate knowing if the Stream code could be constructed differently
The goal was to add the items to a listview for now the results are in a textarea
We have not used the listview before


}if(found != true){

for(indexSC = 0; indexSC < simpleArray.length;indexSC++){
if(simpleArray[indexSC].toLowerCase().contains(txtTest.getText().trim().toLowerCase())){
// array stream foreach filter results to textarea javafx
List<String> cs = Arrays.asList(simpleArray);

ArrayList list = new ArrayList<>();
cs.stream().filter(s -> s.startsWith("someon"))
  //.forEach(System.out::println); 
.forEach(list :: add);   
String aL = list.toString();
System.out.println("!!!! aL "+aL);
String La = list.toString().replace("[", "").replace("]","").replace(",", " ");
System.out.println("@@@@ Stream "+La);

txaML.appendText(La);
txaML.appendText("\n");
//list.forEach(System.out::println);


ListIterator<String> listItr = cs.listIterator();
System.out.println("%%%% Second Find "+simpleArray[indexSC]+" At indexSC "+indexSC+" L "+simpleArray[indexSC].length());
txaML.clear();
while(listItr.hasNext()){
    if(listItr.next().startsWith("someon")){

        txaML.appendText(listItr.next());
        txaML.appendText("\n");
        //txaML.appendText(listItr.next().intern());
        //txaML.appendText("\n");
        System.out.println("!!!! ListIterator "+listItr.next());
        //System.out.println("!!!!!! Next intern "+listItr.next().intern());

    }  
}

We would appreciate knowing if the Stream code could be constructed differently

We added a few item to your project namely a ComboBox and a ListView
Why? If you plan on selecting the information generated by the Stream the TextArea is a real bear when it comes to selecting items (Text)
We noticed that you had an ArrayList list that the Stream added data to
So no need for all that replace code. We added a SelectionModel listener to the ListView

Here is the code with your old remnant code
This is all you need for the ComboBox

 public void CboSelect(){
 months = FXCollections.observableArrayList();
}

@FXML
public void getSP(){
    String selected = cboSP.getValue().toString();
    System.out.println("S S S selected "+selected); 
}

Here is the new work of Art ha ha

        }if(found != true){

        lvListView.setStyle("-fx-font-size:18.0;-fx-background-color: white;");
        for(indexSC = 0; indexSC < simpleArray.length;indexSC++){
        if(simpleArray[indexSC].toLowerCase().contains(txtTest.getText().trim().toLowerCase())){
        // array stream foreach filter results to textarea javafx
        List<String> cs = Arrays.asList(simpleArray);

        ArrayList list = new ArrayList<>();
        cs.stream().filter(s -> s.startsWith("someon"))
          //.forEach(System.out::println); 
        .forEach(list :: add);   
        //String aL = list.toString();
        //System.out.println("!!!! aL "+aL);
        //La = list.toString().replace("[", "").replace("]","").replace(",", "").replace(" ", "\n");
        int L = list.size();
        for(int X = 0; X < L;X++){
        String A = (String) list.get(X);
        cboSP.getItems().add(A);
        txaML.appendText(A);
        txaML.appendText("\n");
        lvListView.getItems().add(A);
        System.out.println(" = = L "+L+" Num "+A);
        }

        lvListView.getSelectionModel().selectedItemProperty()
        .addListener((observable, oldValue, newValue) -> {
        System.out.println(newValue);});

We could not find much on the behavior of ListIterators with regards to performance
We looked a number of SO posts about Streams seems to be the new Java 8 thing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM