Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Java
|
LinkedList
Notes
Example
Iterator
ListIterator
binarySearch
Notes
Ordered collection
null elements allowed
Duplicates elements allowed
Example
LinkedList<String> ls = new LinkedList<>(); ls.add(null); ls.add("ccc"); ls.add("bbb"); ls.add("bbb"); ls.add("aaa"); ls.add("aaa"); ls.add("aaa"); ls.forEach(System.out::println);
Output:
null ccc bbb bbb aaa aaa aaa
Iterator
LinkedList<String> ls = new LinkedList<>(); ls.add("ccc"); ls.add("bbb"); ls.add("aaa"); Iterator<String> i = ls.iterator(); if (i.hasNext()) i.next(); i.remove(); // removes the last element returned: ccc ls.forEach(System.out::println);
Output:
bbb aaa
ListIterator
LinkedList<String> ls = new LinkedList<>(); ls.add("ccc"); ls.add("bbb"); ls.add("aaa"); ListIterator<String> li = ls.listIterator(); ListIterator<String> li_copy = ls.listIterator(); if (li.hasNext()) li.next(); li.set("zzz"); // this will replace the value "ccc" li_copy.next(); // OK li.add("eee"); // li_copy.next(); // this will throw java.util.ConcurrentModificationException ls.forEach(System.out::println);
Output:
zzz eee bbb aaa
binarySearch
LinkedList<String> ls = new LinkedList<>(); ls.add("c"); ls.add("bb"); ls.add("aaa"); Collections.sort(ls); // Objects must implement java.lang.Comparable<T> (Compiler error) System.out.println(Collections.binarySearch(ls, "aaa")); // Objects must implement java.lang.Comparable<T> (Compiler error) Collections.sort(ls, Comparator.comparing(String::length)); System.out.println(Collections.binarySearch(ls, "aaa", Comparator.comparing(String::length)));
Output:
0 2
© 2010-2022
mti
tek