Combining two ArrayLists [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to interleave two arraylists together into one arraylist, but I can't seem to work this out. The code should work when the size of list 1 is bigger, or when the size of list 2 is bigger.
I have yet to learn any methods or terminology that's not included in the code below.
Help would be much appreciated. Thank you! Here is what I have so far:
public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> tempList = new ArrayList<>();
int count = 1;
int length = list1.size() + list2.size();
String temp1 = "";
String temp2 = "";
boolean test = true;
if (list1.size() >= list2.size())
{
for (int j = 0; j<length; j++)
{
for (int i = 0; i < length ; i++)
{
tempList.add(2*i, list1.get(i));
tempList.add(2*i+1, list2.get(i));
count++;
if (count == list2.size())
{
break;
}
}
}
list1.clear();
list1.addAll(tempList);
}
else
{
for (int i = 0; i<length; i++)
{
list1.add(2*i+1, list2.get(i));
count++;
if (count == list1.size())
{
break;
}
}
}
public static void main(String[] args)
{
ArrayList list1 = new ArrayList();
list1.add(2);
list1.add(3);
list1.add(7);
ArrayList list2 = new ArrayList();
list2.add("Hi?");
list2.add("name");
list2.add("mallory");
list2.add("nice");
ArrayList list3 = new ArrayList();
list3.add("my");
list3.add("is");
list3.add("pizzazz");
interLeave(list2, list3);
System.out.print(list2);

How about (Just to simplify)
public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> tempList = new ArrayList<String>();
int length = Math.max(list1.size(), list2.size());
for (int j = 0; j<length; j++)
{
if (j < list1.size()) {
tempList.add(2*j, list1.get(j));
}
if (j < list2.size()) {
tempList.add(2*j+1, list2.get(j));
}
}
list1.clear();
list1.addAll(tempList);
}

Try this
import java.util.ArrayList;
public class Test{
public static void interLeave(ArrayList<String> list1,
ArrayList<String> list2) {
ArrayList<String> tempList = new ArrayList<>();
int count = 1;
int length = list1.size() + list2.size();
String temp1 = "";
String temp2 = "";
boolean test = true;
if (list1.size() >= list2.size()) {
for (int j = 0; j < list2.size(); j++) {
for (int i = 0; i < list2.size(); i++) {
tempList.add(2 * i, list1.get(i));
tempList.add(2 * i + 1, list2.get(i));
count++;
if (count == list2.size()) {
break;
}
}
}
list1.clear();
list1.addAll(tempList);
} else {
for (int i = 0; i < length; i++) {
list1.add(2 * i + 1, list2.get(i));
count++;
if (count == list1.size()) {
break;
}
}
}
}
public static void main(String[] args) {
ArrayList list1 = new ArrayList();
list1.add(2);
list1.add(3);
list1.add(7);
ArrayList list2 = new ArrayList();
list2.add("Hi?");
list2.add("name");
list2.add("irene");
list2.add("nice");
ArrayList list3 = new ArrayList();
list3.add("my");
list3.add("is");
list3.add("Lee");
interLeave(list2, list3);
System.out.print(list2);
}
}
Output is
[Hi?, my, name, is, irene, Lee, Hi?, my, name, is, irene, Lee, Hi?, my, name, is]

One more solution.
public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> tempList = new ArrayList<>();
Iterator<String> Iterator1 = list1.iterator();
Iterator<String> Iterator2 = list2.iterator();
while (Iterator1.hasNext() && Iterator2.hasNext())
{
tempList.add(Iterator1.next());
tempList.add(Iterator2.next());
}
while (Iterator1.hasNext()){
tempList.add(Iterator1.next());
}
while (Iterator2.hasNext()){
tempList.add(Iterator2.next());
}
list1.clear();
list1.addAll(tempList);
}

Related

java- removing substring in a list of strings

