import java.util.HashSet; import java.util.Set; public class StringUtil { public static Set convertToSet(String string) { Set resultSet = new HashSet(); for (int i = 0; i < string.length(); i++) { resultSet.add(new Character(string.charAt(i))); } // Return result return ...
importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importjava.util.Set;publicclassConvertSetToList{publicstaticvoidmain( String[] args ){ System.out.println("Set values ..."); Set<String> set =newHashSet<String>(); set.add("1"); set.add("2"); set.add("3"); s...
arpit.java2blog; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ListToSetMain { public static void main(String args[]) { List<String> listofCountries=new ArrayList<>(); listofCountries.add("India"); listofCountries.add("...
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { public static void main(String[] args) { List<String> myList = new ArrayList<String>(); myList.add("A"); myList.add("B"); myList.add("C"); myList.add("D"...
You can convert JSON String to Java object in just 2 lines by using Gson as shown below : Gson g = new Gson(); Player p = g.fromJson(jsonString, Player.class) You can also convert a Java object to JSON by using the toJson() method as shown below String str = g.toJson(p); ...
In this example, we have usedFunction.identity()to use the same element as key in the Map andString::valueOftoconvert Integer to Stringas we are creating a Map of String to Integer. 3.2 Stream to LinkedHashMap Example You can convert a Stream to LinkedHashMap by using the same toMap(...
Output: ArrayList with 3 elements {“Welcome”, “to”, “BeginnersBook”} The steps to convert string to ArrayList: 1) First split the string usingString split() methodand assign the substrings into an array of strings. We can split the string based on any character, expression etc. ...
Example to Convert Java Set to String importscala.jdk.CollectionConverters._objectmyObject{defmain(args:Array[String]):Unit={valjavaSet=newjava.util.HashSet[Int]()javaSet.add(4535)javaSet.add(2003)javaSet.add(111)valscalaString=javaSet.toString println("The string conversion of java set is ...
import java.util.Set; public class helloWorld { public static void main(String[] args) { Map map = new HashMap(); Map mapcopy = new HashMap(); map.put(1,2);//插入映射 map.put(2, 4); map.put(9, 10); map.get(1);//根据key,获取value ...
Date date=sdf.parse(s);return date;} catch (ParseException e) { e.printStackTrace();} } return null;} } 2.定义全局对象转换器 这⾥使⽤Jackson的ObjectMapper类的readValue()函数实现将Json字符串反序列化为Java对象 @Component public class ObjectConvert implements Converter<String,User> { ...