- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4336 Articles for Java 8
4K+ Views
To count the number of elements in a HashSet, use the size() method.Create HashSet −String strArr[] = { "P", "Q", "R" }; Set s = new HashSet(Arrays.asList(strArr));Let us now count the number of elements in the above Set −s.size()The following is an example to count the number of elements in a HashSet −Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] a) { String strArr[] = { "P", "Q", "R" }; Set s = new HashSet(Arrays.asList(strArr)); System.out.println("Elements: "+s); System.out.println("Number of Elements: ... Read More
66 Views
Check whether a Map is empty or not using the isEmpty() method.Let us first create a NavigableMap and add some elements to it −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");Now, use the following method to check whether the Map is empty or not. Since we added some elements above, therefore the Map is not empty −n. isEmpty();The following is an example to implement isEmpty() method −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new ... Read More
88 Views
Clear the NavigableMap in Java, using the clear() method.Let us first create a NavigableMap and add some elements to it −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");Now, use the clear() method −n.clear();The following is an example to implement clear() method and clear the 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"); ... Read More
1K+ Views
Create an array and convert it to List −Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40}; List l = Arrays.asList(arr);Now, let us convert the above array to HashSet −Set s = new HashSet(l);The following is an example to convert array to HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] argv) throws Exception { Integer[] arr = { 10, 15, 20, 10, 10, 10, 20, 30, 35, 40, 40}; List l = Arrays.asList(arr); Set s = new HashSet(l); for (Iterator i = s.iterator(); i.hasNext();) { Object ele = i.next(); System.out.println(ele); } } }Output35 20 40 10 30 15
199 Views
Declare a HashSet and add elements −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79);Now, iterate over the elements −for (Iterator i = hs.iterator(); i.hasNext();) { Object ele = i.next(); System.out.println(ele); }The following is an example that iterate over the elements of HashSet −Example Live Demoimport java.util.HashSet; import java.util.Iterator; import java.util.Set; public class Demo { public static void main(String[] argv) throws Exception { Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); System.out.println("Elements = "); for (Iterator ... Read More
277 Views
To remove specified element from HashSet, use the remove() method.First, declare a HashSet and add elements −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); hs.add(88);Let’s say you need to remove element 39. For that, use the remove() method −hs.remove(39);The following is an example to remove specified element from HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); ... Read More
298 Views
Create a HashSet and add elements to it −Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87);Try the below given code to iterate through the elements −Iterator i = hs.iterator(); while (i.hasNext()) System.out.println(i.next());To iterate through the elements of HashSet, try the following code −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Set hs = new HashSet(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); hs.add(88); System.out.println("Elements = ... Read More
267 Views
To get the size of HashSet, use the size() method. Let us create a HashSet and add elements −Set hs = new HashSet(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91);Let us now use size() and get the size of HashSet −hs.size()The following is an example to get the size of HashSet −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Set hs = new HashSet(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91); System.out.println("Elements = "+hs); ... Read More
237 Views
Declare a HashSet and add elements −Set hs = new HashSet(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91); hs.add(93); hs.add(97); hs.add(99);To copy all the elements, use the toArray() method −Object[] obArr = hs.toArray();The following is an example to copy all elements to HashSet to an object array −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Set hs = new HashSet(); hs.add(15); hs.add(71); hs.add(82); hs.add(89); hs.add(91); hs.add(93); hs.add(97); hs.add(99); ... Read More
2K+ Views
To remove all the elements from HashSet, use the clear() method.Create a HashSet and initialize elements −String strArr[] = { "P", "Q", "R", "S" }; Set s = new HashSet(Arrays.asList(strArr));Now, to remove all the above elements −s.clear()The following is an example to remove all elements from a HashSet −Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo { public static void main(String[] a) { String strArr[] = { "P", "Q", "R", "S" }; Set s = new HashSet(Arrays.asList(strArr)); strArr = new String[] { "R", "S", "T", "U" }; ... Read More