Consider the case of a list of strings
example : list=['apple','bat','cow,'dog','applebat','cowbat','dogbark','help']
The java code must check if any element of string is a subset of another element and if it is then larger string element must be removed.
so in this case strings 'applebat','cowbat','dogbark, are removed.
The approach I have taken was to take two lists and iterate over them in the following way,
ArrayList<String> list1 = new ArrayList<String>(strings);
ArrayList<String> list2 = new ArrayList<String>(strings);
for(int i = 0; i<list1.size();i++)
{
String curr1 = list1.get(i);
for(int j = 0;j<list2.size();j++)
{
String curr2 = list2.get(j);
if(curr2.contains(curr1)&&!curr2.equals(curr1))
{
list2.remove(j);
j--;
}
}
}
IMPORTANT I have lists with the sizes of 200K to 400K elements.I would like to find a way to improve performance. I even tried hashsets but they were not much help.I am facing issues with the time taken by the program.
Can any one suggest any improvements to my code or any other approaches in java to improve performance??
import java.util.ArrayList;
import java.util.*;
// our main class becomes a file but the main method is still found
public class HelloWorld
{
public static void main(String[] args)
{
String[] strings = {"apple","bat","cow","dog","applebat","cowbat","dogbark","help"};
ArrayList<String> list1 = new ArrayList<String>(Arrays.asList(strings));
ArrayList<String> list2 = new ArrayList<String>(Arrays.asList(strings));
ArrayList<String> result = new ArrayList<String>(Arrays.asList(strings));
for(int i = 0; i<8;i++)
{
String curr1 = list1.get(i);
System.out.println(curr1);
int flag = 0;
for(int j = i+1;j<8;j++)
{
String curr2 = list2.get(j);
if((curr2.contains(curr1)&&!curr2.equals(curr1)))
{
result.remove(curr2);
}
}
}
System.out.println(result);
}
}
For full performance boost of huge list of words, I would think a combination of sort and a string searching algorithm, such as the Aho–Corasick algorithm, is what you need, assuming you're willing to implement such complex logic.
First, sort the words by length.
Then build up the Aho–Corasick Dictionary, in word length order. For each word, first check if a substring exists in the dictionary. If it does, skip the word, otherwise add the word to the dictionary.
When done, dump the dictionary, or the parallel-maintained list if dictionary is not easy/possible to dump.
I suppose set will be faster here.
You can easy do that with java8 stream api.
Try that:
private Set<String> delete() {
Set<String> startSet = new HashSet<>(Arrays.asList("a", "b", "c", "d", "ab", "bc", "ce", "fg"));
Set<String> helperSet = new HashSet<>(startSet);
helperSet.forEach(s1 -> helperSet.forEach(s2 -> {
if (s2.contains(s1) && !s1.equals(s2)) {
startSet.remove(s2);
}
}));
return startSet;
}
Do not delete any elements from set you are iterating for or you will have ConcurrentModificationException.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class SubStrRmove {
public static List<String> randomList(int size) {
final String BASE = "abcdefghijklmnopqrstuvwxyz";
Random random = new Random();
List<String> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
int length = random.nextInt(3) + 2;
StringBuffer sb = new StringBuffer();
for (int j = 0; j < length; j++) {
int number = random.nextInt(BASE.length());
sb.append(BASE.charAt(number));
}
list.add(sb.toString());
sb.delete(0, sb.length());
}
return list;
}
public static List<String> removeListSubStr(List<String> args) {
String[] input = args.toArray(new String[args.size()]);
Arrays.parallelSort(input, (s1, s2) -> s1.length() - s2.length());
List<String> result = new ArrayList<>(args.size());
for (int i = 0; i < input.length; i++) {
String temp = input[i];
if (!result.stream().filter(s -> temp.indexOf(s) >= 0).findFirst().isPresent()) {
result.add(input[i]);
}
}
return result;
}
public static List<String> removeListSubStr2(List<String> args) {
String[] input = args.toArray(new String[args.size()]);
Arrays.parallelSort(input, (s1, s2) -> s1.length() - s2.length());
List<String> result = new ArrayList<>(args.size());
for (int i = 0; i < input.length; i++) {
boolean isDiff = true;
for (int j = 0; j < result.size(); j++) {
if (input[i].indexOf(result.get(j)) >= 0) {
isDiff = false;
break;
}
}
if (isDiff) {
result.add(input[i]);
}
}
return result;
}
public static void main(String[] args) throws InterruptedException {
List<String> list = randomList(20000);
Long start1 = new Date().getTime();
List<String> listLambda = removeListSubStr(list);
Long end1 = new Date().getTime();
Long start2 = new Date().getTime();
List<String> listFor = removeListSubStr2(list);
Long end2 = new Date().getTime();
System.out.println("mothod Labbda:" + (end1 - start1) + "ms");
System.out.println("mothod simple:" + (end2 - start2) + "ms");
System.out.println("" + listLambda.size() + listLambda);
System.out.println("" + listFor.size() + listFor);
}
}
I have tested it on small data and hope it helps you to find solution...
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args){
String []list = {"apple","bat","cow","dog","applebat","cowbat","dogbark","help","helpless","cows"};
System.out.println(Arrays.toString(list));
int prelenght = 0;
int prolenght = 0;
long pretime = System.nanoTime();
for(int i=0;i<list.length;i++){
String x = list[i];
prelenght = list[i].length();
for(int j=i+1;j<list.length;j++){
String y = list[j];
if(y.equals(x)){
list[j] = "0";
}else if(y.contains(x)||x.contains(y)){
prolenght = list[j].length();
if(prelenght<prolenght){
list[j] = "0";
}
if(prelenght>prolenght){
list[i] = "0";
break;
}
}
}
}
long protime = System.nanoTime();
long time = (protime - pretime);
System.out.println(time + "ns");
UpdateArray(list);
}
public static void UpdateArray(String[] list){
ArrayList<String> arrayList = new ArrayList<>();
for(int i=0;i<list.length;i++){
if(!list[i].equals("0")){
arrayList.add(list[i]);
}
}
System.out.println(arrayList.toString());
}
}
Output :
[apple, bat, cow, dog, applebat, cowbat, dogbark, help, helpless, cows]
time elapsed : 47393ns
[apple, bat, cow, dog, help]

