How to make an array of Set<String>? - java

I am trying to declare an array of Set<String> so I do not have to manage each sets separately. But things go wrong:
ArrayList<Set<String>> categories=new LinkedHashSet<>();
Here, Java says that type Set<String> is erroneous and then reports an error.
If this is wrong, then how can I make an array of :
static Set<String> category1 = new LinkedHashSet<>();

You are initialising an ArrayList with LinkedHashSet object and hence the error:
ArrayList<Set<String>> categories=new LinkedHashSet<>();
change it to
ArrayList<Set<String>> categories=new ArrayList<>();
you need to use HashSet when you create a Set to be added into the list. Something like this:
Set<String> firstSet = new HashSet<String>();
//build your set
//add set to list
categories.add(firstSet);
Btw, you mentioned Array in your question description, so here is the declaraiton for plain array of Sets:
Set<String>[] categories=new HashSet[10];

You can do something like:
List<HashSet> list =new ArrayList<HashSet>();
HashSet<String> hs =new HashSet<String>();
hs.add(value1);
hs.add(value2);
list.add(hs);
You can use for or while loop to add values to set(hs) and then add the set to list.

Related

adding lists into a set java

How do I add a list of things into a set?
When I do set.addAll I get an error
required type :Collection <? extends List>
provided type :List
public static Set<List<Food>> getAllMealPlans(Set<Food> foods, int numMeals) {
Set<List<Food>> set = new HashSet<>();
List<Food> aList = new ArrayList<Food>(foods);
List<Food> sortedList = aList.stream().sorted(Comparator.comparing(a -> a.meal)).collect(Collectors.toList());
set.addAll(sortedList);
Set<List<Food>> set = new HashSet<>();
This set object is a Set of List s. This means every item in the set is a List<Food>.
How do I add a list of things into a set?
As you want to create a Set which contains multiple lists, you can simply use set.add(). This will insert the sortedList as an item in the set which will end up what you are looking for.
set.add(sortedList);
When to use addAll()?
Adds all of the specified elements to the specified collection.
Elements to be added may be specified individually or as an array.
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#addAll(java.util.Collection,%20T...)
Possible enhancements
As you are already using java stream, I will get rid of aList variable like below.
List<Food> sortedList = foods.stream().sorted(Comparator.comparing(a -> a.meal)).collect(Collectors.toList());
You can actually remove stream operations. First collect set items to a List object and then perform sorting with a method references in your comparator.
List foodList = new ArrayList(set);
foodList.sort(Comparator.comparing(Food::meal));

Created two Lists from same pojo array, Modifying one List, same thing affects on other list also

I have created two lists object from the same pojo and sorted one of them. When I tried to change one list, other lists also got updated.
List<FilterPojo.Data> filterList = new ArrayList<>();
List<FilterPojo.Data> subFilterList = new ArrayList<>();
If I change the value in filterList, same changes occur in subFilterList
With the limited information that is provided by you, it seems you are creating/populating subFilterList as subList of filterList. When you do that, all changes made in either of the list will be reflected in other.
This happens because List.subList(), returns a view of the list, so modifications to the original list will be reflected in the sub-list. As suggested by others, instead of subList use addAll to populate subFilterList
This could be reference problem. Lists maintains their references when items are copied to other list, if you do something like:
List<FilterPojo.Data> subFilterList = filterList;
Use addAll method instead,
subFilterList.clear();
subFilterList.addAll(filterList);
Try below
List<String> filterList = new ArrayList<String>();
List<String> subFilterList = new ArrayList<String>();
filterList.add("A");
filterList.add("B");
filterList.add("C");
/*subFilterList = filterList; // reference to same object , change will reflect in both
filterList.add("C");
System.out.println(filterList);
System.out.println(subFilterList);*/
subFilterList.addAll(filterList);
filterList.add("C");
System.out.println(filterList);
System.out.println(subFilterList);
I don't know exactly the context that you are asking.
Your lists are holding the same object. For example, in this case p1.
Person p1 = new Person();
List<Person> list1 = new ArrayList<Person>();
list1.add(p1);
List<Person> list2 = new ArrayList<Person>();
list2.add(p1);
p1.setName("new name");

Comparing an arrayList with a Set in java

My set looks like this,
final public static Set<String> env = new HashSet<String>(
Arrays.asList("DEV", "QA", "PREPROD", "PROD"));
And my arrayList will be populated dynamically from DB which will have all the 4 or lesser than that (ex: DEV,QA)
How do i find the missing elements in arraylist and print those?
Given two collections, a and b, here is a way to list the contents of a that are not also in b.
a.stream()
.filter(x -> !b.contains(x))
.forEach(System.out::println);
Without streams, you could do this:
for (String x : a) {
if (!b.contains(x)) {
System.out.println(x);
}
}
Copy the Set and remove all of the elements in the List using the removeAll method. You'll be left with a Set containing the missing ones:
List<String> list = ...;
Set<String> set = ...;
Set<String> copy = new HashSet<>(set);
copy.removeAll(list);
System.out.println(copy);
You should use an Enum that contains all possible values in code.
Then you could do :
Arrays.asList(YourEnum.values()).removeAll(<elements_got_from_database>));
for (String element : array)
if (!otherarray.contains(element))
print(element);

