Found 4337 Articles for Java 8

Iterate through elements of Java LinkedHashSet

Samual Sam
Updated on 30-Jun-2020 06:11:07

255 Views

First, create a LinkedHashSet and add elements to it −LinkedHashSet set = new LinkedHashSet(); set.add(10); set.add(20); set.add(30); set.add(40); set.add(50); set.add(60);Now, use the Iterator to iterate through the elements −Iterator i = set.iterator();    while (i.hasNext()) {    System.out.println(i.next()); }The following is an example to iterate through elements of LinkedHashSet −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       LinkedHashSet set = new LinkedHashSet();       set.add(10);       set.add(20);       set.add(30);       set.add(40);       set.add(50);       set.add(60);       System.out.println("LinkedHashSet...");   ... Read More

Create a new ArrayList from another collection in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

278 Views

An ArrayList can be created from another collection using the java.util.Arrays.asList() method. A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = { "John", "Macy", "Peter", "Susan", "Lucy" };       List aList = new ArrayList(Arrays.asList(str));       System.out.println("The ArrayList elements are: " + aList);    } }OutputThe ArrayList elements are: [John, Macy, Peter, Susan, Lucy]Now let us understand the above program.The string array str[] is defined. Then an ArrayList is created using ... Read More

Fill elements in a Java int array in a specified range

Samual Sam
Updated on 30-Jul-2019 22:30:24

579 Views

Elements can be filled in a Java int array in a specified range using the java.util.Arrays.fill() method. This method assigns the required int value in the specified range to the int array in Java.The parameters required for the Arrays.fill() method are the array name, the index of the first element to be filled(inclusive), the index of the last element to be filled(exclusive) and the value that is to be stored in the array elements.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {     ... Read More

How to use headSet() method of Java NavigableSet Class

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

82 Views

The headset() method returns elements up to a limit defined as a parameter.First, create a NavigableSet and add elements −NavigableSet set = new TreeSet(); set.add(10); set.add(25); set.add(40); set.add(55); set.add(70); set.add(85); set.add(100);Now, use the headset() method −set.headSet(55));The following is an example −Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add(10);       set.add(25);       set.add(40);       set.add(55);       set.add(70);       set.add(85);       set.add(100);       System.out.println("Returned Value = " + ... Read More

Retrieve the last entry in the TreeMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

2K+ Views

Use the lastEntry() method in TreeMap to retrieve the last entry.Create a TreeMap and add some elements −TreeMap m = new TreeMap(); m.put(1, "India"); m.put(2, "US"); m.put(3, "Australia"); m.put(4, "Netherlands"); m.put(5, "Canada");Now, retrieve the last entry −m.lastEntry()The following is an example to retrieve last entry in the TreeMap −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       TreeMap m = new TreeMap();       m.put(1, "India");       m.put(2, "US");       m.put(3, "Australia");       m.put(4, "Netherlands");       m.put(5, "Canada");       for(Map.Entry e:m.entrySet()) ... Read More

Get last entry from NavigableMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

135 Views

To display the last entry from NavigableMap in Java, use the lastEntry() method.Let us first create NavigableMap.NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); n.put("C", 868); n.put("D", 988); n.put("E", 686); n.put("F", 888); n.put("G", 999); n.put("H", 444); n.put("I", 555); n.put("J", 666);Get the last entry now.n.lastEntry()The following is an example to get the last entry from NavigableMap.Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put("A", 498); n.put("B", 389); ... Read More

Get the first element from a Sorted Set in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

433 Views

To create a Sorted Set, firstly create a Set.Set s = new HashSet();Add elements to the above set.int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try { for(int i = 0; i < 5; i++) { s.add(a[i]); }After that, use TreeSet class to sort.TreeSet sorted = new TreeSet(s);Get the first element, using the first() method −System.out.println("First element of the sorted set = "+ (Integer)sorted.first());The following is the code to get the first element from a Sorted Set ... Read More

Convert an ArrayList to HashSet in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

882 Views

To convert ArrayList to HashSet, firstly create an ArrayList −List l = new ArrayList();Add elements to the ArrayList −l.add("Accent"); l.add("Speech"); l.add("Diction"); l.add("Tone"); l.add("Pronunciation");Now convert the ArrayList to HashSet −Set s = new HashSet(l);The following is an example to convert an ArrayList to HashSet in Java.Example Live Demoimport java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.HashSet; public class Main { public static void main(String[] args) { List l = new ArrayList(); l.add("Accent"); l.add("Speech"); l.add("Diction"); ... Read More

Get higher key from NavigableMap in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

89 Views

Getting higher key means to return the least key strictly greater than the given key. This is done using higherKey() method in Java.The following is an example.Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob"); System.out.println("NavigableMap elements..."+n); System.out.println("Higher Key = "+n.higherKey(15)); } }OutputNavigableMap elements... {1=Tim, 4=Jackie, 5=Tom, 9=John, 14=Jamie, 15=Kurt, 19=Tiger, 24=Jacob} Higher Key = 19

Get ceiling key from NavigableMap in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

96 Views

Getting ceiling key means to return the least key greater than or equal to the given key. For this, use the ceilingKey() method.The following is an example to get ceiling key from NavigableMap.Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); ... Read More

Advertisements