Java: First item of Array do not display correctly

I need to compare words to tweets and count how many times they appear on text.
I used two for cycle to compare ArrayList with words and ArrayList with tweets but the first word of ArrayList with words don't display correctly and don't want to count.
Output Image
It is supposed to champions word count twice
My code is:
Read txt with words and save on arraylist
public ArrayList <String> fread(String dir) throws FileNotFoundException, IOException {
Scanner s = new Scanner(new File(dir));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
list.add(s.next().toLowerCase());
}
s.close();
return list;
}
Get Tweets and save them on arraylist
public ArrayList<String> showTimeLine() throws TwitterException {
List<Status> statuses = twitter.getHomeTimeline(new Paging (1,200));
ArrayList<String> allTweets=new ArrayList<String>();
for (Status status : statuses) {
allTweets.add(status.getText().replaceAll("https[^\\s]+","").toLowerCase());
}
return allTweets;
}
Compare two arrays:
public ArrayList<String> result(ArrayList<String>tweets, ArrayList<String> palavras){
for (int i = 0; i <palavras.size() ; i++) {
int count = 0;
for (int j = 0; j <tweets.size() ; j++) {
if (tweets.get(j).contains(palavras.get(i))){
count++;
}
}
numero.add(count);
result.add(palavras.get(i));
}
return result;
}
And print
for (int i = 0; i <result.size() ; i++) {
System.out.printf("%40s",result.get(i)+" "+numero.get(i)+"\n");
}
Static ArrayList:
static ArrayList<Integer> numero = new ArrayList<Integer>();
static ArrayList<String> result = new ArrayList<String>();
Thanks ! :)
In the method
public ArrayList<String> result(ArrayList<String>tweets, ArrayList<String> palavras){
for (int i = 0; i <palavras.size() ; i++) {
int count = 0;
for (int j = 0; j <tweets.size() ; j++) {
if (tweets.get(j).contains(palavras.get(i))){
count++;
}
}
numero.add(count);
result.add(palavras.get(i));
}
return result;
}
Your if-statement finds a single result and then ends. You can fix this like so
public static ArrayList<String> result(ArrayList<String>tweets, ArrayList<String> palavras){
for (int i = 0; i <palavras.size() ; i++) {
int count = 0;
for (int j = 0; j <tweets.size() ; j++) {
boolean isDone = false; //NEW
int tweetCharIter = 1; //NEW
while (!isDone){ //NEW
if (tweets.get(j).substring(tweetCharIter).contains(palavras.get(i))){ //ALTERED
tweetCharIter += tweets.get(j).substring(tweetCharIter).indexOf(palavras.get(i)) +1; //ALTERED
count++;
} else { //NEW
isDone = true; //NEW
}
}
}
numero.add(count);
result.add(palavras.get(i));
}
return result;
}

