List LinkedList add sort find Item in Java example

List LinkedList Collections add sort find search Item, get searched item to string in Java example.

MainClass.java

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
 
public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = {"nothing", "Hello", "people"
				, "bye-bye", "hello", "world!", "End" };
 
                   List<String> arrayList = new LinkedList<String>();
 
                   for(String s: arrayOfString)
		     arrayList.add(s);
 
		    Collections.sort(arrayList);
		    // foreach
		    for (String str: arrayList)
		      System.out.println(str);
 
		    Object objMin = Collections.min(arrayList);
		    System.out.println("Min is: " + objMin);
 
		    Object objMax = Collections.max(arrayList);
		    System.out.println("Max is: " + objMax);
 
		    int index = Collections.binarySearch(arrayList, "people");
		    System.out.println("Index of people is: " + index);
		    // print word on index 5
		    System.out.println("Index 5 is: " + arrayList.get(5));
                    String sItem = arrayList.get(5); // get item from index 5 to string
 
		  }
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
Index 5 is: people
*/
This entry was posted in Android Examples Code, Array,List,Collections and tagged , , , , , , . Bookmark the permalink.


Comments are closed.



6,303 views