- Java TreeSet addAll(Collection The method addAll() from TreeSet is declared as: public boolean addAll(Collectionextends E> c) The method addAll() has the following parameter: The method addAll() returns true if this set changed as a result of the call The method addAll() throws the following exceptions: NullPointerException — if the specified collection is null or if any element is null and this set uses natural ordering, or its comparator does not permit null elements ClassCastException — if the elements provided cannot be compared with the elements currently in the set Example The following code shows how to use TreeSet from java.util. Specifically, the code shows you how to use Java TreeSet addAll(Collection c) import javax.swing.*; import java.util.*; public class UIDefaults < public static void main(String[] args) < try // w w w . d e m o 2 s . c o m SetMap.EntryObject, Object>> defaults = UIManager.getLookAndFeelDefaults().entrySet(); // this TreeSet will hold the sorted properties TreeSetMap.EntryObject, Object>> ts = new TreeSet<>(new ComparatorMap.EntryObject, Object>>() < public int compare(Map.EntryObject, Object> a, Map.EntryObject, Object> b) < Map.Entry ea = (Map.Entry) a; Map.Entry eb = (Map.Entry) b; return (ea.getKey().toString()).compareTo(eb.getKey().toString()); > >); ts.addAll(defaults); for (Iterator i = ts.iterator(); i.hasNext();) < Map.Entry entry = (Map.Entry) i.next(); System.out.print(entry.getKey() + " https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"> Example 2 Copy import java.util.*; import java.io.*; public class Jukebox8 < ArrayList songList = new ArrayList(); public static void main(String[] args) < new Jukebox8().go(); >/ * w w w . d e m o 2 s . c o m * / public void go() < getSongs(); System.out.println(songList); Collections.sort(songList); System.out.println(songList); TreeSet songSet = new TreeSet(); songSet.addAll(songList); System.out.println(songSet); > void getSongs() < try < File file = new File("SongListMore.txt" ); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) < addSong(line); >> catch (Exception ex) < ex.printStackTrace(); >> void addSong(String lineToParse) < String[] tokens = lineToParse.split("/" ); Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]); songList.add(nextSong); > > //Implementing Sets //Using hash tables: no order import java.util.*; import java.io.*; public class Set03 < public static void main(String[] args) throws IOException < String[] wordArray = utilities.fileToStringArray("awmt.txt" ); ListString> wordList = Arrays.asList(wordArray); TreeSetString> setA = new TreeSetString>(wordList); TreeSetString> setC; IteratorString> it; it = setA.iterator();/ * w w w . d e m o 2 s . c o m * / while (it.hasNext()) < if (condAB(it.next())) < it.remove(); >> for (String s : setA) < System.out.println(s); > System.out.println("Our set has " + setA.size() + " elements." ); setC = union(setA, setA); System.out.println("Our set has " + setC.size() + " elements." ); > public static boolean condAB(String str) < String s = str.toLowerCase(); if (s.compareTo("c" ) >= 0) < return true; > return false; > public static TreeSetString> union(TreeSetString> A, TreeSetString> B) < A.addAll(B); return A; > > import java.util.*; public class IdentityMod implements SetModifier < public SortedSet apply(SortedSet extends E> set, Comparator super E> comp) < if (comp == null) < return new TreeSet(set); > else < TreeSet ret = new TreeSet<>(comp); ret.addAll(set); return ret; > > > //Implementing Sets //Using hash tables: no order import java.util.*; import java.io.*; public class Set04 < public static void main(String[] args) throws IOException < String[] wordArray = utilities.fileToStringArray("awmt.txt" ); ListString> wordList = Arrays.asList(wordArray); TreeSetString> setA = new TreeSetString>(wordList); TreeSetString> setB = new TreeSetString>(wordList); TreeSetString> setC; IteratorString> it; it = setA.iterator();/ / w w w . d e m o 2 s . c o m while (it.hasNext()) < if (condG7(it.next())) < it.remove(); >> for (String s : setA) < System.out.println(s); > System.out.println("Our set has " + setA.size() + " elements." ); it = setB.iterator(); while (it.hasNext()) < if (condLE7(it.next())) < it.remove(); >> for (String s : setB) < System.out.println(s); > System.out.println("SetA has " + setA.size() + " elements." ); System.out.println("SetB has " + setB.size() + " elements." ); setC = union(setA, setB); System.out.println("The union has " + setC.size() + " elements." ); > public static boolean condG7(String str) < if (str.length() > 7) < return true; > return false; > public static boolean condLE7(String str) < if (str.length() return true; > return false; > public static TreeSetString> union(TreeSetString> A, TreeSetString> B) < A.addAll(B); return A; > > //Implementing Sets //Using hash tables: no order import java.util.*; import java.io.*; public class UnionAB < public static void main(String[] args) throws IOException < String[] wordArray = utilities.fileToStringArray("awmt.txt" ); ListString> wordList = Arrays.asList(wordArray); TreeSetString> setA = new TreeSetString>(wordList); TreeSetString> setB = new TreeSetString>(wordList); TreeSetString> setC; IteratorString> it; it = setA.iterator();/ * w w w . d e m o 2 s . c o m * / while (it.hasNext()) < if (!condA(it.next())) < it.remove(); >> for (String s : setA) < System.out.println(s); > it = setB.iterator(); while (it.hasNext()) < if (!condB(it.next())) < it.remove(); >> for (String s : setB) < System.out.println(s); > System.out.println("SetA has " + setA.size() + " elements." ); System.out.println("SetB has " + setB.size() + " elements." ); setC = union(setA, setB); System.out.println("The union has " + setC.size() + " elements." ); > public static boolean condA(String str) < if (str.charAt(0) == 'a' || str.charAt(0) == 'b' || str.charAt(0) == 'c' || str.charAt(0) == 'd' ) < return true; > return false; > public static boolean condB(String str) < if (str.charAt(0) == 'c' || str.charAt(0) == 'd' || str.charAt(0) == 'e' || str.charAt(0) == 'f' ) < return true; > return false; > //This is a good way to write a union method: it doesn't alter the input. public static TreeSetString> union(TreeSetString> A, TreeSetString> B) < TreeSetString> Acopy = new TreeSetString>(); Acopy.addAll(A); Acopy.addAll(B); return Acopy; > > Related demo2s.com | Email: | Demo Source and Support. All rights reserved. Источник java.util.TreeSet.addAll() Method The addAll(Collection c) method is used to add all of the elements in the specified collection to this set. Declaration Following is the declaration for java.util.TreeSet.addAll() method. public boolean addAll(Collection c) Parameters c − These are the elements to be added. Return Value The method call returns true if this set changed as a result of the call. Exception ClassCastException − This exception is thrown if the elements provided cannot be compared with the elements currently in the set. NullPointerException − This exception is thrown if the specified collection is null. Example The following example shows the usage of java.util.TreeSet.addAll() method. package com.tutorialspoint; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo < public static void main(String[] args) < // creating a TreeSet TreeSet treeone = new TreeSet(); TreeSet treetwo = new TreeSet(); // adding in the treeone treeone.add(12); treeone.add(13); treeone.add(14); // adding in the treetwo treetwo.add(15); treetwo.add(16); treetwo.add(17); // adding treetwo to treeone treeone.addAll(treetwo); // create an iterator Iterator iterator; iterator = treeone.iterator(); // displaying the Tree set data System.out.print("Tree set data: "); while (iterator.hasNext()) < System.out.print(iterator.next() + " "); >> > Let us compile and run the above program, this will produce the following result. Tree set data: 12 13 14 15 16 17 Источник Java TreeSet add() addAll() Methods Tutorial In this section, we will learn what the TreeSet add() and addAll() methods are and how to use them in Java. What is Java TreeSet add() Method? The Java TreeSet add() method is used to add an element to a TreeSet object. Java add() Method Syntax: add() Method Parameters: The method takes one argument and that is the element we want to add to the target TreeSet object. add() Method Return Value: The return value of this method is of type Boolean. If the value we’ve set as the argument of this method didn’t exist in the list and calling this method caused a new element to be added, then the return value of this method becomes true. Otherwise, the value false will return instead. add() Method Exceptions: The method might throw two types of exceptions: ClassCastException: We get this exception if the argument is of incompatible type compared to the elements in the target TreeSet object. NullPointerException: we get this exception if the argument is a null value. Example: using TreeSet add() method import java.util.TreeSet; class Main < public static void main(String[] args) < TreeSetts = new TreeSet<>(); ts.add(200); ts.add(2); ts.add(3); ts.add(0); ts.add(10); ts.add(30); System.out.println(ts.last()); > > What is Java TreeSet addAll() Method? The Java TreeSet addAll() method is used to add the elements of another collection to a TreeSet object. Basically, when we want to add multiple elements at one call to a TreeSet object, we use this method. Java addAll() Method Syntax: public boolean addAll(Collection c) addAll() Method Parameters: The method takes one argument and that is a reference to the collection object we want to copy its elements and put them as the elements of a TreeSet object. addAll() Method Return Value: The return value of this method is of type boolean. If one or more elements were passed to the target TreeSet object (Basically, if the target TreeSet object was changed as a result of calling this method) then the return value will be true. Otherwise, if nothing happened (perhaps because the elements of the reference collection are already existing in the target TreeSet object) then the return value of this method becomes false because nothing changed as a result of calling the method. addAll() Method Exceptions: ClassCastException: We get this exception if the argument is of incompatible type compared to the elements in the target TreeSet object. NullPointerException: We get this exception if the argument is a null value. Example: using TreeSet addAll() method import java.util.TreeSet; import java.util.ArrayList; class Main < public static void main(String[] args) < TreeSetts = new TreeSet<>(); ts.add(200); ts.add(2); ts.add(3); ts.add(0); ts.add(10); ts.add(30); ArrayList list = new ArrayList<>(); list.add(1000); list.add(2000); list.add(3000); ts.addAll(list); for (int i: ts) < System.out.println(i); >> > 0 2 3 10 30 200 1000 2000 3000 Источник Java TreeSet Tutorial with Examples Java TreeSet tutorial with examples will help you understand how to use Java TreeSet in an easy way. TreeSet in Java is a Set implementation based on the TreeMap. Unlike HashMap, the elements of the TreeSet are ordered using their natural ordering. Also, unlike the Map implementations, for example, TreeMap or HashMap, the TreeSet does not maintain key value pairs but offers the uniqueness of its elements. If you do not want to sort the TreeSet elements using their natural ordering, you can provide a custom Comparator when you create the TreeSet object using the below given constructor. The TreeSet in Java provides log(n) time cost for the add , remove and contains operations. The TreeSet implementation is not synchronized. That means if multiple threads are trying to modify the TreeSet object at the same time then the access must be synchronized explicitly. If this is the case, you can either synchronize the TreeSet object or you can get the synchronized SortedSet object from the existing TreeSet using synchronizedSortedSet method of the Collections class as given below. The Iterator object obtained by the iterator method of the TreeSet is fail-fast. That means if the TreeSet object is structurally modified after obtaining the iterator (except by using iterator’s own remove method), calling any methods of the iterator object will throw ConcurrentModificationException . How to create a new TreeSet object? The TreeSet class in Java provides several constructors using which we can create new TreeSet objects. 1. How to create a new empty TreeSet object? The default constructor of the TreeSet class creates a new empty TreeSet object. Источник
- Example
- Related
- java.util.TreeSet.addAll() Method
- Declaration
- Parameters
- Return Value
- Exception
- Example
- Java TreeSet add() addAll() Methods Tutorial
- What is Java TreeSet add() Method?
- Java add() Method Syntax:
- add() Method Parameters:
- add() Method Return Value:
- add() Method Exceptions:
- Example: using TreeSet add() method
- What is Java TreeSet addAll() Method?
- Java addAll() Method Syntax:
- addAll() Method Parameters:
- addAll() Method Return Value:
- addAll() Method Exceptions:
- Example: using TreeSet addAll() method
- Java TreeSet Tutorial with Examples
- How to create a new TreeSet object?
- 1. How to create a new empty TreeSet object?
Java TreeSet addAll(Collection
The method addAll() from TreeSet is declared as:
public boolean addAll(Collectionextends E> c)
The method addAll() has the following parameter:
The method addAll() returns true if this set changed as a result of the call
The method addAll() throws the following exceptions:
- NullPointerException — if the specified collection is null or if any element is null and this set uses natural ordering, or its comparator does not permit null elements
- ClassCastException — if the elements provided cannot be compared with the elements currently in the set
Example
The following code shows how to use TreeSet from java.util.
Specifically, the code shows you how to use Java TreeSet addAll(Collection c)
import javax.swing.*; import java.util.*; public class UIDefaults < public static void main(String[] args) < try // w ww . d e m o 2 s . c o m SetMap.EntryObject, Object>> defaults = UIManager.getLookAndFeelDefaults().entrySet(); // this TreeSet will hold the sorted properties TreeSetMap.EntryObject, Object>> ts = new TreeSet<>(new ComparatorMap.EntryObject, Object>>() < public int compare(Map.EntryObject, Object> a, Map.EntryObject, Object> b) < Map.Entry ea = (Map.Entry) a; Map.Entry eb = (Map.Entry) b; return (ea.getKey().toString()).compareTo(eb.getKey().toString()); > >); ts.addAll(defaults); for (Iterator i = ts.iterator(); i.hasNext();) < Map.Entry entry = (Map.Entry) i.next(); System.out.print(entry.getKey() + " https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">Example 2
import java.util.*; import java.io.*; public class Jukebox8 < ArrayList songList = new ArrayList(); public static void main(String[] args) < new Jukebox8().go(); >/*w w w . d e m o2 s . c o m */ public void go() < getSongs(); System.out.println(songList); Collections.sort(songList); System.out.println(songList); TreeSet songSet = new TreeSet(); songSet.addAll(songList); System.out.println(songSet); > void getSongs() < try < File file = new File("SongListMore.txt"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) < addSong(line); >> catch (Exception ex) < ex.printStackTrace(); >> void addSong(String lineToParse) < String[] tokens = lineToParse.split("/"); Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]); songList.add(nextSong); > >//Implementing Sets //Using hash tables: no order import java.util.*; import java.io.*; public class Set03 < public static void main(String[] args) throws IOException < String[] wordArray = utilities.fileToStringArray("awmt.txt"); ListString> wordList = Arrays.asList(wordArray); TreeSetString> setA = new TreeSetString>(wordList); TreeSetString> setC; IteratorString> it; it = setA.iterator();/*w w w . de m o2 s . co m */ while (it.hasNext()) < if (condAB(it.next())) < it.remove(); >> for (String s : setA) < System.out.println(s); > System.out.println("Our set has " + setA.size() + " elements."); setC = union(setA, setA); System.out.println("Our set has " + setC.size() + " elements."); > public static boolean condAB(String str) < String s = str.toLowerCase(); if (s.compareTo("c") >= 0) < return true; > return false; > public static TreeSetString> union(TreeSetString> A, TreeSetString> B) < A.addAll(B); return A; > >import java.util.*; public class IdentityMod implements SetModifier < public SortedSet apply(SortedSet extends E> set, Comparator super E> comp) < if (comp == null) < return new TreeSet(set); > else < TreeSet ret = new TreeSet<>(comp); ret.addAll(set); return ret; > > >//Implementing Sets //Using hash tables: no order import java.util.*; import java.io.*; public class Set04 < public static void main(String[] args) throws IOException < String[] wordArray = utilities.fileToStringArray("awmt.txt"); ListString> wordList = Arrays.asList(wordArray); TreeSetString> setA = new TreeSetString>(wordList); TreeSetString> setB = new TreeSetString>(wordList); TreeSetString> setC; IteratorString> it; it = setA.iterator();// w w w . d em o 2 s . c o m while (it.hasNext()) < if (condG7(it.next())) < it.remove(); >> for (String s : setA) < System.out.println(s); > System.out.println("Our set has " + setA.size() + " elements."); it = setB.iterator(); while (it.hasNext()) < if (condLE7(it.next())) < it.remove(); >> for (String s : setB) < System.out.println(s); > System.out.println("SetA has " + setA.size() + " elements."); System.out.println("SetB has " + setB.size() + " elements."); setC = union(setA, setB); System.out.println("The union has " + setC.size() + " elements."); > public static boolean condG7(String str) < if (str.length() > 7) < return true; > return false; > public static boolean condLE7(String str) < if (str.length() returntrue; > return false; > public static TreeSetString> union(TreeSetString> A, TreeSetString> B) < A.addAll(B); return A; > >
//Implementing Sets //Using hash tables: no order import java.util.*; import java.io.*; public class UnionAB < public static void main(String[] args) throws IOException < String[] wordArray = utilities.fileToStringArray("awmt.txt"); ListString> wordList = Arrays.asList(wordArray); TreeSetString> setA = new TreeSetString>(wordList); TreeSetString> setB = new TreeSetString>(wordList); TreeSetString> setC; IteratorString> it; it = setA.iterator();/* ww w . d e m o2 s . c o m*/ while (it.hasNext()) < if (!condA(it.next())) < it.remove(); >> for (String s : setA) < System.out.println(s); > it = setB.iterator(); while (it.hasNext()) < if (!condB(it.next())) < it.remove(); >> for (String s : setB) < System.out.println(s); > System.out.println("SetA has " + setA.size() + " elements."); System.out.println("SetB has " + setB.size() + " elements."); setC = union(setA, setB); System.out.println("The union has " + setC.size() + " elements."); > public static boolean condA(String str) < if (str.charAt(0) == 'a' || str.charAt(0) == 'b' || str.charAt(0) == 'c' || str.charAt(0) == 'd') < return true; > return false; > public static boolean condB(String str) < if (str.charAt(0) == 'c' || str.charAt(0) == 'd' || str.charAt(0) == 'e' || str.charAt(0) == 'f') < return true; > return false; > //This is a good way to write a union method: it doesn't alter the input. public static TreeSetString> union(TreeSetString> A, TreeSetString> B) < TreeSetString> Acopy = new TreeSetString>(); Acopy.addAll(A); Acopy.addAll(B); return Acopy; > >
Related
demo2s.com | Email: | Demo Source and Support. All rights reserved.
java.util.TreeSet.addAll() Method
The addAll(Collection c) method is used to add all of the elements in the specified collection to this set.
Declaration
Following is the declaration for java.util.TreeSet.addAll() method.
public boolean addAll(Collection c)
Parameters
c − These are the elements to be added.
Return Value
The method call returns true if this set changed as a result of the call.
Exception
- ClassCastException − This exception is thrown if the elements provided cannot be compared with the elements currently in the set.
- NullPointerException − This exception is thrown if the specified collection is null.
Example
The following example shows the usage of java.util.TreeSet.addAll() method.
package com.tutorialspoint; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo < public static void main(String[] args) < // creating a TreeSet TreeSet treeone = new TreeSet(); TreeSet treetwo = new TreeSet(); // adding in the treeone treeone.add(12); treeone.add(13); treeone.add(14); // adding in the treetwo treetwo.add(15); treetwo.add(16); treetwo.add(17); // adding treetwo to treeone treeone.addAll(treetwo); // create an iterator Iterator iterator; iterator = treeone.iterator(); // displaying the Tree set data System.out.print("Tree set data: "); while (iterator.hasNext()) < System.out.print(iterator.next() + " "); >> >
Let us compile and run the above program, this will produce the following result.
Tree set data: 12 13 14 15 16 17
Java TreeSet add() addAll() Methods Tutorial
In this section, we will learn what the TreeSet add() and addAll() methods are and how to use them in Java.
What is Java TreeSet add() Method?
The Java TreeSet add() method is used to add an element to a TreeSet object.
Java add() Method Syntax:
add() Method Parameters:
The method takes one argument and that is the element we want to add to the target TreeSet object.
add() Method Return Value:
The return value of this method is of type Boolean.
If the value we’ve set as the argument of this method didn’t exist in the list and calling this method caused a new element to be added, then the return value of this method becomes true.
Otherwise, the value false will return instead.
add() Method Exceptions:
The method might throw two types of exceptions:
ClassCastException: We get this exception if the argument is of incompatible type compared to the elements in the target TreeSet object.
NullPointerException: we get this exception if the argument is a null value.
Example: using TreeSet add() method
import java.util.TreeSet; class Main < public static void main(String[] args) < TreeSetts = new TreeSet<>(); ts.add(200); ts.add(2); ts.add(3); ts.add(0); ts.add(10); ts.add(30); System.out.println(ts.last()); > >
What is Java TreeSet addAll() Method?
The Java TreeSet addAll() method is used to add the elements of another collection to a TreeSet object.
Basically, when we want to add multiple elements at one call to a TreeSet object, we use this method.
Java addAll() Method Syntax:
public boolean addAll(Collection c)
addAll() Method Parameters:
The method takes one argument and that is a reference to the collection object we want to copy its elements and put them as the elements of a TreeSet object.
addAll() Method Return Value:
The return value of this method is of type boolean.
If one or more elements were passed to the target TreeSet object (Basically, if the target TreeSet object was changed as a result of calling this method) then the return value will be true.
Otherwise, if nothing happened (perhaps because the elements of the reference collection are already existing in the target TreeSet object) then the return value of this method becomes false because nothing changed as a result of calling the method.
addAll() Method Exceptions:
ClassCastException: We get this exception if the argument is of incompatible type compared to the elements in the target TreeSet object.
NullPointerException: We get this exception if the argument is a null value.
Example: using TreeSet addAll() method
import java.util.TreeSet; import java.util.ArrayList; class Main < public static void main(String[] args) < TreeSetts = new TreeSet<>(); ts.add(200); ts.add(2); ts.add(3); ts.add(0); ts.add(10); ts.add(30); ArrayList list = new ArrayList<>(); list.add(1000); list.add(2000); list.add(3000); ts.addAll(list); for (int i: ts) < System.out.println(i); >> >
0 2 3 10 30 200 1000 2000 3000
Java TreeSet Tutorial with Examples
Java TreeSet tutorial with examples will help you understand how to use Java TreeSet in an easy way. TreeSet in Java is a Set implementation based on the TreeMap. Unlike HashMap, the elements of the TreeSet are ordered using their natural ordering. Also, unlike the Map implementations, for example, TreeMap or HashMap, the TreeSet does not maintain key value pairs but offers the uniqueness of its elements.
If you do not want to sort the TreeSet elements using their natural ordering, you can provide a custom Comparator when you create the TreeSet object using the below given constructor.
The TreeSet in Java provides log(n) time cost for the add , remove and contains operations.
The TreeSet implementation is not synchronized. That means if multiple threads are trying to modify the TreeSet object at the same time then the access must be synchronized explicitly. If this is the case, you can either synchronize the TreeSet object or you can get the synchronized SortedSet object from the existing TreeSet using synchronizedSortedSet method of the Collections class as given below.
The Iterator object obtained by the iterator method of the TreeSet is fail-fast. That means if the TreeSet object is structurally modified after obtaining the iterator (except by using iterator’s own remove method), calling any methods of the iterator object will throw ConcurrentModificationException .
How to create a new TreeSet object?
The TreeSet class in Java provides several constructors using which we can create new TreeSet objects.
1. How to create a new empty TreeSet object?
The default constructor of the TreeSet class creates a new empty TreeSet object.