Printing empty brackets?

I have a simple program that should take two arrays, convert them into lists, and take out the values that are the same. For some reason though I am getting a value of:
[]
[]
[]
[]
Although the result should be "eggs", "lasers", "hats", "pie" - "lasers", "hats" which should get me a result of "eggs" "pie".
Here is my code:
public class Arraystring {
public static void main(String[] args) {
String[] things = { "eggs", "lasers", "hats", "pie" };
List<String> list1 = new ArrayList<String>();
for (String x : things) {
list1.add(x);
}
String[] thingstwo = { "lasers", "hats" };
List<String> list2 = new ArrayList<String>();
for (int i = 0; i < list2.size(); i++) {
list2.add(thingstwo[i]);
}
editlist(list1, list2);
}
public static void editlist(Collection<String> l1, Collection<String> l2) {
Iterator<String> it = l1.iterator();
while (it.hasNext()) {
if (l2.contains(it.next())) {
System.out.println("hui");
it.remove();
}
System.out.println(l2);
}
}
}
First your line in Arraystring {..}
for (int i = 0; i < list2.size(); i++) {
should be
for (int i = 0; i < thingstwo.length; i++) {
And then you are printing the wrong list in editlist(..):
System.out.println(l2);
should be
System.out.println(l1);

Java array I need your thought

Given an array of strings, return another array containing all of its longest strings.
For (String [] x = {"serm", "aa", "sazi", "vcd", "aba","kart"};)
output will be
{"serm", "sazi" , "kart"}.
The following code is wrong, What can I do to fix it.
public class Tester {
public static void main(String[] args) {
Tester all = new Tester();
String [] x = {"serm", "aa", "sazi", "vcd", "aba","kart"};
String [] y = all.allLongestStrings(x);
System.out.println(y);
}
String[] allLongestStrings(String[] input) {
ArrayList<String> answer = new ArrayList<String>(
Arrays.asList(input[0]));
for (int i = 1; i < input.length; i++) {
if (input[i].length() == answer.get(0).length()) {
answer.add(input[i]);
}
if (input[i].length() > answer.get(0).length()) {
answer.add(input[i]);
}
}
return answer.toArray(new String[0]);
}
}
I will give you solution, but as it homework, it will be only sudo code
problem with your solution is, you are not finging longest strings, but strings same size or bigger than size of first element
let helper = []
let maxLength = 0;
for each string in array
if (len(string) >maxLength){
maxLength = len(string);
clear(helper)
}
if (len(string) == maxLength)
helper.add(string)
}
return helper;
You can try below code
private static String[] solution(String[] inputArray) {
int longestStrSize = 0;
List<String> longestStringList = new ArrayList<>();
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] != null) {
if (longestStrSize <= inputArray[i].length()) {
longestStrSize = inputArray[i].length();
longestStringList.add(inputArray[i]);
}
}
}
final int i = longestStrSize;
return longestStringList.stream().filter(x -> x.length() >= i).collect(Collectors.toList()).stream()
.toArray(String[]::new);
}

How to iterate through two dimensional ArrayList using iterator?