Collections sort on a copy of a Arraylist (to keep the orginal order)

If i have an arraylist, and i want both a sorted and unsorted version of it, how can i achive it.
tried with Collections
ArrayList<Integer> someNumbers = new ArrayList<>();
if i then make a new arraylist = someNumbers and sort it. The orginal one gets sorted too ?
ArrayList<Integer> sortedNumbers = someNumbers;
Collections.sort(sortedNumbers);
both list gets sorted.
How can i simply achive what im trying to do ? ( get a sorted copy, and keep the orginal in its orginal ordering )
When you do ArrayList<Integer> sortedNumbers = someNumbers;, you are not creating a new list to sort. You are basically assigning the variable sortedNumbers to the exact same list that someNumbers is assigned to.
To fix it, you can create a new list from your other list. Like this:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
The reason why both lists get sorted is because even you first create a new ArrayList(), you then override this instance with a reference to someNumbers. Therefore when you sort sortedNumbers you actually sort the original list someNumbers.
There are several ways you can achieve your goal:
ArrayList<Integer> sortedNumbers = new ArrayList<>();
sortedNumbers.addAll(someNumbers);
Or you can achieve the same in one step by using the copy constructor:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
You need to make a copy of the list and then sort it, e.g.
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
This uses ArrayList(java.util.Collection) constructor
Just copy the array and sort the copy:
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);
You have to copy the ArrayList first to have two versions. Then you can sort one of the two ArrayLists
ArrayList<Integer> someNumbers = ...
ArrayList<Integer> sortedNumbers = new ArrayList<>(someNumbers);
Collections.sort(sortedNumbers);

How to correctly specify a list in java

I am using Eclipse Juno and Java.
I want to create a list and then store that list in another list so I can pass the list of lists to the server side. I have tried:
ArrayList<T> listAccountAndCubs = new ArrayList<Comparable>();
listAccountAndCubs.add(accountId);
listAccountAndCubs.add(sqlDateArchived);
However, I can not get the values "T" and "Comparable" correct. I tried "String" however that does not work for storing the date.
Once the above is correct how do I set up the list to contain "listAccountAndCubs"?
Thanks for any assistance,
Glyn
this is how you can create a list
List<String> l = new ArrayList<String>();
this is how you can create list of list
List<List<Comparable>> listOfList = new ArrayList<List<Comparable>>();
listOfList.add(new ArrayList<Comparable>());
...
Sounds like you want something like this
List<List<String>> listAccountAndCubs = new ArrayList<List<String>>();
I would recomment using Google Guava library to clean the syntax a bit
List<List<String>> listAccountAndCubs = Lists.newArrayList();
List<ArrayList<Comparable>> listAccountAndCubs = new ArrayList<>();
or
List<String> l1=new ArrayList<>();
List<List<String>> l2=new ArrayList<>();
l1.add("a");
l2.add(l1);
If I understand you crrectly you want to have a list of Strings, and store this in another list?
List<String> sl = new ArrayList<String>();
List<List<String>>sls = new ArrayList<List<String>>();
sls.add(sl);
sl.add("String 1");
The value "T" is just a placeholder for the type, as the list is a generic interface, which can take any arbitrary object.
If you want to create a list of unspecified types, you would use
List<?>list = new ArrayList<?>();
Then you can add untyped objects to it, but in your case this is not neccessary.
Instead you can of course also create a list of comparables. Like this:
List<Comparable<String>>list = new ArrayList<Comparable<String>>();

Categories

Resources