I would like to iterate through two dimensional ArrayList which includes String objects using iterator. I also would like to iterate in a way that let me choose whether I want to iterate horizontally(row) first or vertically(column) by using a boolean value. How can I implement this in java?
What I've tried so far.
public class IterateThis implements Iterator<String>{
ArrayList<ArrayList<String>> array;
public IterateThis(){
array = new ArrayList<ArrayList<String>>();
array.add(new ArrayList<String>());
array.add(new ArrayList<String>());
array.add(new ArrayList<String>());
array.get(0).add("1");
array.get(0).add("2");
array.get(0).add("2");
array.get(1).add("4");
array.get(1).add("5");
array.get(1).add("6");
}
Iterator<String> it = array.iterator(); //This gives me an error...why?
I don't know how I can implement the boolean value though.
Maybe you need to implement two versions, with a boolean that decides which loop to use:
public void iterate(boolean horizantalFirst){
if(horizontalFirst){
for(int i=0; i<array.size(); i++){ // first iterate through the "outer list"
for(int j=0; j<array.get(i).size(); j++){ // then iterate through all the "inner lists"
array.get(i).get(j)="1";
}
}
}else{
int j=0; // index to iterate through the "inner lists"
for(; j<array.get(j).size(); j++){ //dangerous, you need to be sure that there is a j-th element in array
for(int i=0; i<array.size(); i++){ // iterate here through the outer list, by always working on the j-th element
array.get(i).get(j)="1";
}
}
}
}
Why not try this:
import java.util.ArrayList;
public class Iteration
{
private ArrayList<ArrayList<String>> array;
public Iteration()
{
array = new ArrayList<>();
array.add(new ArrayList<String>());
array.get(0).add("000");
array.get(0).add("001");
array.get(0).add("010");
array.add(new ArrayList<String>());
array.get(1).add("100");
array.get(1).add("101");
array.get(1).add("110");
array.get(1).add("111");
iterateRowWise();
System.out.println("\n\n");
iterateColumnWise();
}
public void iterateRowWise()
{
// This uses iterator behind the scene.
for (ArrayList<String> row : array)
{
for (String element : row)
{
System.out.print(element + " ");
}
System.out.println();
}
}
public void iterateColumnWise()
{
int arraySize = array.size();
int maxColumns = getMaximumListSize();
for (int c = 0; c < maxColumns; c++)
{
for (int r = 0; r < arraySize; r++)
{
ArrayList<String> rowList = array.get(r);
if (c < rowList.size())
{
System.out.print(rowList.get(c) + " ");
}
}
System.out.println();
}
}
private int getMaximumListSize()
{
int maxListSize = 0;
for (ArrayList<String> rowList : array)
{
if (maxListSize < rowList.size())
maxListSize = rowList.size();
}
return maxListSize;
}
public static void main(String[] args)
{
new Iteration();
}
}
The iterateRowWise() method iterates using the iterator, but it does so behind the scene.
The iterateColumnWise() method doesn't use iterator, but its safe to use.
Row-wise iteration is simple as shown in the #Awfully Awesome answer.
Tried a columnwise iteration with assumption that List will always have m cross n elements where m=n
public static void IterateThis() {
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
array.add(new ArrayList<String>());
array.add(new ArrayList<String>());
array.get(0).add("1");
array.get(0).add("2");
array.get(0).add("2");
array.get(1).add("4");
array.get(1).add("5");
array.get(1).add("6");
Iterator<ArrayList<String>> it = array.iterator();
int topLevelIteratorResetCounter = 0;
int noOfIteratorNextRequired = 1;
int size = array.size();
while (it.hasNext()) {
ArrayList<String> strList = it.next();
if (noOfIteratorNextRequired > strList.size())
break;
Iterator<String> itString = strList.iterator();
int numtimes = 0;
String str = null;
while (numtimes != noOfIteratorNextRequired) {
str = itString.next();
numtimes++;
}
System.out.println(str);
numtimes = 0;
topLevelIteratorResetCounter++;
if (topLevelIteratorResetCounter == size) { //as column count is equal to column size
it = array.iterator(); //reset the iterator
noOfIteratorNextRequired++;
topLevelIteratorResetCounter = 0;
}
}
}
The answer uses Iterator.

Categories